yml文件
首先yaml文件:
# 指定compose 版本
version: '3'
services:
mysql:
image: mysql:latest
restart: always
# 容器的名称
container_name: docker_mysql
# 映射端口 相当于 -p 宿主机端口:容器端口
ports:
- '3306:3306'
# 设置环境变量
environment:
# 设置root用户的密码
MYSQL_ROOT_PASSWORD: 123456
MYSQL_USER: mysql
MYSQL_PASSWORD: 123456
# 设置时区
TZ: Asia/Shanghai
command:
# 防止低版本的连接客服报错的问题
--default-authentication-plugin=mysql_native_password
# 设置数据库的编码
--character-set-server=utf8mb4
# 设置数据库默认的排序方式
--collation-server=utf8mb4_general_ci
# 解决低版本不允许时间字段 not null
--explicit_defaults_for_timestamp=true
# Linux 表名区分大小写 1 表示不区分大小写
--lower_case_table_names=1
--sql_mode= STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
# 挂载
volumes:
# 数据的数据挂载到容器中
- ./mysql/data:/var/lib/mysql
- ./mysql/log:/var/log/mysql
- ./mysql/config/:/etc/mysql/coonf.d/
my.cnf配置文件
需要注意,直接运行上面的yaml文件,在本地通过Navicat连接是连接不上的,我们还需要额外的配置,在**./mysql/config/**目录下创建一个配置文件my.cnf,内容如下:
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
skip-grant-tables
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
lower_case_table_names=1
sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
init_connect='SET collation_connection = utf8mb4_general_ci'
init_connect='SET NAMES utf8mb4'
character-set-client-handshake=FALSE
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci
max_connections = 1000
net_read_timeout=3600
net_write_timeout=3600
docker-compose运行
配置好之后,运行:
docker-compose -f [mysql.yaml] up -d