Centos7 with MYSQL8 root 密碼的置換

CentOS7 重置 MySQL 8.0 密碼

 

安裝完成後 MySQL會給我們自動生成一個隨機密碼

檢視命令如下:

grep 'temporary password' /var/log/mysqld.log

因為自動的生成的密碼無法直接使用,也不便於我們記憶,所以我們要修改密碼。

配置檔案 MySQL 免密碼登入

編輯 MySQL 的配置檔案

vim /etc/my.cnf

在 pid 開頭的下面一行加入下面這句

skip-grant-tables

儲存並退出。

重啟 MySQL 服務

service mysqld restart 

免密碼登入到 MySQL 上

mysql -u root -p

 

選擇 mysql 資料庫

use mysql;

使用者的 user 表

mysql 資料庫的 user 中檢查現在 root 資訊

select host, user, authentication_string, plugin from user; 

 

表格中有以下資訊:

host: 允許使用者登入的 ip ‘位置’ % 表示可以遠端;

user: 當前資料庫的使用者名稱;

authentication_string: 使用者密碼(在mysql 5.7.9以後廢棄了password欄位和password()函式);

plugin: 密碼加密方式;

將預設的 root 密碼置空

use mysql;  
update user set authentication_string='' where user='root'; 

8.退出 mysql命令列

quit

9.刪除 /etc/my.cnf 檔案最後的 skip-grant-tables

vim /etc/my.cnf

刪除 skip-grant-tables 並儲存退出。

10.重啟 MySQL 服務

service mysqld restart 

11.重新登入到 MySQL 上

mysql -u root -p

提示輸入密碼時直接敲回車,因為我們剛才已經將密碼置為空了。

12.使用 ALTER 修改 root 使用者密碼

ALTER user 'root'@'localhost' IDENTIFIED BY 'your password';

 

如果

如果

如果

到這出現  skip-grant-tables的錯誤訊息.

 

mysql> flush privileges;

Query OK, 0 rows affected (0.01 sec)

 

mysql> grant all on *.* to root@'localhost' identified by 'yourpassword';   //居然兩個數字也可以.

Query OK, 0 rows affected (0.00 sec)

 

 

 

 

 

← 回列表