下载镜像
docker pull mysql:5.7
编写docker-compose.yml
version: '3'
services:
mysql:
image: "mysql:5.7"
container_name: mysql57
restart: always
ports:
- "8306:3306"
command:
# 配置密码连接
--default-authentication-plugin=mysql_native_password
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
--explicit_defaults_for_timestamp=true
--lower_case_table_names=1
--max_allowed_packet=128M
# 此处已在my.cnf里面配置
# --sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO"
environment:
TZ: Asia/Shanghai
MYSQL_ROOT_PASSWORD: "123456"
volumes:
# 挂载mysql数据目录
- /home/data/mysql/db:/var/lib/mysql
# 挂载mysql日志目录
- /home/data/mysql/log:/var/log/mysql
# 挂载mysql配置文件
- ./my.cnf:/etc/mysql/my.cnf
# 挂载mysql初始化脚本
- ./init/:/docker-entrypoint-initdb.d/
my.cnf默认配置
[mysqld]
user=mysql
default-storage-engine=INNODB
character-set-server=utf8
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
启动mysql
docker-compose up -d