小知识:MySQL配置文件优先级

今天在RHEL7上,严格按以前的安装规范文档,部署MySQL环境时,发现经过服务的方式启动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 参数值正好匹配报错信息。
解决方案有两个:sql

  • 1.删掉/etc/my.cnf配置文件
  • 2.使用规范的参数配置,直接覆盖/etc/my.cnf配置文件

我这里选择了第二种方案,成功解决问题。shell

最后复盘时发现用:socket

mysql --help|more

能够看到参数文件的顺序是以下:code

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]下,就能够实现被后面优先级低的用户配置文件覆盖。it

相关文章
相关标签/搜索