解決Mysql忘記密碼無法登入問題
阿新 • • 發佈:2018-10-31
問題描述:
在使用mysql -u root -p登入Mysql多次卻因為密碼問題登入失敗時,我們可以手動來重置密碼
解決方案如下:
1: 首先以管理員身份登入
[[email protected]33 bin]#
2: 然後編輯/etc/my.cnf 在最後增加一行skip-grant-tables
[[email protected]33 bin]# vi /etc/my.cnf
...
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
skip-grant-tables
3: 之後再通過mysql -u root -p登入時不需要認證直接Enter即可登入
[ [email protected]33 bin]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.21 MySQL Community Server (GPL)
...
4: 重置密碼,通過更新user表中儲存密碼的欄位,在不同版本的Mysql中儲存密碼的欄位不同,
我的是5.7版本儲存密碼的欄位是authentication_string(之前版本可能是通過password來儲存)
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 authentication_string=password('abc123,') where user='root';
Query OK, 3 rows affected, 1 warning (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 1
5: 退出mysql
mysql> exit ;
Bye
6: 密碼重置之後再將/etc/my.cnf檔案改回之前的狀態,刪除增加的skip-grant-tables
[[email protected]33 bin]# vi /etc/my.cnf
7: 重啟Mysql
[[email protected]33 bin]# service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service
8: 之後可以正常通過 mysql -u root -p加設定的密碼進行登入