MySQL在grant时报错ERROR 1064 (42000)如何解决

2023-05-29 08:22:04 238
摘要:网上查到的grant方式大多会报错,主要原因是MySQL版本8.0后不能再使用原来的方式

网上查到的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 by '123456'' 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 use near ‘identified by ‘123456’’ 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 an d 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版本,而官方已经废弃了一键建用户加权限赋予的方法。

你觉得这篇文章怎么样?

91 0
扫描二维码与小二CMS创始人沟通

7*24小时为您服务

小二CMS专注于高端网站定制、系统开发、商城开发、外贸网站建设、公众号开发、小程序开发、网站优化推广、安全运维等技术领域。是高端定制网站领域著名服务商!

本文标题:MySQL在grant时报错ERROR 1064 (42000)如何解决
免责声明:文章《MySQL在grant时报错ERROR 1064 (42000)如何解决》来至网络,文章表达观点不代表本站观点,文章版权属于原作者所有,若有侵权,请联系本站站长处理!
不忘初心,方得始终
中高端网站定制开发服务商
与我们取得联系
198-1095-0281 460623785

好作品自己会说话,

我们敬畏您给予的点滴信任 !

期待携手合作,请立即咨询我们:
QQ咨询
微信咨询
198-1095-0281
我们用心,期待您成为我们的客户
联系QQ客服

QQ扫码联系客服

联系微信客服

微信扫码联系客服

微信号: 198-1095-0281
添加微信好友, 详细咨询相关信息。

复制并跳转微信
19810950281