docker-compose部署mysql

  1. 1. 说明
  2. 2. 代码
    1. 2.1. 创建网络
    2. 2.2. docker-compose.yaml 文件
    3. 2.3. .env 文件

说明

对 mysql 进行容器化

代码

创建网络

1
docker network create --subnet=172.20.0.0/24 --gateway=172.20.0.1 --attachable local-networks

docker-compose.yaml 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
version: '3.6'
services:
mysql-server:
image: mysql:5.7
container_name: mysql-server
restart: always
volumes:
- "./data:/var/lib/mysql:rw"
# linux使用
- "/usr/share/zoneinfo/Asia/Shanghai:/etc/localtime:ro"
# macos使用
# - "/var/db/timezone/zoneinfo/Asia/Shanghai:/etc/localtime:ro"
environment:
- "MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}"
- "TZ=Asia/Shanghai"
- "LANG=C.UTF-8"
ports:
- 3306:3306
command:
--wait-timeout=28800
--init-connect='SET NAMES utf8mb4'
--character-set-server=utf8mb4
--collation-server=utf8mb4_unicode_ci
--character-set-client-handshake=FALSE
--explicit_defaults_for_timestamp=true
--max_allowed_packet=500M
--lower_case_table_names=1
--log-bin=mysql-bin
--server-id=1
--expire_logs_days=7
--log_timestamps=SYSTEM
--default-time-zone='+08:00'
--sql-mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
--innodb-flush-log-at-trx-commit=1
networks:
local-networks:
ipv4_address: 172.20.0.2

networks:
local-networks:
external: true

.env 文件

1
2
# mysql root密码
MYSQL_ROOT_PASSWORD=