实验前准备:
1.环境
端口3306 该实例是生产环境,欲找回3306的 root密码
测试3307 测试时使用
2.开启 3306,3307 mysqld 服务
[[email protected] ~]# mysqld --defaults-file=/data/mysql/mysql3306/my.cnf & [[email protected] ~]# mysqld --defaults-file=/data/mysql/mysql3307/my.cnf &
两种方法:
一.使用 skip_grant_tables 参数
[[email protected] ~]# ps -ef|grep 3306|grep -v grep mysql 947 365 0 19:00 pts/0 00:00:00 mysqld --defaults-file=/data/mysql/mysql3306/my.cnf [[email protected] ~]# kill -9 947 [[email protected] ~]# mysqld --defaults-file=/data/mysql/mysql3306/my.cnf --skip_grant_tables & [[email protected] ~]# mysql -S /tmp/mysql3306.sock
mysql> alter user user() identified by ‘zstzst‘; ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement mysql> update mysql.user set authentication_string=password(‘zstzst3306‘) where host=‘localhost‘ and user=‘root‘; Query OK, 1 row affected, 1 warning (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 1 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> shutdown; Query OK, 0 rows affected (0.00 sec)
[[email protected] ~]# mysqld --defaults-file=/data/mysql/mysql3306/my.cnf & [[email protected] ~]# mysql -S /tmp/mysql3306.sock -pzstzst3306 #登陆成功
这个方法需要重启MySQL服务,由于免密码登陆,所以再启动数据库时,最好也要加上参数 --skip__networking
二.拷贝修改user.*
3306 老密码是zstzst,假装遗忘,通过拷贝user*文件方式修改成zstzst3306
3307 password‘zstzst3307‘
[[email protected] ~]# mysql -S /tmp/mysql3306.sock -pzstzst3306 mysql> alter user [email protected] identified by ‘zstzst‘; #3306密码修改成 zstzst
[[email protected] ~]# ps -ef|grep mysqld|grep -v grep mysql 1032 365 0 19:00 pts/0 00:00:00 mysqld --defaults-file=/data/mysql/mysql3307/my.cnf mysql 3960 365 0 19:05 pts/0 00:00:00 mysqld --defaults-file=/data/mysql/mysql3306/my.cnf [[email protected] ~]# kill -9 1032 #杀掉3307 [ro[email protected] ~]# \cp /data/mysql/mysql3306/data/mysql/user.frm /data/mysql/mysql3307/data/mysql/user.frm [[email protected] ~]# \cp /data/mysql/mysql3306/data/mysql/user.MYD /data/mysql/mysql3307/data/mysql/user.MYD [[email protected] ~]# \cp /data/mysql/mysql3306/data/mysql/user.MYI /data/mysql/mysql3307/data/mysql/user.MYI [[email protected] ~]# mysqld --defaults-file=/data/mysql/mysql3307/my.cnf --skip_grant_tables & #这样不用重启生产库 [[email protected] ~]# mysql -S /tmp/mysql3307.sock
mysql> update mysql.user set authentication_string=password(‘zstzst3306‘) where host=‘localhost‘ and user=‘root‘; Query OK, 0 rows affected, 1 warning (0.00 sec) Rows matched: 1 Changed: 0 Warnings: 1 mysql> flush privileges; Query OK, 0 rows affected (0.01 sec) mysql> shutdown; Query OK, 0 rows affected (0.01 sec)
[[email protected] ~]# \cp /data/mysql/mysql3307/data/mysql/user.frm /data/mysql/mysql3306/data/mysql/user.frm [[email protected] ~]# \cp /data/mysql/mysql3307/data/mysql/user.MYD /data/mysql/mysql3306/data/mysql/user.MYD [[email protected] ~]# \cp /data/mysql/mysql3307/data/mysql/user.MYI /data/mysql/mysql3306/data/mysql/user.MYI [[email protected] ~]# kill -HUP 3960 [[email protected] ~]# mysql -S /tmp/mysql3306.sock -pzstzst3306 #验证登陆成功
原文地址:https://www.cnblogs.com/bkwong/p/9264973.html
时间: 2024-10-09 22:14:19