1. 程式人生 > 其它 >忘記密碼,linux如何修改mysql的密碼?

忘記密碼,linux如何修改mysql的密碼?

假如Mysql忘記密碼,如何修改密碼呢,這時就需要用到mysql的引數配置 skip-grant-tables。

1、需要先停止mysql服務

systemctl stop mysqld.service

檢視mysql啟動狀態

systemctl status mysqld.service

重新啟動mysql

systemctl restart mysqld.service

2、輸入命令:mysqld_safe --skip-grant-tables &

[root@.... bin]# mysqld_safe --skip-grant-tables &  
[1
] 7313 -bash: mysqld_safe: command not found

原因:這是由於系統預設會查詢/usr/bin下的命令,如果這個命令不在這個目錄下,當然會找不到命令,我們需要做的就是對映一個連結到/usr/bin目錄下,相當於建立一個連結檔案。
首先得知道mysql命令或mysqladmin命令的完整路徑,比如mysqld_safe的路徑是:/usr/local/mysql/bin/mysqld_safe,我們則可以這樣執行命令:

ln -s /usr/local/mysql/bin/mysqld_safe /usr/bin

結果如下:

[root@i.. bin]# ln -s /usr/local/mysql/bin/mysqld_safe /usr/bin
[
1]+ Exit 127 mysqld_safe --skip-grant-tables

再次執行:mysqld_safe --skip-grant-tables &

[root@i.. bin]# mysqld_safe --skip-grant-tables &
[1] 7706
[root@izbp13nq1pxf5wl3ueeih3z bin]# 2022-05-23T02:16:05.353293Z mysqld_safe Logging to '/usr/local/mysql/data/izbp13nq1pxf5wl3ueeih3z.err'.
2022
-05-23T02:16:05.417880Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data

3、輸入mysql登入mysql系統:mysql

[root@i.... bin]# mysqld_safe --skip-grant-tables &
[1] 7706
[root@i... bin]# 2022-05-23T02:16:05.353293Z mysqld_safe Logging to '/usr/local/mysql/data/i....err'.
2022-05-23T02:16:05.417880Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 5.7.18 MySQL Community Server (GPL)

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

修改密碼:

update user set authentication_string=password('新密碼') where user='root';

注意:檢視使用者表字段發現沒有password欄位,改為了authentication_string欄位

mysql> update user set authentication_string=password('新密碼') WHERE  user='root';
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column

執行發現報錯。

4、在navicat中修改程式碼

我們改為在navicat中執行如下程式碼:

update user set authentication_string=password('新密碼') where user='root';
flush privileges;

5、重啟mysql進行登入