萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mysql教程 >> MySQL忘記root密碼不重啟mysqd重置root密碼

MySQL忘記root密碼不重啟mysqd重置root密碼

MySQL忘記root密碼一般正常情況我們都要修改然後重啟服務器了,今天我們來看一篇不重啟mysqd重置root密碼的教程.

找回丟失mysql root密碼
一、啟動修改丟失的mysql單實例root密碼方法;
1.首先停止mysql
/etc/init.d/mysql stop
2.使用--skip-grant-tables啟動mysql,忽略授權登錄驗證
mysqld_safe --skip-grant-tables --user=mysql & --提示:在啟動時加--skip-grant-tables 參數,表示忽略授權驗證
3.進入數據庫系統
shell>mysql
4.修改mysqlroot密碼:update
mysql>update mysql.user set password=password("123456") where user='root' and host='localhost';
mysql>flush privileges;
shell>mysqladmin -uroot -p123456 shutdown
5.重新啟動mysql
shell>/etc/init.d/mysql start
shell>mysql -uroot -p123456
二、多實例丟失密碼的方法:
1.關閉mysql
mysqld_mulit stop
2.啟動時加--skip-grant-tables參數
mysqld_safe --defaults-files=/data/mysql/mysql3377/mysql3377.cnf --skip-grant-tables &
mysql -uroot -p -S /tmp/mysql3377.sock <==登錄時空密碼
3.修改密碼方法:
update mysql.user set password=password("123456") where user='root'
flush privileges;
4.重啟服務用新密碼登錄:
killall mysqld
mysqld_mulit restart 3377


三、不重啟mysqld的方法

1、首先得有一個可以擁有修改權限的mysql數據庫賬號,當前的mysql實例賬號(較低權限的賬號,比如可以修改test數據庫)或者其他相同版本實例的賬號。把data/mysql目錄下面的user表相關的文件復制到data/test目錄下面。

[root@localhost mysql]# cp mysql/user.* test/
[root@localhost mysql]# chown mysql.mysql test/user.*
2、使用另一個較低權限的賬號鏈接數據庫,設置test數據庫中的user存儲的密碼數據。


[root@localhost mysql]# mysql -utest -p12345
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.5.25a-log Source distribution

Copyright (c) 2000, 2011, 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> use test
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 password=password('yayun') where user='root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 5  Changed: 0  Warnings: 0

mysql>

3、把修改後的user.MYD和user.MYI復制到mysql目錄下,記得備份之前的文件。

mv mysql/user.MYD mysql/user.MYD.bak
mv mysql/user.MYI mysql/user.MYI.bak
cp test/user.MY* mysql/
chown mysql.mysql mysql/user.*
4、查找mysql進程號,並且發送SIGHUP信號,重新加載權限表。

[root@localhost mysql]# pgrep -n mysql
2184
[root@localhost mysql]#
[root@localhost mysql]# kill -SIGHUP 2184
5.登陸測試


[root@localhost mysql]# mysql -uroot -pyayun
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 5.5.25a-log Source distribution

Copyright (c) 2000, 2011, 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>

copyright © 萬盛學電腦網 all rights reserved