创建文件夹
mkdir -p /home/usr/docker/mysql/mydir
mkdir -p /home/usr/docker/mysql/datadir
mkdir -p /home/usr/docker/mysql/conf
mkdir -p /home/usr/docker/mysql/sql
创建mysql-docker-compose.yaml
version: '3'
services:
mysql:
restart: always
image: mysql:5.7.16
container_name: my_mysql
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 # 不区分大小写
volumes:
- /home/usr/docker/mysql/mydir:/mydir
- /home/usr/docker/mysql/datadir:/var/lib/mysql
- /home/usr/docker/mysql/conf/my.cnf:/etc/my.cnf
# 数据库还原目录 可将需要还原的sql文件放在这里
- /home/usr/docker/mysql/sql:/docker-entrypoint-initdb.d
environment:
- "MYSQL_ROOT_PASSWORD=root"
- "TZ=Asia/Shanghai"
ports:
- 33306:3306
networks:
- net_mysql
networks:
net_mysql:
driver: bridge
my.cnf
[mysqld]
user=mysql
default-storage-engine=INNODB
#character-set-server=utf8
character-set-client-handshake=FALSE
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
[client]
#utf8mb4字符集可以存储emoji表情字符
#default-character-set=utf8
default-character-set=utf8mb4
[mysql]
#default-character-set=utf8
default-character-set=utf8mb4
运行 docker-compose
docker-copmse up -d