Linux下mysql忘記root用戶密碼
今天在接收一台MYSQL服務器的時候發現忘記MYSQL的root用戶的密碼,查找資料發了各種文檔裡面也沒有root用戶密碼,因此需要修改root用戶密碼。
首先確認服務器出於安全的狀態,也就是沒有人能夠任意地連接MySQL數據庫。因為在重新設置MySQL的root密碼的期間,MySQL數據庫完全出於沒有密碼保護的狀態下,其他的用戶也可以任意地登錄和修改MySQL的信息。最安全的狀態是到服務器的Console上面操作,並且拔掉網線。
1、修改MySQL的登錄設置:
在[mysqld]的段中加上的skip-grant-tables
# sed -i '/\[mysqld\]/a\skip-grant-tables ' /etc/my.cnf
2、重新啟動mysqld
# service mysqld restart
Shutting down MySQL. SUCCESS!
Starting MySQL. SUCCESS!
3、登錄並修改MySQL的root密碼
# /usr/bin/mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.19 Source distribution
Copyright (c) 2000, 2014, 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.
mysql> USE mysql ;
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 ( 'doiido' ) WHERE User = 'root' ;
Query OK, 3 rows affected (0.01 sec)
Rows matched: 3 Changed: 3 Warnings: 0
mysql> flush privileges ;
Query OK, 0 rows affected (0.01 sec)
mysql> quit
Bye
4、將MySQL的登錄設置修改回來
將剛才在[mysqld]的段中加上的skip-grant-tables刪除
# sed -i "/skip-grant-tables/d" /etc/my.cnf
5、重新啟動mysqld
# service mysqld restart
Shutting down MySQL. SUCCESS!
Starting MySQL. SUCCESS!
這個時候,就可以使用root/doiido進行登錄了