Mysql忘记root密码怎么办?(已解决)

为了写这篇文档,假装一下忘记密码!!!!

首先我数据库是正常的,可以使用。

root@localhost:~# mysql -uroot -p
mysql>
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
mysql> exit
Bye 

假如我忘记了密码,现在无法登陆数据库,会提示密码错误。

root@localhost:~# mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) 

1)、首先安全停掉mysql服务

root@localhost:~# /etc/init.d/mysql stop
Shutting down MySQL
.... *

2)、编辑配置文件,在[mysqld]添加skip-grant-tables

(--skip-grant-tables 的意思是启动 MySQL 服务的时候跳过权限表认证)

3)、启动mysql服务

root@localhost:~# /etc/init.d/mysql start
Starting MySQL
..... *

4)、登录数据库并修改密码

root@localhost:~# mysql -uroot
mysql>
mysql> update mysql.user set password=PASSWORD('jeqThs1qOVbHGRz0') where user='root';
Query OK, 2 rows affected (0.00 sec)
Rows matched: 4  Changed: 2  Warnings: 0

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

mysql> exit
Bye

5)、再次编辑配置文件,删除skip-grant-tables

root@localhost:~# vim /etc/mysql/my.cnf

6)、修改之后重启mysql服务

root@localhost:~# /etc/init.d/mysql restart
Shutting down MySQL
.... *
Starting MySQL
..... *

7)、此时就可以用新密码登录啦

root@localhost:~# mysql -uroot -pjeqThs1qOVbHGRz0
mysql>
mysql> exit
Bye

猜你喜欢

转载自www.cnblogs.com/zhaomeng/p/10470197.html