从供应商那边接手一个MySQL数据库(数据库版本为5.7.21 MySQL Community Server (GPL)),在建立帐号时遇到了“ERROR 1044 (42000): Access denied for user 'root'@'localhost' to database xxx”错误,以下所示mysql
mysql> grant all on xxx.* to xxx@'192.168.%' identified by 'xxx';
ERROR 1044 (42000): Access denied for user 'root'@'localhost' to database 'xxxx'
mysql>
照理说,root用户应该有任何权限,那么为何出现这个错误呢? 查看当前用户为root@localhost,顺便查看了一下各个root帐号的权限。以下所示:sql
mysql> select current_user() from dual;
+----------------+
| current_user() |
+----------------+
| root@localhost |
+----------------+
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 root@'localhost';
+--------------------------------------------------------------+
| Grants for root@localhost |
+--------------------------------------------------------------+
| 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 root@'127.0.0.1';
+---------------------------------------------------------------------+
| Grants for root@127.0.0.1 |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' WITH GRANT OPTION |
+---------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> show grants for root@'%';
+-------------------------------------------+
| Grants for root@% |
+-------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' |
+-------------------------------------------+
1 row in set (0.00 sec)
如上所示,root@localhost帐号没有WITH GRANT OPTION选项,关于WITH GRANT OPTION选项,若是想让受权的用户,也能够将这些权限授予给其余用户,须要选项 “WITH GRANT OPTION“ 。也就是说有这个选项就能够将权限传递给第三方。这也是上面root@localhost用户给其它用后受权报错的缘由,若是以 root@127.0.0.1登陆(此帐号拥有WITHGRANT OPTION选项),建立用户并受权就不会有这个错误,以下所示:数据库
# mysql -host 127.0.0.1 -u root -p
Enter password:
mysql> grant all on xxx.* to xxx@'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)
固然还有其它方面的缘由也可能会引发这个错误,不过在这个案例当中,确实仅仅是由于上面缘由引发。特此记录一下这个案例@!ide