動態

詳情 返回 返回

世界可寫(關於windows上docker +wsl的權限問題) - 動態 詳情

在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

感謝老鐵門

user avatar zjkal 頭像 yanwushu 頭像 rui_sen 頭像 guanguans 頭像 0xboo 頭像 buildyuan 頭像 biliangxianting 頭像 manongsir 頭像 xiaotuyu 頭像 shenchendebanma 頭像 _kysou 頭像
點贊 11 用戶, 點贊了這篇動態!
點贊

Add a new 評論

Some HTML is okay.