在安装MySQL时,随便设了一个密码,现在怎么也想不起来了,只好重设MySQL的root密码。
在网上搜到一篇文章:Resetting a forgotten MySQL root password,按照文章里的办法恢复成功。把步骤记录如下。
首先停止MySQL服务:
# service mysqld stop
接下来,使用mysqld_safe命令在后台启动MySQL服务器,这里使用了–skip-grant-tables选项,允许不使用密码连接MySQL数据库:
# /usr/bin/mysqld_safe --skip-grant-tables &
[1] 5933
nohup: ignoring input and redirecting stderr to stdout
Starting mysqld daemon with databases from /var/lib/mysql
mysqld_safe[5972]: started
接下来,登录数据库,并更新root用户的密码:
# mysql -u root mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.56
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> update user set Password=PASSWORD('new-password-here') WHERE User='root';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
现在,把mysqld_safe切换到前台,并使用CTRL-C结束它的运行:
# fg
/usr/bin/mysqld_safe --skip-grant-tables
^C
STOPPING server from pid file /var/run/mysqld/mysqld.pid
mysqld_safe[6005]: ended
然后重新启动MySQL服务:
# service mysqld start
Starting MySQL database server: mysqld.
Checking for corrupt, not cleanly closed and upgrade needing tables..
现在,就可以使用新设置的root连接数据库了:
# mysql -u root -p
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 681
Server version: 5.1.56
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> quit
Bye
|