登錄MySQL,密碼輸入錯誤
/* 密碼錯誤,報如下錯誤 */[root@TESTDB ~]# mysql -uroot -p -P3306 Enter password: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
如果忘記密碼,對於MySQL而言處理起來也相對比較簡單。但需要修改配置,重啟資料庫。可以按照如下步驟處理。
1. 修改資料庫配置文件
vim /etc/my.cnf--添加如下參數skip_grant_tables
2. 重啟資料庫
如果部署了服務 可以重啟資料庫服務重啟,如果沒有部署,需要殺掉資料庫進程,在重新啟動資料庫。
/*重啟資料庫服務 *//etc/init.d/mysqld restart或 ps -ef|grep mysql /* 查出MySQL 的進程號,下一步中使用 */kill 30516 29246 /*不建議使用 kill -9 */
3. 登錄資料庫修改密碼
/*此時可以直接登錄資料庫 無需輸入密碼 */[root@TESTDB ~]# mysql -uroot -P3306Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 4Server version: 5.7.23-24-log Percona Server (GPL), Release 24, Revision 57a9574Copyright (c) 2009-2018 Percona LLC and/or its affiliatesCopyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>
再修改密碼
/* MySQL5.7 中修改密碼 */mysql> update mysql.user set authentication_string=password('123456') where user='root' and host='localhost';Query OK, 0 rows affected, 1 warning (0.02 sec)Rows matched: 1 Changed: 0 Warnings: 1mysql> flush privileges;Query OK, 0 rows affected (0.06 sec)
註:
a) 不可以使用set password命令修改密碼,只能通過更新資料庫表的方式
mysql> set password=password('123456');ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
b) 使用update表mysql.user的方式需要flush privileges生效
c) 不同的版本mysql.user的欄位以及密碼加密方式不同,例如MySQL5. 6 中密碼存儲在password中,MySQL8. 0 中加密方式有變更等,處理時需要根據版本來相應修改腳本處理。
4 . 將配置文件還原
去掉第 1 步中my.cnf配置文件中添加的skip_grant_tables參數
vim /etc/my.cnf
#skip_grant_tables /* 注釋掉該參數*/
5. 重啟資料庫
Mysql5. 7 中可以直接在MySQL命令行中使用shutdown命令關閉資料庫,之後再啟動資料庫即可。
mysql> shutdown;Query OK, 0 rows affected (0.00 sec)
啟動後,即可使用重置後的密碼登錄
[root@TESTDB ~]# mysql -uroot -P3306 -p'123456'mysql: [Warning] Using a password on the command line interface can be insecure.Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 3Server version: 5.7.23-24-log Percona Server (GPL), Release 24, Revision 57a9574Copyright (c) 2009-2018 Percona LLC and/or its affiliatesCopyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>
至此,密碼重置完畢。
本文轉載自微信公眾號【資料庫乾貨鋪】。