由于是单机同时运行主从实例,仅用于开发环境或学习主从复制配置。
项目地址 github.com/TomCzHen/my…
文件结构
├── docker-compose.yaml
├── env
│ ├── base.env
│ ├── master.env
│ └── node.env
├── .env
├── init-db-sql
│ ├── init-master.sh
│ ├── init-node.sh
│ ├── sakila-data.sql
│ └── sakila-schema.sql
└── README.md
复制代码
env
根目录下的 .env
文件作用域是在 docker-compose.yaml
中,而 env
路径下的文件作用与容器内部环境变量,两者作用不同。
.env
TAG=5.7.20
MASTER_SERVER_ID=1
NODE_1_SERVER_ID=10
NODE_2_SERVER_ID=20
MASTER_MYSQL_ROOT_PASSWORD=master_root_pwd
NODE_MYSQL_ROOT_PASSWORD=node_root_pwd
复制代码
docker-compose.yaml
version: "3.3"
services:
mysql-master: &mysql
image: mysql:${TAG}
container_name: mysql-master
restart: unless-stopped
env_file:
- env/base.env
- env/master.env
environment:
- MYSQL_ROOT_PASSWORD=${MASTER_MYSQL_ROOT_PASSWORD}
ports:
- "3306:3306"
expose:
- "3306"
volumes:
- mysql-master-data:/var/lib/mysql
- ./init-db-sql/sakila-schema.sql:/docker-entrypoint-initdb.d/1-schema.sql
- ./init-db-sql/sakila-data.sql:/docker-entrypoint-initdb.d/2-data.sql
- ./init-db-sql/init-master.sh:/docker-entrypoint-initdb.d/3-init-master.sh
command: [
"--log-bin=mysql-bin",
"--server-id=${MASTER_SERVER_ID}",
"--character-set-server=utf8mb4",
"--collation-server=utf8mb4_unicode_ci",
"--innodb_flush_log_at_trx_commit=1",
"--sync_binlog=1"
]
mysql-node-1: &mysql-node