日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
MySQL客戶端對配置文件的讀取順序

?一、場景現(xiàn)象

在本地做測試時,發(fā)現(xiàn)一個奇怪的現(xiàn)象,當我使用 socket 打算登錄數(shù)據(jù)庫,發(fā)現(xiàn)不指定用戶時,默認并不是用的 root 用戶登錄,而是被修改為了 zhenxing 用戶。

創(chuàng)新互聯(lián)公司是專業(yè)的康縣網(wǎng)站建設公司,康縣接單;提供成都網(wǎng)站設計、網(wǎng)站制作、外貿營銷網(wǎng)站建設,網(wǎng)頁設計,網(wǎng)站設計,建網(wǎng)站,PHP網(wǎng)站建設等專業(yè)做網(wǎng)站服務;采用PHP框架,可快速的進行康縣網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!

[root@10-186-61-162 ~]# mysql -S /data/mysql/3306/data/mysqld.sock -p
Enter password:
ERROR 1045 (28000): Access denied for user 'zhenxing'@'127.0.0.1' (using password: NO)

通過觀測當前 mysql 客戶端的默認參數(shù)行為,可以看到與報錯一致,默認用戶確實是變?yōu)榱?zhenxing。

[root@10-186-61-162 ~]# mysql --help|egrep "user|host|port"
-h, --host=name Connect to host.
-P, --port=# Port number to use for connection or 0 for default to, in
-u, --user=name User for login if not current user.
host 127.0.0.1
port 3306
user zhenxing

在這里作為 DBA ,我們的第一反應是肯定是查看 /etc/my.cnf 文件中是否對默認用戶做了配置,于是查看該配置文件的客戶端配置參數(shù),如下

[client]
host = 127.0.0.1
user = root
port = 3306

[mysql]
host = 127.0.0.1
user = root
port = 3306
prompt = '\U[\d]> '

發(fā)現(xiàn)配置文件中的反而配置值是 root 用戶,并沒有對 zhenxing 用戶做配置,看來讀取的還不是這個配置文件,那是不是讀取了其他配置文件呢,繼續(xù)排查其他的配置文件。

二、排查思路

1、獲取配置文件讀取順序?

我們先打印出所有可能讀取的配置文件及其讀取的順序做逐個排查。

## 查看mysql客戶端讀取配置文件的順序
[root@10-186-61-162 ~]# mysql --verbose --help|grep my.cnf
order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf /data/mysql/3306/base/my.cnf ~/.my.cnf

2、排查 /etc/my.cnf?

/etc/my.cnf 在前面已經(jīng)確認沒有做相關配置,這里直接跳過。

3、排查 /etc/mysql/my.cnf?

查看 /etc/mysql/my.cnf 配置,發(fā)現(xiàn)不存在相關配置,排除。

[root@10-186-61-162 ~]# cat /etc/mysql/my.cnf
cat: /etc/mysql/my.cnf: 沒有那個文件或目錄

4、排查 /usr/local/mysql/etc/my.cnf?

查看 /usr/local/mysql/etc/my.cnf 配置,發(fā)現(xiàn)不存在相關配置,排除。

[root@10-186-61-162 ~]# cat /usr/local/mysql/etc/my.cnf
cat: /usr/local/mysql/etc/my.cnf: 沒有那個文件或目錄

5、排查 /data/mysql/3306/base/my.cnf?

查看 /data/mysql/3306/base/my.cnf 配置,發(fā)現(xiàn)不存在相關配置,排除。

[root@10-186-61-162 ~]# cat /data/mysql/3306/base/my.cnf
cat: /data/mysql/3306/base/my.cnf: 沒有那個文件或目錄

6、排查 ~/.my.cnf?

查看 ~/.my.cnf 依舊不存在相關配置,排除。

[root@10-186-61-162 ~]# cat ~/.my.cnf
cat: /root/.my.cnf: 沒有那個文件或目錄

至此按照 mysql --verbose --help|grep my.cnf 獲取的配置文件讀取路徑都被排除,都未對用戶zhenxing做配置。

7、使用no-defaults排除配置文件干擾?

嘗試用 --no-defaults 不讀取任何配置文件排除配置文件的干擾,看是否會恢復正常。

[root@10-186-61-162 ~]# mysql --help|grep no-defaults
--no-defaults Don't read default options from any option file

## 查看不讀取配置文件時,客戶端的默認值
[root@10-186-61-162 ~]# mysql --no-defaults --help|egrep "user|host|port"
-h, --host=name Connect to host.
-P, --port=# Port number to use for connection or 0 for default to, in
-u, --user=name User for login if not current user.
host 127.0.0.1
port 3306
user zhenxing

