以MySQL多实例为例,演示找回MySQL root的密码
1、关闭mysql服务
[root@mysql ~]# mysqladmin -uroot -poldboy123 -S /data/3306/mysql.sock shutdown ==>通过mysqladmin shutdown优雅关闭mysql服务。
[root@mysql ~]# lsof -i:3306
2、使用--skip-grant-tables参数后台启动mysql,忽略授权验证直接登录
[root@mysql ~]# mysqld_safe --defaults-file=/data/3306/my.cnf --skip-grant-tables & ==>--skip-grant-tables这个参数一定要放在默认文件的后面,&表示后台启动。
[1] 63540
[root@mysql ~]# 160216 22:27:26 mysqld_safe Logging to '/data/3306/mysql_oldboy3306.err'.
160216 22:27:26 mysqld_safe Starting mysqld daemon with databases from /data/3306/data
^C
[root@mysql ~]# lsof -i:3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 64277 mysql 12u IPv4 127392 0t0 TCP *:mysql (LISTEN)
3、登录mysql,使用update语句修改MySQL root密码
[root@mysql ~]# mysql -S /data/3306/mysql.sock ==>做了忽略授权认证,不用密码直接登录;
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.32-log Source distribution
Copyright (c) 2000, 2013, 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.
mysql> update mysql.user set password=PASSWORD("oldboy123") where user='root' and host='localhost'; ==>使用update语句修改MySQL root密码。
Query OK, 0 rows affected (0.26 sec)
Rows matched: 1 Changed: 0 Warnings: 0
mysql> flush privileges; ==>一定要刷新授权表。
Query OK, 0 rows affected (0.05 sec)
mysql> \q
Bye
4、使用修改后的root密码登录mysql
[root@mysql ~]# mysql -uroot -poldboy123 -S /data/3306/mysql.sock ==>使用root密码登录。
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.5.32-log Source distribution
Copyright (c) 2000, 2013, 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.
mysql>
至此,找回并修改root密码成功。