創建用戶后授權的時候報錯,顯示沒有訪問權限:
mysql> grant select, process, super, replication client, reload on *.* to 'abc'@'127.0.0.1'; ERROR 1045 (28000): Access denied for user 'root'@'%' (using password: YES)
先看看系統中,有哪些數據庫用戶:
mysql> select user,host,authentication_string from mysql.user; +---------------+-----------+ | user | host | +---------------+-----------+ | abc | 127.0.0.1 | | root | % | +---------------+-----------+
這里的abc就是剛才創建的用戶。
查看一下當前登錄的用戶信息:(status命令看到不一定是真的)
mysql> status; -------------- /usr/local/mysql/bin/mysql Ver 14.14 Distrib 5.7.29, for linux-glibc2.12 (x86_64) using EditLine wrapper Connection id: 74969 Current database: Current user: root@localhost SSL: Not in use Current pager: stdout Using outfile: '' Using delimiter: ; Server version: 5.7.29-log MySQL Community Server (GPL) Protocol version: 10 Connection: Localhost via UNIX socket Server characterset: utf8mb4 Db characterset: utf8mb4 Client characterset: utf8mb4 Conn. characterset: utf8mb4 UNIX socket: /tmp/mysql.sock Uptime: 49 days 4 hours 12 min 24 sec Threads: 63 Questions: 325324823 Slow queries: 66 Opens: 34378 Flush tables: 99 Open tables: 476 Queries per second avg: 76.569 -------------- mysql> select user(), current_user(); +----------------+----------------+ | user() | current_user() | +----------------+----------------+ | root@localhost | root@% | +----------------+----------------+ 1 row in set (0.00 sec)
user()表示當前的登錄用戶;current_user()表示對應於mysql.user表里對應的賬號。
看下當前用戶權限;
mysql> show grants; +-------------------------------------------+ | Grants for root@% | +-------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' | +-------------------------------------------+ 1 row in set (0.00 sec) mysql>
可以看到root用戶是沒有with grant option權限的。正確的應該是這樣的:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION
修改方法:
mysql> select user,host,Grant_priv from mysql.user; +---------------+-----------+------------+ | user | host | Grant_priv | +---------------+-----------+------------+ | abc | 127.0.0.1 | N | | root | % | N | +---------------+-----------+------------+ mysql> update mysql.user set Grant_priv="Y" where user="root" and host="%"; mysql> flush privileges;
退出重新登錄,再次授權即可。