首頁  >  文章  >  資料庫  >  mysql重置root密碼

mysql重置root密碼

王林
王林轉載
2020-01-22 19:26:032423瀏覽

mysql重置root密碼

對於資料庫來說,如果忘記root使用者的密碼,那將是非常致命的,但是我們可以透過新增--ship-grant-tables參數來跳過權限表。

具體方法如下:

忘記root密碼,進不去資料庫:

mysql重置root密碼

這個時候需要強制停止庫,先查看MySQL進程號

mysql重置root密碼

Kill掉MySQL進程,指令如下:

Kill完可以再查看是否還有進程

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

mysql重置root密碼

建議停庫這種操作不要影響業務的情況下,停服操作的,還有最好這種密碼記錄好的,可以用keepass保存密碼軟體!

免費學習影片教學分享:mysql影片教學

然後加上跳過權限表參數,重新啟動資料庫。這樣即使不輸入密碼,也可以進入資料庫。

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

mysql重置root密碼

直接輸入mysql不需要密碼即可進入

mysql重置root密碼

#給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沒有密碼是無法進入的了。

[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

mysql重置root密碼

[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                |
+--------------------+
rows in set (0.01 sec)

相關文章教學推薦:mysql教學

以上是mysql重置root密碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:cnblogs.com。如有侵權,請聯絡admin@php.cn刪除