下载镜像

  1. docker pull mysql:5.7

编写docker-compose.yml

  1. version: '3'
  2. services:
  3. mysql:
  4. image: "mysql:5.7"
  5. container_name: mysql57
  6. restart: always
  7. ports:
  8. - "8306:3306"
  9. command:
  10. # 配置密码连接
  11. --default-authentication-plugin=mysql_native_password
  12. --character-set-server=utf8mb4
  13. --collation-server=utf8mb4_general_ci
  14. --explicit_defaults_for_timestamp=true
  15. --lower_case_table_names=1
  16. --max_allowed_packet=128M
  17. # 此处已在my.cnf里面配置
  18. # --sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO"
  19. environment:
  20. TZ: Asia/Shanghai
  21. MYSQL_ROOT_PASSWORD: "123456"
  22. volumes:
  23. # 挂载mysql数据目录
  24. - /home/data/mysql/db:/var/lib/mysql
  25. # 挂载mysql日志目录
  26. - /home/data/mysql/log:/var/log/mysql
  27. # 挂载mysql配置文件
  28. - ./my.cnf:/etc/mysql/my.cnf
  29. # 挂载mysql初始化脚本
  30. - ./init/:/docker-entrypoint-initdb.d/

my.cnf默认配置

  1. [mysqld]
  2. user=mysql
  3. default-storage-engine=INNODB
  4. character-set-server=utf8
  5. sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
  6. [client]
  7. default-character-set=utf8
  8. [mysql]
  9. default-character-set=utf8

启动mysql

  1. docker-compose up -d