忘记mysql root管理员帐号密码处理方法

点击阅读原文,提升阅读体验: https://www.modb.pro/db/22826?cyn 

摘要:丢失root密码,意味着不能以超级管理员帐号登录数据库,进行维护工作。需要找回root密码。

概述

丢失root密码,意味着不能以超级管理员帐号登录数据库,进行维护工作。需要找回root密码。

处理方法

杀掉mysql进程,然后添加一个参数–skip-grant-tables跳过权限表,启动mysql实例,然后以无密码方式登录数据库,修改root用户密码,再关闭mysql实例(关闭或者kill mysql进程),最后正常启动mysql实例,就可以使用root用户登录数据库了。

关键操作代码

点击阅读原文,提升阅读体验:  https://www.modb.pro/db/22826?cyn 

mysql -uroot -p
ps -ef|grep mysql
kill -9 mysql进程号
mysqld_safe --defaults-file=/etc/my.cnf --skip-grant-tables &
mysql
use mysql;
update user set authentication_string=password(‘oracle123’) where user=‘root’;
flush privileges;
mysqladmin -p shutdown
mysqld_safe --defaults-file=/etc/my.cnf &
mysql -uroot -p

操作演示日志

[root@source ~]# mysql -uroot -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)   ==故意输错密码,模拟root密码丢失==
[root@source ~]# 
[root@source ~]# ps -ef|grep mysql
root       6733   6191  0 10:28 pts/0    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf
mysql      7029   6733  2 11:10 pts/0    00:00:00 /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin 
--user=mysql --log-error=source.err --pid-file=source.pid --port=3306
root       7063   6191  0 11:10 pts/0    00:00:00 grep mysql
[root@source ~]#  
[root@source ~]# kill -9 7029 6733
[root@source ~]# 
[1]+  Killed                  mysqld_safe --defaults-file=/etc/my.cnf
[root@source ~]# 
[root@source ~]# ps -ef|grep mysql
root       7066   6191  0 11:10 pts/0    00:00:00 grep mysql
[root@source ~]# 
[root@source ~]# 
[root@source ~]# mysqld_safe --defaults-file=/etc/my.cnf --skip-grant-tables &
[1] 7286
[root@source ~]# 2020-02-16T03:11:41.998645Z mysqld_safe Logging to '/data/mysql/source.err'.
2020-02-16T03:11:42.020962Z mysqld_safe Starting mysqld daemon with databases from /data/mysql
[root@source ~]# 
[root@source ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 3
Server version: 5.7.20-log MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
root@db 11:11:  [(none)]> 
root@db 11:11:  [(none)]> 
root@db 11:12:  [(none)]> 
root@db 11:12:  [(none)]> use mysql;
Database changed
root@db 11:12:  [mysql]> 
root@db 11:12:  [mysql]> update user set authentication_string=password('oracle123') where user='root'; ==修改密码==
Query OK, 1 row affected, 1 warning (0.01 sec)
Rows matched: 2  Changed: 1  Warnings: 1
root@db 11:13:  [mysql]> 
root@db 11:13:  [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
root@db 11:13:  [mysql]> 
root@db 11:14:  [mysql]> exit
Bye
[root@source ~]# 
[root@source ~]# mysqladmin -p shutdown
Enter password: 
[root@source ~]# 
[root@source ~]# 2020-02-16T03:15:27.217666Z mysqld_safe mysqld from pid file /data/mysql/source.pid ended
[1]+  Done                    mysqld_safe --defaults-file=/etc/my.cnf --skip-grant-tables
[root@source ~]# 
[root@source ~]# ps -ef|grep mysql
root       7502   6191  0 11:15 pts/0    00:00:00 grep mysql
[root@source ~]#  
[root@source ~]# mysqld_safe --defaults-file=/etc/my.cnf &
[1] 7503
[root@source ~]# 2020-02-16T03:15:57.181920Z mysqld_safe Logging to '/data/mysql/source.err'.
2020-02-16T03:15:57.217614Z mysqld_safe Starting mysqld daemon with databases from /data/mysql
[root@source ~]# 
[root@source ~]# mysql -uroot -p
Enter password:          ==root密码找回后,正常登录==
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 4
Server version: 5.7.20-log MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
root@db 11:16:  [(none)]>
原文地址:https://www.cnblogs.com/hzcya1995/p/13311769.html