ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your

 遇到这个报错?不知道怎么办?要哭了?

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 '("123456") where user='root'' at line 1

 

MySQL grant的SQL命令其实在5.X之后就已经被弃用了


首先,你先检查一下你的MySQL版本, 大多数执行报错的MySQL版本是8.0的。


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;

正确的执行赋权


那么在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版本,所以建议大家以后使用这种方式赋权,一键建用户加赋权官方已经弃用了。