ERROR 1044 (42000): Access denied for user ‘root‘@‘localhost‘

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ERROR 1044 (42000): Access denied for user ‘root‘@‘localhost‘相关的知识,希望对你有一定的参考价值。

从供应商那边接手一个mysql数据库(数据库版本为5.7.21 MySQL Community Server (GPL)),在创建账号时遇到了ERROR 1044 (42000): Access denied for user ‘root‘@‘localhost‘ to database xxx错误,如下所示

 

 

mysql> grant all on xxx.* to [email protected]‘192.168.%‘ identified by ‘xxx‘;
ERROR 1044 (42000): Access denied for user ‘root‘@‘localhost‘ to database ‘xxxx‘
mysql> 

 

照理说,root用户应该有任何权限,那么为什么出现这个错误呢? 查看当前用户为[email protected],顺便查看了一下各个root账号的权限。如下所示:

 

 

mysql> select current_user() from dual;
+----------------+
| current_user() |
+----------------+
| [email protected] |
+----------------+
1 row in set (0.00 sec
 
mysql> select host,user from user where user=‘root‘;
+-----------+----------+
| host      | user     |
+-----------+----------+
| %         | root     |
| 127.0.0.1 | root     |
| ::1       | root     |
| localhost | root     |
+-----------+----------+
7 rows in set (0.00 sec)
 
mysql>  show grants for [email protected]‘localhost‘;
+--------------------------------------------------------------+
| Grants for [email protected]                                    |
+--------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘localhost‘            |
| GRANT PROXY ON ‘‘@‘‘ TO ‘root‘@‘localhost‘ WITH GRANT OPTION |
+--------------------------------------------------------------+
2 rows in set (0.00 sec)
 
mysql> show grants for [email protected]‘127.0.0.1‘;
+---------------------------------------------------------------------+
| Grants for [email protected]                                           |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘127.0.0.1‘ WITH GRANT OPTION |
+---------------------------------------------------------------------+
1 row in set (0.00 sec)
 
mysql> show grants for [email protected]‘%‘;
+-------------------------------------------+
| Grants for [email protected]%                         |
+-------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘%‘ |
+-------------------------------------------+
1 row in set (0.00 sec)

 

 

 

如上所示,[email protected]账号没有WITH GRANT OPTION选项,关于WITH GRANT OPTION选项,如果想让授权的用户,也可以将这些权限授予给其他用户,需要选项 WITH GRANT OPTION 。说就是将权限传递给第三方。这也是上面[email protected]用户给其它用后授权报错的原因,如果以 [email protected]登录(此账号拥有WITHGRANT OPTION选项),创建用户并授权就不会有这个错误:

 

 

# mysql -host 127.0.0.1 -u root -p
Enter password: 
 
 
 
 
mysql>  grant all on eqmobilewarehouse.* to [email protected]‘192.168.%‘ identified by ‘test1249‘;
Query OK, 0 rows affected, 1 warning (0.00 sec)
 
 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

 

 

当然还有其它方面的原因也会引起这个错误,不过在这个案例当中,确实仅仅是因为上面原因引起。特此记录一下这个案例@

以上是关于ERROR 1044 (42000): Access denied for user ‘root‘@‘localhost‘的主要内容,如果未能解决你的问题,请参考以下文章

mysql ERROR 1044 (42000): Access denied for user ''@'localhost' to database

ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'ambari

[故障解决]Mysql爆出ERROR 1044 (42000)的错误怎么办?

ERROR 1044 (42000): Access denied for user 'root'@'localhost' to database 'mysql

解决 ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql&

ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'db'(示