MYSQL重置root密码

1899 查看

1.停止MYSQL服务;

在Ubuntu 或 Debian上

sudo service mysql stop

或者

sudo /etc/init.d/mysql stop

在CentOS, Fedora, RHEL上:

sudo service mysqld stop

或者

sudo /etc/init.d/mysqld stop

2.进入安全模式

sudo mysqld_safe --skip-grant-tables &

PS:最后的&符号是要有的

你可能会看到这样的信息:

mysqld_safe Can't log to error log and syslog at the same time.  Remove all --log-error configuration options for --syslog to take effect.

mysqld_safe Logging to '/var/log/mysql/error.log'.

mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

出现这些类似信息之后,你可以按CTRL+C退出,然后进行下一步

NOTES:如果,我是说如果,没有报错请直接跳到第三步,在这你遇到报错

mysqld_safe Can't log to error log and syslog at the same time. Remove all --log-error configuration options for --syslog to take effect.

mysqld_safe Logging to '/var/log/mysql/error.log'.

mysqld_safe A mysqld process already exists

请使用以下命令来确认mysql是否真的终止了

ps uaxww | grep -i mysql

一旦找到,就用kill将进程终止,再重试一次

3.使用空密码登录

mysql -u root

4.使用mysql数据库

use mysql;

你会看到这样的类似信息:

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

5.重新设置密码

update user set password=PASSWORD("yournewpassword") where User='root';

yournewpassword 替换为你设置的密码

你大概会看到这些:

mysql> update user set password=PASSWORD("yournewpassword") where User='root';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0

6.重新设置权限

flush privileges;

如果出现以下熟悉的提示,基本上大功告成。

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

7.退出

quit

8.重启MYSQL

在Ubuntu 或 Debian上

sudo /etc/init.d/mysql stop

然后

sudo /etc/init.d/mysql start

在CentOS, Fedora, RHEL上:

sudo /etc/init.d/mysqld stop

然后

sudo /etc/init.d/mysqld start

9.重新登录

mysql -u root -p

输入刚刚设置的密码。DONE

Happy Hacking