小知識:MySQL配置文件優先級


今天在RHEL7上,嚴格按之前的安裝規范文檔,部署MySQL環境時,發現通過服務的方式啟動MySQL失敗:
關鍵錯誤是:

log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.

規范的配置文件是:/etc/mysql/my.cnf,其中也有對應 log-error 參數的值,並不是上面錯誤提示的路徑。
而且為何會有mariadb字樣呢,猜測可能是RHEL7默認配套mariadb的原因,但是查詢安裝相關的rpm包,也未發現有mariadb的服務端。
最后對比發現,RHEL7默認的/etc/my.cnf有默認值,即使mariadb沒有安裝,而這個默認值里配置的 log-error 參數值正好匹配報錯信息。
解決方案有兩個:

  • 1.刪掉/etc/my.cnf配置文件
  • 2.使用規范的參數配置,直接覆蓋/etc/my.cnf配置文件

我這里選擇了第二種方案,成功解決問題。

最后復盤時發現用:

mysql --help|more

可以看到參數文件的順序是如下:

Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf 

也就是說優先級:/etc/my.cnf > /etc/mysql/my.cnf
但我們知道優先級低的配置文件因為最后被讀到,如果有同一參數在不同配置文件中設置有差異,反而優先級低的配置文件,反而應該會覆蓋之前優先級高的配置文件中的對應參數內容。
那么這又是怎么回事呢?

實際上仔細觀察,會發現RHEL7中默認的my.cnf內容如下:

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

實際上log-error的配置是在標簽[mysqld_safe]下,而不是[mysqld]下,而[mysqld_safe]標簽下的內容在之后優先級低的配置文件中並沒有再次設置;
換句話說,如果log-error在各個配置文件中,都是統一配置在[mysqld]下,就可以實現被后面優先級低的用戶配置文件覆蓋。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM