技術分享 | MySQL 設定管理員密碼無法生效一例

語言: CN / TW / HK

作者:楊濤濤

資深資料庫專家,專研 MySQL 十餘年。擅長 MySQL、PostgreSQL、MongoDB 等開源資料庫相關的備份恢復、SQL 調優、監控運維、高可用架構設計等。目前任職於愛可生,為各大運營商及銀行金融企業提供 MySQL 相關技術支援、MySQL 相關課程培訓等工作。

本文來源:原創投稿

* 愛可生開源社群出品,原創內容未經授權不得隨意使用,轉載請聯絡小編並註明來源。

昨天某位客戶向我諮詢這樣一個問題:他通過本地 MySQL 命令列連線資料庫發現管理員不需要驗證密碼即可進行後續操作。為了查明原因,他嘗試過修改管理員密碼,依然無效。為了對比,他還特意建立了一個帶密碼的新使用者,通過 MySQL 命令列可以正常進行密碼驗證。

經過對他遇到的問題做了詳細瞭解後,我大概知道問題出在哪,不過還需要繼續驗證。

此類問題大致會有如下幾種原因:

  1. 此使用者本身並沒有設定密碼。

  2. 配置檔案裡開啟 skip-grant-tables 跳過授權表。

  3. 配置檔案裡有明文 password 選項來跳過密碼。

  4. 使用者的認證外掛有可能使用 auth_socket 。

我先來大致復現下這個問題。現象如下:MySQL 命令列客戶端列印“hello world ”不需要驗證密碼。

root@ytt-large:/home/ytt# mysql -e "select 'hello world'"
+-------------+
| hello world |
+-------------+
| hello world |
+-------------+

換個使用者就必需驗證密碼後方可正常列印字串:

root@ytt-large:/home/ytt# mysql -uadmin -e "select 'hello world'"
ERROR 1045 (28000): Access denied for user 'admin'@'localhost' (using password: NO)

root@ytt-large:/home/ytt# mysql -uadmin -p -e "select 'hello world'"
Enter password: 
+-------------+
| hello world |
+-------------+
| hello world |
+-------------+

嘗試修改管理員密碼,依然不需要驗證密碼即可執行命令:看結果好像是修改密碼無效。

root@ytt-large:~# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 35
Server version: 8.0.29 MySQL Community Server - GPL

...

mysql> alter user root@localhost identified by 'root';
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye
root@ytt-large:/home/ytt# mysql -e "select 'hello world'"
+-------------+
| hello world |
+-------------+
| hello world |
+-------------+

那接下來基於我復現的場景以及我開頭想到的可能原因來逐步判斷到底問題出在哪裡。

  1. 此使用者本身並沒有設定密碼。

這個原因可以快速排除掉!已經執行過一次 alter user 改密碼的操作,所以不可能沒有密碼。

  1. 配置檔案裡開啟 skip-grant-tables 跳過授權表。

這個原因也可以快速排除掉!如果是因為開啟這個選項,那必定所有使用者都不會驗證密碼,而不只是針對管理員賬號本身。

  1. 配置檔案裡有明文 password 選項來跳過密碼。

有可能是這個原因。可以用工具 my_print_defaults 來列印相關配置、或者直接手動檢查配置檔案有沒有[client] 、[mysql] 等段裡包含有 password 明文選項。例如:

root@ytt-large:/home/ytt# my_print_defaults /etc/mysql/my.cnf client mysql
--password=*****

結果確實是設定了 password 選項,但是仔細想想,有點站不住腳。如果是因為這個原因,那修改密碼後,為什麼依然不驗證新密碼?因此這個可能性也被排除掉。

  1. 使用者的認證外掛有可能使用 auth_socket 。

極有可能是這個原因!

外掛 auth_socket MySQL 官網全稱為:Socket Peer-Credential Pluggable Authentication(套接字對等憑據可插拔的身份驗證)。

官方文件地址:http://dev.mysql.com/doc/refman/8.0/en/socket-pluggable-authentication.html

閱讀官方文件後可以得出的結論為外掛 auth_socket 不需要驗證密碼即可進行本地認證!它有兩個認證條件:

  1. 客戶端通過本地 unix socket 檔案連線 MySQL 服務端。

  2. 通過 socket 的選項 SO_PEERCRED 來獲取執行客戶端的 OS 使用者名稱,隨後判斷 OS 使用者名稱是否在 mysql.user 表裡。

另外,想了解更多關於 socket 的選項 SO_PEERCRED 可以參考這個網址:http://man7.org/linux/man-pages/man7/unix.7.html

那我們接下來驗證結論是否正確。檢視當前登入使用者是不是 root@localhost :確認無疑。

root@ytt-large:/home/ytt# mysql  -e "select user(),current_user()"
   +----------------+----------------+
   | user()         | current_user() |
   +----------------+----------------+
   | root@localhost | root@localhost |
   +----------------+----------------+

檢查 mysql.user 表記錄:檢查欄位 plugin、authentication_string(此欄位有可能不為空)。

mysql> select plugin,authentication_string from mysql.user where user = 'root' ;
   +-------------+-----------------------+
   | plugin      | authentication_string |
   +-------------+-----------------------+
   | auth_socket |                       |
   +-------------+-----------------------+
   1 row in set (0.01 sec)

確認管理員賬號外掛為 auth_socket ,難怪改密碼無效。接下來把外掛改為非 auth_socket 即可。

mysql> alter user root@localhost identified with mysql_native_password by 'root';
   Query OK, 0 rows affected (0.04 sec)

再次執行 MySQL 命令列:無密碼正常報錯,輸入正確密碼後執行成功。

root@ytt-large:/home/ytt# mysql -p -e "select 'hello world'"
   ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
   root@ytt-large:/home/ytt# mysql -proot -e "select 'hello world'"
   mysql: [Warning] Using a password on the command line interface can be insecure.
   +-------------+
   | hello world |
   +-------------+
   | hello world |
   +-------------+

結語:

一般在遇到 MySQL 問題時,建議對 MySQL 系統函式、資料庫內部物件等進行檢索而不是直接列印字串,有時候可能對快速定位問題原因有幫助。

本文關鍵字 #MySQL 認證外掛 # #MySQL 日常問題#