【問題處理】MySQL忘記root密碼的處理辦法

恩強Boy發表於2021-01-05

MySQL 忘記 root 密碼的處理辦法

1.  編輯/etc/my.cnf 檔案

1) 在[mysqld] 下新增以下內容:

skip-grant-tables

2) 重啟MySQL 服務

# service mysqld restart

Shutting down MySQL.. SUCCESS!

Starting MySQL. SUCCESS!

2.  直接進入mysql

# mysql

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.7.25 MySQL Community Server (GPL)

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

mysql>

3.  修改回密碼

1)  檢視密碼

mysql> select user,host,authentication_string from mysql.user;

+---------------+-----------+-------------------------------------------+

| user           | host         | authentication_string                      |

+---------------+-----------+-------------------------------------------+

| root           | localhost | *8232A1298A49F710DBEE0B330C42EEC825D4190A |

| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |

| mysql.sys       | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |

+---------------+-----------+-------------------------------------------+

3 rows in set (0.00 sec)

2)  修改密碼

情況一:mysql 5.7.6 or later 版本

mysql> update mysql.user set authentication_string=password('P@ssw0rd')   where user='root';

情況二:mysql 5.7.5 or earlier 版本

mysql> update mysql.user set password=password("P@ssw0rd") where user="root" and host="localhost";

4.  修改配置檔案,重啟MySQL

1)  修改my.cnf 檔案

刪除或註釋skip-grant-tables

2)  重啟mysql

# service mysqld restart

Shutting down MySQL.. SUCCESS!

Starting MySQL. SUCCESS!

3)  新密碼驗證

# mysql -uroot -p'P@ssw0rd'

 

---- end ----


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31529886/viewspace-2747684/,如需轉載,請註明出處,否則將追究法律責任。

相關文章