MySQL8.x使用GRANT為用戶賦權(quán)時(shí)報(bào)錯(cuò)的解決
目錄
- MySQL8.x使用GRANT為用戶賦權(quán)時(shí)報(bào)錯(cuò)
- 問題描述
- 原因分析
- 解決方案
- mysql版本:'for the right syntax to use near 'identified by 'password' with grant option'
- 總結(jié)
MySQL8.x使用GRANT為用戶賦權(quán)時(shí)報(bào)錯(cuò)
問題描述
在安裝 MySQL 8.x 的過程中,往往需要為 MySQL 中的一些賬戶賦予遠(yuǎn)程訪問的權(quán)限。
在 MySQL 5.x 的版本中的操作方式
GRANT ALL PRIVILEGES ON *.* TO "root"@"%" IDENTIFIED BY "123456" WITH GRANT OPTION;
在 MySQL 8.x 中版本中按照以上操作
mysql> GRANT ALL PRIVILEGES ON *.* TO "root"@"%" IDENTIFIED BY "123456" WITH GRANT OPTION; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near "IDENTIFIED BY "123456" WITH GRANT OPTION" at line 1
原因分析
通過官網(wǎng)文檔的指引可以知道,新版本的 MySQL 8.x 版本已經(jīng)將創(chuàng)建賬戶和賦權(quán)的方式分開導(dǎo)致以上的命令在 MySQL 8.x 上執(zhí)行報(bào)語法錯(cuò)誤。
解決方案
最終解決方案
# 創(chuàng)建賬戶 CREATE USER "用戶名"@"訪問主機(jī)" IDENTIFIED BY "密碼"; # 為創(chuàng)建的賬戶賦權(quán) GRANT "權(quán)限列表" ON "數(shù)據(jù)庫" TO "用戶名"@"訪問主機(jī)"; GRANT ALL ON *.* TO "root"@"%"; # 刷新 FLUSH PRIVILEGES;
mysql版本:'for the right syntax to use near 'identified by 'password' with grant option'
查詢mysql具體版本
SELECT @@VERSION
問題分析:
mysql版本8.0.13,在給新用戶授權(quán)時(shí),發(fā)生了變化:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'identified by 'password' with grant option' at line 5, Time: 0.000000s
出錯(cuò)的語句:
grant all privileges on *.* to "root"@"172.16.10.203" identified by ?"password" with grant option
修正后的語句:分開三次執(zhí)行
#創(chuàng)建賬戶 create user "root"@"172.16.10.203" identified by ?"password" #賦予權(quán)限,with grant option這個(gè)選項(xiàng)表示該用戶可以將自己擁有的權(quán)限授權(quán)給別人 grant all privileges on *.* to "root"@"172.16.10.203" with grant option #改密碼&授權(quán)超用戶,flush privileges 命令本質(zhì)上的作用是將當(dāng)前user和privilige表中的用戶信息/權(quán)限設(shè)置從mysql庫(MySQL數(shù)據(jù)庫的內(nèi)置庫)中提取到內(nèi)存里 flush privileges;
原因分析:
此版的的mysql版本把將創(chuàng)建賬戶和賦予權(quán)限分開了。
創(chuàng)建賬戶::create user ‘用戶名"@‘訪問主機(jī)" identified by ‘密碼"; 賦予權(quán)限:grant 權(quán)限列表 on 數(shù)據(jù)庫 to ‘用戶名"@‘訪問主機(jī)" ; with grant option這個(gè)選項(xiàng)表示該用戶可以將自己擁有的權(quán)限授權(quán)給別人
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持。
