MySQL如何找到使用的是哪个配置文件?
1、问题来源
docker创建mysql8的容器
docker run \
--name="mysql8" \
--restart=unless-stopped \
-v "/mnt/mysql8/data:/var/lib/mysql" \
-v "/mnt/mysql8/conf/my.cnf:/etc/mysql/conf.d/my.cnf" \
-v "/mnt/mysql8/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d" \
-e TZ="Asia/Shanghai" \
-e MYSQL_ROOT_PASSWORD="123456" \
-e MYSQL_USER="admin" \
-e MYSQL_PASSWORD="123456" \
-e MYSQL_ROOT_HOST='%' \
-e MYSQL_USER_HOST='%' \
-p "3306:3306" \
-d \
mysql:8.0
其中好奇my.cnf的位置为什么放在conf.d下,然后需要在容器找到了很多my.cnf,例如
/etc/mysql/my.cnf
/etc/mysql/mysql.conf.d/mysqld.cnf
/usr/local/mysql/etc/my.cnf(如果MySQL是安装在非标准位置)
/usr/etc/my.cnf
所以想确认我的文件能否正常被mysql正常识别并使用。
2、解答
在mysql的查询中,可以查询出当前使用了哪些配置文件
mysql> SELECT variable_path,COUNT(*) FROM performance_schema.variables_info WHERE variable_path<>'' GROUP BY variable_path;
+------------------------------------+----------+
| variable_path | COUNT(*) |
+------------------------------------+----------+
| /etc/mysql/my.cnf | 4 |
+------------------------------------+----------+
| /etc/mysql/conf.d/docker.cnf | 1 |
+------------------------------------+----------+
所以mysql的指定的配置文件就是这两个,那我的文件岂不就是没有生效了吗?
别急,还有反转
当查看/etc/mysql/my.cnf 文件时,查看目录,发现有三个文件
root@4d6ad856f388:/etc/mysql# ls
conf.d my.cnf my.cnf.fallback
-my.cnf就是数据库指定的配置文件
-my.cnf.fallback是当my.cnf修改或者文件错误时,方便回滚
接着查询my.cnf时,发现最后一句 !includedir /etc/mysql/conf.d/
root@4d6ad856f388:/etc/mysql# cat my.cnf
# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
# Custom config should go here
!includedir /etc/mysql/conf.d/
---------------------------------------------------------------------------------------------------------
解释如下:
# Custom config should go here:
这是一个注释,提示用户或管理员,如果需要在MySQL的默认配置之外添加自定义配置,应该在这个位置或之后的配置文件中进行。然而,在Docker容器中,由于配置文件可能是只读的或者通过环境变量进行配置的,因此你可能需要将自定义配置添加到/etc/mysql/conf.d/目录下的文件中,而不是直接修改主配置文件。
!includedir /etc/mysql/conf.d/:
这是一个指令,告诉MySQL服务器在启动时除了读取主配置文件(如my.cnf)之外,还应该包含(include)位于/etc/mysql/conf.d/目录下的所有配置文件。这些额外的配置文件通常用于添加或覆盖主配置文件中的设置。
因此,当我们有自己的配置文件时,可以将其放在容器的/etc/mysql/conf.d下。