1. 程式人生 > >mysql 檢視當前使用的配置檔案my.cnf的方法

mysql 檢視當前使用的配置檔案my.cnf的方法

my.cnf是mysql啟動時載入的配置檔案,一般會放在mysql的安裝目錄中,使用者也可以放在其他目錄載入。

安裝mysql後,系統中會有多個my.cnf檔案,有些是用於測試的。

使用locate my.cnf命令可以列出所有的my.cnf檔案

命令
locate my.cnf

輸出
/usr/local/Cellar/mysql/5.6.24/my.cnf
/usr/local/Cellar/mysql/5.6.24/mysql-test/include/default_my.cnf
/usr/local/Cellar/mysql/5.6.24/mysql-test/suite/federated/my.cnf
/usr/local
/Cellar/mysql/5.6.24/mysql-test/suite/ndb/my.cnf /usr/local/Cellar/mysql/5.6.24/mysql-test/suite/ndb_big/my.cnf /usr/local/Cellar/mysql/5.6.24/mysql-test/suite/ndb_binlog/my.cnf /usr/local/Cellar/mysql/5.6.24/mysql-test/suite/ndb_rpl/my.cnf /usr/local/Cellar/mysql/5.6.24/mysql-test/suite/ndb_team/my.cnf /usr/local/Cellar/mysql/5.6.24/mysql-test/suite/rpl/extension/bhs/my
.cnf /usr/local/Cellar/mysql/5.6.24/mysql-test/suite/rpl/my.cnf /usr/local/Cellar/mysql/5.6.24/mysql-test/suite/rpl_ndb/my.cnf

當我們需要修改配置檔案時,需要找到mysql啟動時是載入了哪個my.cnf檔案。

1.檢視是否使用了指定目錄的my.cnf

啟動mysql後,我們檢視mysql的程序,看看是否有設定使用指定目錄的my.cnf檔案,如果有則表示mysql啟動時是載入了這個配置檔案。

命令
ps aux|grep mysql|grep 'my.cnf'

輸出
fdipzone         25174
0.0 0.0 3087244 600 ?? S 4:12下午 0:01.14 /usr/local/Cellar/mysql/5.6.24/bin/mysqld --defaults-file=/usr/local/Cellar/mysql/5.6.24/my.cnf --basedir=/usr/local/Cellar/mysql/5.6.24 --datadir=/usr/local/var/mysql --plugin-dir=/usr/local/Cellar/mysql/5.6.24/lib/plugin --bind-address=127.0.0.1 --log-error=/usr/local/var/mysql/TerrydeMacBook-Air.local.err --pid-file=/usr/local/var/mysql/TerrydeMacBook-Air.local.pid fdipzone 25064 0.0 0.0 2452824 4 ?? S 4:12下午 0:00.03 /bin/sh /usr/local/opt/mysql/bin/mysqld_safe --defaults-file=/usr/local/Cellar/mysql/5.6.24/my.cnf --bind-address=127.0.0.1 --datadir=/usr/local/var/mysql

可以看到/usr/local/Cellar/mysql/5.6.24/my.cnf就是mysql啟動載入的配置檔案。

如果上面的命令沒有輸出,表示沒有設定使用指定目錄的my.cnf。

2.檢視mysql預設讀取my.cnf的目錄

如果沒有設定使用指定目錄的my.cnf,mysql啟動時會讀取安裝目錄根目錄及預設目錄下的my.cnf檔案。

檢視mysql啟動時讀取配置檔案的預設目錄

命令
mysql --help|grep 'my.cnf'

輸出
                      order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/local/etc/my.cnf ~/.my.cnf

/etc/my.cnf, /etc/mysql/my.cnf, /usr/local/etc/my.cnf, ~/.my.cnf 這些就是mysql預設會搜尋my.cnf的目錄,順序排前的優先

3.啟動時沒有使用配置檔案

如果沒有設定使用指定目錄my.cnf檔案及預設讀取目錄沒有my.cnf檔案,表示mysql啟動時並沒有載入配置檔案,而是使用預設配置。

需要修改配置,可以在mysql預設讀取的目錄中,建立一個my.cnf檔案(例如:/etc/my.cnf),把需要修改的配置內容寫入,重啟mysql後即可生效。