mysql 설정 파일의 우선 순위

MySQL을 실행시킬 때 my.cnf라는 이름의 설정 파일을 사용하게 되는데, MySQL에서는 이 설정 파일의 위치를 한 곳으로 고정시켜두지 않았다. my.cnf는 여러 곳에 위치할 수 있다. 그러나 그 모든 내용을 읽고 적용시키지는 않는다. 여러 경로에서 가장 먼저 발견되는 my.cnf만 적용되는 것이다.

왜 이런 식으로 설계되었을까? 뭔가 장점이 있었겠지만 그 히스토리는 일단 넘어가고, 당장 설정하면서 필요한 MySQL이 my.cnf를 찾아낼 수 있는 경로와 그 우선순위를 살펴보자.

1
2
3
4
5
$ 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

mysql client-help 옵션으로 많은 것을 알 수 있는데 너무 많아 혼란스러우므로 grep을 사용했다.

출력된 결과

  1. /etc/my.cnf
  2. /etc/mysql/my.cnf
  3. /usr/local/etc/my.cnf
  4. ~/.my.cnf

가 곧 MySQL이 설정 파일 my.cnf를 찾는 경로이며 그 우선 순위가 되겠다.

그런데 4번인 ~/.my.cnf는 다른 것들과 달리 상대 경로에 위치해 있다. 4번의 절대 경로는 어디일까?

1
2
3
4
5
6
# 먼저 MySQL 이 실행되어 있다는 전제 하에,
$ ps aux|grep mysql

# 결과
70920 0.0 10.9 3083972 456244 s000 S 5:04PM 0:00.65 /usr/local/Cellar/mysql/5.6.27/bin/mysqld --basedir=/usr/local/Cellar/mysql/5.6.27 --datadir=/usr/local/var/mysql --plugin-dir=/usr/local/Cellar/mysql/5.6.27/lib/plugin --log-error=/usr/local/var/mysql/MacbookAir.local.err --pid-file=/usr/local/var/mysql/MacbookAir.local.pid
70824 0.0 0.0 2454888 1272 s000 S 5:04PM 0:00.03 /bin/sh /usr/local/Cellar/mysql/5.6.27/bin/mysqld_safe --datadir=/usr/local/var/mysql --pid-file=/usr/local/var/mysql/MacbookAir.local.pid

이 경우 brew로 MySQL을 설치했기 때문에 설치된 MySQL의 경로가 /usr/local/Cellar/mysql/5.6.27가 되었다. 이 위치에서 my.cnf를 발견할 수 있다.