解决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加设置的密码进行登入
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值