首頁  >  文章  >  資料庫  >  MySQL在grant時報錯ERROR 1064 (42000)如何解決

MySQL在grant時報錯ERROR 1064 (42000)如何解決

PHPz
PHPz轉載
2023-05-28 23:53:133879瀏覽

網路上查到的grant方式大多會報錯,主要原因是MySQL版本8.0後不能再使用原來的方式

查詢MySQL版本

SELECT version();

在8.0版本下

grant all privileges on test.* to test@'%' identified by '123456';

錯誤

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 your MySQL server version for the right syntax to use near 'identified by '1123566 '' at line 1

正確的grant方式

create user test@'localhost' identified by '123456';
grant all privileges on test.* to test@'localhost';
flush privileges;

MySQL8.0密碼登入不上

alter user test@'localhost' identified with mysql_native_password by '123456';

補充:MySQL ERROR 1064 (42000) ——不管怎樣grant總是報錯,怎麼回事?

用過MySQL的朋友都知道,常常會有一些grant(授權)操作,不知道你有沒有遇過這樣的問題。

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 that near ‘identified時間; ; at line 1

MySQL grant的SQL

貼到終端執行,報錯!
每個字母敲後執行,又報錯!
重複確認很多遍執行,又又報錯!

要瘋了,怎麼辦。別急,接著看文章。

版本的差異導致

首先,你先檢查一下你的MySQL版本, 大多數執行錯誤的MySQL版本是8.0的,透過記憶甚至盲打指令都不層報錯的,估計通常都是用的最多的5.7了。信不信呢?

用真實資料測試

1.先用MySQL 8.0試試看

mysql> grant all privileges on test.* to test@'%' identified by '123456';  
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'' at line 1

報錯了

2.再用MySQL 5.7試試看

mysql> grant all privileges on test.* to test@'%' identified by '123456';    
Query OK, 0 rows affected, 1 warning (0.08 sec)
mysql> flush privileges;

成功。

細心的朋友有沒有註意到回傳結果裡有一個訊息:1 warning,是什麼內容呢?

mysql> show warnings;
+---------+------+------------------------------------------------------------------------------------------------------------------------------------+
| Level   | Code | Message                                                                                                                            |
+---------+------+------------------------------------------------------------------------------------------------------------------------------------+
| Warning | 1287 | Using GRANT for creating new user is deprecated and will be removed in future release. Create new user with CREATE USER statement. |
+---------+------+------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

原來在MySQL5.7的時候,官方就提示了,這個文法SQL將會被棄用了。

正確的執行賦權

那麼在MySQL8.0版本及以後,我們要如何正確執行grant呢?

先建立用戶,再賦予授權。

mysql> create user test@'localhost' identified by '123456';
Query OK, 0 rows affected (0.10 sec)

mysql> grant all privileges on test.* to test@'localhost';
Query OK, 0 rows affected (0.17 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.18 sec)

建議在以後的使用中採用這種方式進行權限賦予,因為它也適用於MySQL5.7版本,而官方已經廢棄了一鍵建用戶加權限賦予的方法。

以上是MySQL在grant時報錯ERROR 1064 (42000)如何解決的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除