解决Mysql忘记密码无法登入问题

问题描述:  
    在使用mysql -u root -p登入Mysql多次却因为密码问题登录失败时,我们可以手动来重置密码   
解决方案如下:
1: 首先以管理员身份登录  
[root@node-33 bin]#

2: 然后编辑/etc/my.cnf 在最后增加一行skip-grant-tables
[root@node-33 bin]# vi /etc/my.cnf
...
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
skip-grant-tables

3: 之后再通过mysql -u root -p登录时不需要认证直接Enter即可登入
[root@node-33 bin]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.21 MySQL Community Server (GPL)
...

4: 重置密码,通过更新user表中保存密码的字段,在不同版本的Mysql中保存密码的字段不同,
我的是5.7版本保存密码的字段是authentication_string(之前版本可能是通过password来保存)
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
mysql> update user set authentication_string=password('abc123,') where user='root';
Query OK, 3 rows affected, 1 warning (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 1

5: 退出mysql
mysql> exit;
Bye

6: 密码重置之后再将/etc/my.cnf文件改回之前的状态,删除增加的skip-grant-tables
[root@node-33 bin]# vi /etc/my.cnf

7: 重启Mysql
[root@node-33 bin]# service mysqld restart
Redirecting to /bin/systemctl restart  mysqld.service

8: 之后可以正常通过 mysql -u root -p加设置的密码进行登入

猜你喜欢

转载自blog.csdn.net/Noonebirdyou/article/details/79996338
今日推荐