我以前在 MySQL 中访问 root 用户就好了。但是最近,我已经不能了。
我可以正常登录:
mysql -u root -p
这是登录后的mysql状态:
mysql> status -------------- mysql Ver 14.14 Distrib 5.5.28, for debian-linux-gnu (i686) using readline 6.2 Connection id: 37 Current database: Current user: root@localhost SSL: Not in use Current pager: stdout Using outfile: '' Using delimiter: ; Server version: 5.5.28-0ubuntu0.12.04.3 (Ubuntu) Protocol version: 10 Connection: Localhost via UNIX socket Server characterset: latin1 Db characterset: latin1 Client characterset: utf8 Conn. characterset: utf8 UNIX socket: /var/run/mysqld/mysqld.sock Uptime: 4 min 16 sec Threads: 1 Questions: 112 Slow queries: 0 Opens: 191 Flush tables: 1 Open tables: 6 Queries per second avg: 0.437 --------------
但是当我想做任何动作时,例如:
mysql> CREATE DATABASE moyennegenerale; ERROR 1044 (42000): Access denied for user 'root'@'%' to database 'moyennegenerale'
我知道%用于表示任何主机,但我的状态清楚地表明了 localhost。有人知道可能会发生什么吗?
%
我认为您有匿名用户
尝试运行这个:
SELECT user,host,password FROM mysql.user WHERE user='';
这将显示存在哪些匿名用户。最有可能的是,您会看到一行空白用户、主机%和空白密码,如下所示:
mysql> select user,host,password from mysql.user; +-----------+-------------+-------------------------------------------+ | user | host | password | +-----------+-------------+-------------------------------------------+ | lwdba | 127.0.0.1 | *xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | | lwdba | localhost | *xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | | lwdba | % | *xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | | root | localhost | *xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | | root | 127.0.0.1 | *xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | | vanilla | localhost | | | mywife | % | | | | % | | <<<--- LOOK !!! | replicant | 10.64.113.% | *xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | | kumar | % | | +-----------+-------------+-------------------------------------------+
那么,您是如何登录的呢?运行此查询:
SELECT USER(),CURRENT_USER();
第二个函数CURRENT_USER()揭示了如何使用哪个匿名用户登录。
CURRENT_USER()
请运行
SHOW GRANTS;
这将揭示您在登录时拥有的特权。您被阻止创建数据库的事实表明您不是 root 用户,而是某个较低特权的用户。
至于重置root密码,请执行以下操作:
echo "SET PASSWORD FOR root@localhost=PASSWORD('password');" > /var/lib/mysql/rootpwd.sql service mysql restart rm -f /var/lib/mysql/rootpwd.sql