您的位置:首頁技術文章
文章詳情頁

MySQL8.x使用GRANT為用戶賦權時報錯的解決

瀏覽:103日期:2023-05-05 10:12:03
目錄
  • MySQL8.x使用GRANT為用戶賦權時報錯
    • 問題描述
    • 原因分析
    • 解決方案
  • mysql版本:'for the right syntax to use near 'identified by 'password' with grant option'
    • 總結

      MySQL8.x使用GRANT為用戶賦權時報錯

      問題描述

      在安裝 MySQL 8.x 的過程中,往往需要為 MySQL 中的一些賬戶賦予遠程訪問的權限。

      在 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

      原因分析

      通過官網文檔的指引可以知道,新版本的 MySQL 8.x 版本已經將創建賬戶和賦權的方式分開導致以上的命令在 MySQL 8.x 上執行報語法錯誤。

      解決方案

      最終解決方案

      # 創建賬戶
      CREATE USER "用戶名"@"訪問主機" IDENTIFIED BY "密碼";
      # 為創建的賬戶賦權
      GRANT "權限列表" ON "數據庫" TO "用戶名"@"訪問主機";
      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,在給新用戶授權時,發生了變化:

      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

      出錯的語句:

      grant all privileges on *.* to "root"@"172.16.10.203" identified by ?"password" with grant option

      修正后的語句:分開三次執行

      #創建賬戶
      create user "root"@"172.16.10.203" identified by ?"password"
      #賦予權限,with grant option這個選項表示該用戶可以將自己擁有的權限授權給別人
      grant all privileges on *.* to "root"@"172.16.10.203" with grant option
      #改密碼&授權超用戶,flush privileges 命令本質上的作用是將當前user和privilige表中的用戶信息/權限設置從mysql庫(MySQL數據庫的內置庫)中提取到內存里
      flush privileges;

      原因分析:

      此版的的mysql版本把將創建賬戶和賦予權限分開了。

      創建賬戶::create user ‘用戶名"@‘訪問主機" identified by ‘密碼";
      賦予權限:grant 權限列表 on 數據庫 to ‘用戶名"@‘訪問主機" ;
      with grant option這個選項表示該用戶可以將自己擁有的權限授權給別人

      總結

      以上為個人經驗,希望能給大家一個參考,也希望大家多多支持。

      標簽: MySQL
      国产综合久久一区二区三区