(共556篇)
全部分类

Docker Compose 创建Mysql服务需要注意的问题
[ Docker ] 

前提

在一次使用Docker Compose 启动Mysql服务时, 有以下要求:

  1. 需要覆盖原镜像的command
  2. 持久化mysql数据, 配置文件内容如下:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
version: '3'
services:
  mysql:
    image: mysql:8.0
    environment:
      - MYSQL_ROOT_PASSWORD=budao123
      - MYSQL_DATABASE=budao
    volumes:
      - ./cache:/var/lib/mysql
    command:
      [
        "/bin/sh",
        "-c",
        "sed -i '/\\[mysqld\\]/ a sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION' /etc/mysql/my.cnf && mysqld"
      ]
    ports:
      - 8406:3306

volumes部分, 使用docker-compose.yml文件统计目录下的cache目录作为持久化数据目录.

问题1

如果启动后发现服务启动失败, 并且有以下错误提示:

1
2
3
4
5
6
7
8
9
[+] Running 2/1
 ⠿ Network docker_default    Created                                                                                                    0.1s
 ⠿ Container docker-mysql-1  Created                                                                                                    0.1s
Attaching to docker-mysql-1
docker-mysql-1  | 2022-11-22T09:36:38.899332Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.27) starting as process 9
docker-mysql-1  | 2022-11-22T09:36:38.902726Z 0 [ERROR] [MY-010123] [Server] Fatal error: Please read "Security" section of the manual to find out how to run mysqld as root!
docker-mysql-1  | 2022-11-22T09:36:38.903313Z 0 [ERROR] [MY-010119] [Server] Aborting
docker-mysql-1  | 2022-11-22T09:36:38.903466Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.27)  MySQL Community Server - GPL.
docker-mysql-1 exited with code 1

这个问题很好解决, Mysql8版本默认情况下, 禁止使用root用户启动服务, 修改一下启动命令即可:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
version: '3'
services:
  mysql:
    image: mysql:8.0
    environment:
      - MYSQL_ROOT_PASSWORD=budao123
      - MYSQL_DATABASE=budao
    volumes:
      - ./cache:/var/lib/mysql
    command:
      [
        "/bin/sh",
        "-c",
        "sed -i '/\\[mysqld\\]/ a sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION' /etc/mysql/my.cnf && mysqld --user=root"
      ]
    ports:
      - 8406:3306

问题2

如果启动失败, 并且有如下错误提示:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11

[+] Running 1/0
 ⠿ Container docker-mysql-1  Created                                                                                                    0.0s
Attaching to docker-mysql-1
docker-mysql-1  | 2022-11-22T09:37:24.533181Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.27) starting as process 8
docker-mysql-1  | 2022-11-22T09:37:24.541693Z 0 [Warning] [MY-010075] [Server] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 45414757-6a49-11ed-a407-0242ac120002.
docker-mysql-1  | 2022-11-22T09:37:24.546424Z 1 [ERROR] [MY-011011] [Server] Failed to find valid data directory.
docker-mysql-1  | 2022-11-22T09:37:24.546783Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed.
docker-mysql-1  | 2022-11-22T09:37:24.546989Z 0 [ERROR] [MY-010119] [Server] Aborting
docker-mysql-1  | 2022-11-22T09:37:24.547555Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.27)  MySQL Community Server - GPL.
docker-mysql-1 exited with code 1

这是因为在启动mysqld时, 挂载的持久化目录还没有被初始化, 导致启动失败, 遇到这种情况, 可以按照以下步骤操作:

  1. 注释掉command部分, 先通过docker compose up -d启动一下服务, 这是为了初始化持久化数据保存目录
  2. 打开command部分, 重新通过docker compose up -d启动服务即可

其他

Mysql8版本之后, 使用group by时, 要求group by的字段必须出现在select中, 可以通过修改sql_mode来关闭这个特性, 所以在本文的docker compose文件中, 添加了sed -i '/\\[mysqld\\]/ a sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION' /etc/mysql/my.cnf. 目的就是关闭sql mode中的ONLY_FULL_GROUP_BY 模式