## 查看讀取的所有客戶端配置文件參數(shù)設置
[root@10-186-61-162 ~]# mysql --print-defaults
mysql would have been started with the following arguments:
--host=127.0.0.1 --user=root --port=3306 --host=127.0.0.1 --user=root --port=3306 --prompt=\U[\d]> --user=zhenxing --password=***** --host=127.0.0.1 --port=3306

從上面輸出的結果來看,我們可以得到以下2個基本現(xiàn)象:

  • 即使指定--no-defaults不讀取任何配置文件,這個 user 的默認值依舊是 zhenxing 用戶
  • 當輸出--print-defaults獲取實際運行值時,可以看到 /etc/my.cnf 下的[client] ,和 [mysql] 標簽下的屬性配置從上到下被正確獲取
  • 除了 /etc/my.cnf 外,在最后還有--user=zhenxing --password=***** --host=127.0.0.1 --port=3306這4個參數(shù)被額外添加到了命令最后

ps:mysql 客戶端和服務端讀取配置的原則都是文件讀取從上到下,后面相同參數(shù)配置覆蓋前面的參數(shù)經(jīng)過一系列的排除,依舊沒找到這個默認值被修改的源頭

8、打印 mysql 客戶端的系統(tǒng)調用?

  • 使用 strace 直接觀測 mysql 客戶端在執(zhí)行時到底調用了哪些配置,以下是調用 my.cnf 相關配置的片段(對結果做了精簡輸出)
1. stat("/etc/my.cnf", {st_mode=S_IFREG|0644, st_size=195, ...}) = 0      = 3
2. stat("/etc/mysql/my.cnf", 0x7ffd56813180) = -1 ENOENT (No such file or directory)
3. stat("/usr/local/mysql/etc/my.cnf", 0x7ffd56813180) = -1 ENOENT (No such file or directory)
4. stat("/data/mysql/3306/base/my.cnf", 0x7ffd56813180) = -1 ENOENT (No such file or directory)
5. stat("/root/.my.cnf", 0x7ffd56813180) = -1 ENOENT (No such file or directory)
6. stat("/root/.mylogin.cnf", {st_mode=S_IFREG|0600, st_size=336, ...}) = 0

通過以上調用順序可以可到以下結論:

  • 1-5行的調用順序與我們驗證的邏輯基本一致
  • 2-5行顯示為 No such file or directory 與我們的驗證結果一致
  • 第6行輸出,增加了一個對 /root/.mylogin.cnf 的讀取操作,并且可以知道當前這個文件是確實存在的

9、排查 /root/.mylogin.cnf?

看到這個文件我們一般都知道,這個是 mysql_config_editor 工具用來配置 login-path 的生成的文件,我們可以用以下方式查看當前的配置信息。

[root@10-186-61-162 ~]# mysql_config_editor print --all
[client]
user = "zhenxing"
password = *****
host = "127.0.0.1"
port = 3306

這里可以看到配置中有一個 client? 標簽的連接參數(shù)配置,配置的內容正好是我們文章開頭顯示的異常默認值,到這我們基本定位了造成這個奇怪現(xiàn)象的原因,出現(xiàn)這個故障場景的原因也是剛好這臺是測試環(huán)境曾經(jīng)做過一些 mysql_config_editor 用法的測試,導致了該現(xiàn)象的發(fā)生

三、場景總結

1、mysql客戶端除了會按照命令 mysql --verbose --help|grep my.cnf?輸出的常規(guī)的順序讀取配置外,在最后還會額外的讀取 .mylogin.cnf 文件中配置。

2、即使指定了 --no-defaults ,依舊會去讀取 .mylogin.cnf 中的 [client] ,[mysql] 標簽的配置值。

其中官方文檔也在以下鏈接中給到了明確的說明(以下是關鍵描述片段)。

  • https://dev.mysql.com/doc/refman/8.0/en/option-file-options.html
The mysql client reads [client] and [mysql] from other option files, and [client], [mysql], and [mypath] from the login path file.

Client programs read the login path file even when the --no-defaults option is used. This permits passwords to be specified in a safer way than on the command line even if --no-defaults is present.
  • 個人猜測當時這么設計的目的是考慮如備份腳本等需要連接數(shù)據(jù)庫時,為了防止非必要的參數(shù)文件的干擾,在指定了 --no-defaults參數(shù)后依舊能使用到 .mylogin.cnf 文件中配置的加密密碼,提升安全性。?

網(wǎng)頁標題:MySQL客戶端對配置文件的讀取順序
當前路徑:http://www.5511xx.com/article/ccegjgc.html