在windows上安装了docker,总会有奇怪的问题。这次的问题是加载mysql的conf文件,果不其然,没有加载上来。

300°近视的我看到了一行提示:

sh-4.2# mysql -uroot -ppassword
mysql: [Warning] World-writable config file '/etc/mysql/conf.d/mysql.cnf' is ignored.
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.44 MySQL Community Server (GPL)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

你看到了吗,就是

World-writable config file '/etc/mysql/conf.d/mysql.cnf' is ignored.

世界可写的文件被忽略。。。

吓得我赶紧去看看这个世界啥样子

zhuan@zhuan MINGW64 /d/project/code/12java/-docker/mysql-conf
$ file  my.cnf
my.cnf: ASCII text, with CRLF line terminators

zhuan@zhuan MINGW64 /d/project/code//12java/docker/mysql-conf
$ ls -l my.cnf
-rw-r--r-- 1 zhuan 197121 279 Oct 31 11:40 my.cnf

zhuan@zhuan MINGW64 /d/project/code/12java/docker/mysql-conf
$

尼玛,-rw-r--r-- 肯定不是世界可写啊。

于是我本着打严谨的态度,看了看容器内的配置文件

mysql> select @@SQL_MODE;
+-------------------------------------------------------------------------------------------------------------------------------------------+
| @@SQL_MODE                                                                                                                                |
+-------------------------------------------------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+-------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> exit
Bye
sh-4.2# ls -l /etc/mysql/conf.d/mysql.cnf
-rwxrwxrwx 1 root root 279 Oct 31 11:40 /etc/mysql/conf.d/mysql.cnf
sh-4.2# 

尼玛,-rwxrwxrwx 好吧,你没错。我错了。那怎么办?

问了一圈ai。结论就是 wsl和docker之间的挂载的权限并不是透传的,而是擅自主张的。下面是原文:

🔍 深度分析:为什么 644 还被当成“世界可写”?

虽然你在 WSL/MINGW64 里看到权限是 644,但 Docker Desktop 在 Windows 上运行时,挂载的文件权限可能被重新解释,尤其是在以下情况:

✅ 根本原因:
Docker Desktop for Windows 使用 gVFS 或 CIFS/SMB 映射宿主机文件到容器,文件权限可能被强制设为宽松模式(如 777),即使你在 WSL 看到的是 644。

你在 WSL 中看到的权限是 WSL 层的。
但 Docker 容器运行在 Hyper-V 虚拟机(MobyLinuxVM) 中,它通过 CIFS 挂载 Windows 文件夹。
CIFS 默认挂载时会设置 file_mode=0777,dir_mode=0777,导致文件在容器内被视为“世界可写”。
因此,MySQL 容器内看到的 /etc/mysql/conf.d/mysql.cnf 实际权限是 777,于是触发安全警告并忽略文件。

解决办法:

  1. 自己build镜像,即加载dockfile的方式,打进去的任何配置都可以(前置解决)
mysql:
  # image: mysql:5.7
  build:  # 使用自定义构建
    context: .
    dockerfile: Dockerfile.mysql
  container_name: dapeng-mysql

dockerfile如下:

FROM mysql:5.7

# 设置时区(可选)
ENV TZ=Asia/Shanghai

# 将本地配置文件复制到镜像中(不会受挂载权限影响)
COPY ./mysql-conf/my.cnf /etc/mysql/conf.d/mysql.cnf

# 设置文件权限(确保是 644)
RUN chmod 644 /etc/mysql/conf.d/mysql.cnf
  1. 用command去走一遍(后置解决)
 ports:
    - "3306:3306"
  command: >
    --sql_mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"
    --character-set-server=utf8mb4
    --collation-server=utf8mb4_general_ci
    --lower_case_table_names=1
  healthcheck:
    test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-ppassword"]
    interval: 30s
    timeout: 10s
    retries: 1

感谢老铁门


牙小木木
1.5k 声望84 粉丝

iamtb.cn