自建mysql丢失了root密码,如果要找回root密码,传统的做法是需要停止mysql的。大致的做法如下:
停止mysql进程,mysqladmin shutdown;
修改my.cnf或者在启动加上选项,skip-grant-tables,跳过mysql启动时权限表的加载;
登录mysql,修改user表。update mysql.user set password=password('xxx') where user='root' and host='xxx';
修改my.cnf或者启动选项去除skip-grant-tables,重启mysql即可重新登录。
不重启mysql的前提下,如何找回root密码呢?大致原理如下:
新启动一个mysql实例B,B上面没有innodb表;
需要找回密码的mysql实例A,拷贝A的user.[frm|MYD|MYI]到实例B;
修改B上面的密码,并将user.[frm|MYD|MYI]拷贝回A
需要找回root密码的mysql实例为A,端口20122。临时的新mysql实例为B,端口20125。
具体操作流程如下:
启动实例B,在实例B的my.cnf上增加配置并重启。此步骤不是必须,但需要保证mysql.user是myisam引擎。my.cnf配置及查看引擎结果如下
1 skip-innodb2 default-storage-engine=myisam
1 mysql> useinformation_schema2 mysql> select table_name,engine from tables where table_name='user';3 +------------+--------+
4 | table_name | engine |
5 +------------+--------+
6 | user | MyISAM |
7 +------------+--------+
8 1 row in set (0.00 sec)
通过客户端,先连接到实例B。拷贝实例A的user.[frm|MYD|MYI],覆盖实例B的user.[frm|MYD|MYI]
1 cp data/20122/mysql/user.* data/20125/mysql/
在实例B上修改root密码。flush tables将新的权限表加载到内存,修改完毕后flush privileges将更新的权限表回写磁盘。
1 mysql> usemysql;2 mysql> select User,Host,Password from user where User='root' and Host='localhost'\G3 *************************** 1. row ***************************
4 User: root5 Host: localhost6 Password: *3C316168860E5204C4A2AEDDF36D13E99D80A9817 1 row in set (0.00sec)8
9 mysql> update user set Password=password('1234') where User='root' and Host='localhost';10 Query OK, 1 row affected (0.00sec)11 Rows matched: 1 Changed: 1 Warnings: 0
12
13 mysql> select User,Host,Password from user where User='root' and Host='localhost'\G14 *************************** 1. row ***************************
15 User: root16 Host: localhost17 Password: *A4B6157319038724E3560894F7F932C8886EBFCF18 1 row in set (0.00sec)19
20 mysql> flush privileges;21 Query OK, 0 rows affected (0.00 sec)
将实例B的user.[frm|MYD|MYI]拷贝回实例A
1 cp data/20125/mysql/user.* data/20122/mysql/
实例A磁盘上的user权限表已经更新,但还需要重新加载到内存中。因为不能登录实例A执行flush操作,所以需要想实例A发送SIGHUP信号。发送SIGHUP信号,mysql会进行flush privileges、flush logs、刷新线程缓存等操作。如此间接将磁盘权限数据重新加载到内存
1 #查找mysqld进程的pid,并发送SIGHUP信号2 kill -1 $(/sbin/pidof mysqld)
重新登录实例A,通过修改后的密码即可
1 [root@TENCENT64 /data1/mysql_root]# mysql --socket=/tmp/mysql_20122.sock -uroot -p12342 Welcome to the MySQL monitor. Commands end with ; or \g.3 Your MySQL connection id is 2188
4 Server version: 5.1.54-log MySQL Customize Server (GPL)5
6 Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.7
8 Oracle is a registered trademark of Oracle Corporation and/or its9 affiliates. Other names may be trademarks of their respective10 owners.11
12 Type 'help;' or '\h' for help. Type '\c' to clearthe current input statement.13
14 mysql>
注意点:
系统权限表mysql.user必须是myisam引擎的。因为innodb引擎的表,不管是独立表空间还是共享表空间,部分数据都是共享在ibdata*文件里面。mysql官方文档说明,5.6.21及以后版本,使用skip-innodb选项将会产生warning。并且后续的mysql发行版会去除该选项。
在拷贝实例A的user.[frm|MYD|MYI]文件到实例B,到拷贝回实例A的过程中,实例A不能对表mysql.user操作。例如增加/删除权限,否则新修改的权限会被覆盖而丢失。
来源引用:http://www.percona.com/blog/2014/12/10/recover-mysql-root-password-without-restarting-mysql-no-downtime/