找回丟失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>