MySQL數據庫root密碼忘記丟失重置方法

 

對於DBA來講,丟失超管用戶root的密碼是致命的,mysql

能夠經過添加--ship-grant-tables參數來跳過權限表。sql

 

一、忘記root密碼,進不去數據庫:

這個時候須要強制停庫,先查看MySQL進程號數據庫

Kill掉MySQL進程,命令以下:測試

Kill完能夠再查看是否還有進程spa

[root@tse2 tmp]# kill -9 9840 1
[root@tse2 tmp]# ps -ef |grep mysql

建議停庫這種操做不要影響業務的狀況下,停服操做的,還有最好這種密碼記錄好的,能夠用keepass保存密碼軟件!code

而後加跳過權限表參數,重啓數據庫。這樣即便不輸入密碼,也能夠進入數據庫。orm

[root@tse2 bin]# mysqld_safe --defaults-file=/etc/my.cnf --skip-grant-tables &
[1] 4854

直接輸入mysql不須要密碼便可進入blog

給root用戶設置新的密碼並刷新權限,MySQL5.7以後,庫下的password字段用authentication_string字段代替。進程

(product)root@localhost:mysql.sock [(none)]> use mysql;
Database changed
(product)root@localhost:mysql.sock [mysql]> update user set authentication_string=password('123456') where user='root';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 1

(product)root@localhost:mysql.sock [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

設置完成以後,重啓數據庫。注意重啓的時候不須要再加--skip-grant-tables參數了,正常啓動服務,輸入新的密碼能夠正常進入數據庫了。這邊重啓之後測試直接mysql沒有密碼是沒法進入的了。ip

[root@tse2 bin]# mysqld_safe --deaults-file=/etc/my.cnf &
[2] 6720
[root@tse2 bin]# 2020-01-16T02:55:45.223195Z mysqld_safe Logging to '/mysql/mysql3306/logs/error.log'.
2020-01-16T02:55:45.262302Z mysqld_safe A mysqld process already exists

[2]+  Exit 1                  mysqld_safe --deaults-file=/etc/my.cnf
[root@tse2 bin]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

這邊輸入密碼鏈接,便可正常進入,是修改後的密碼123456

[root@tse2 bin]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.23-log MySQL Community Server (GPL)

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

(product)root@localhost:mysql.sock [(none)]> use mysql
Database changed
(product)root@localhost:mysql.sock [mysql]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)
相關文章
相關標籤/搜索