docker node update --label-add app=db 节点ID或HOSTNAME
示例:
docker node update --label-add app=db worker1
docker node update --label-add app=db worker2
docker node update --label-add role=master 节点ID或HOSTNAME
示例:
docker node update --label-add role=master work1
docker node update --label-add role=slave 节点ID或HOSTNAME
示例:
docker node update --label-add role=slave work2
vim compose.yaml
version: "3.9"
services:
master:
image: mysql:8.0.33
container_name: mysql_8_master
hostname: master
restart: always
networks:
- db_network
ports:
- "13306:3306"
volumes:
- /etc/localtime:/etc/localtime:ro
- db_master:/var/lib/mysql
configs:
- source: master_conf
target: /etc/mysql/conf.d/master.cnf
mode: 0444
- source: master_initdb
target: /docker-entrypoint-initdb.d/master_init.sql
mode: 0444
secrets:
- root_key
- replication_key
environment:
- MYSQL_ROOT_PASSWORD_FILE=/run/secrets/root_key
- MYSQL_REPLICATION_MODE=master
- MYSQL_REPLICATION_USER=replication
- MYSQL_REPLICATION_PASSWORD_FILE=/run/secrets/replication_key
deploy:
mode: replicated
replicas: 1
placement:
constraints:
- node.labels.app==db
- node.labels.role==master
slave:
image: mysql:8.0.33
container_name: mysql_8_slave
hostname: slave
restart: always
networks:
- db_network
ports:
- "23306:3306"
volumes:
- /etc/localtime:/etc/localtime:ro
- db_slave:/var/lib/mysql
configs:
- source: slave_conf
target: /etc/mysql/conf.d/slave.cnf
mode: 0444
- source: salve_initdb
target: /docker-entrypoint-initdb.d/slave_init.sql
mode: 0444
secrets:
- root_key
environment:
- MYSQL_ROOT_PASSWORD_FILE=/run/secrets/root_key
- MYSQL_REPLICATION_MODE=slave
deploy:
mode: replicated
replicas: 1
placement:
constraints:
- node.labels.app==db
- node.labels.role==slave
depends_on:
- master
volumes:
db_master:
db_slave:
configs:
master_conf:
file: master.cnf
master_initdb:
file: master_init.sql
slave_conf:
file: slave.cnf
salve_initdb:
file: slave_init.sql
secrets:
root_key:
file: root.key
replication_key:
file: replication.key
networks:
db_network:
name: db_network
driver: overlay
driver_opts:
encrypted: 'true'
ipam:
driver: default
config:
- subnet: 172.21.0.1/24
说明:
mysql:8.0.33
为 MySQL 的镜像实际使用时,修改上述两处内容即可,如集群外部无需访问 mysql 访问,则可不映射端口,但下方的 sql 文件需要做对应的修改
vim master.cnf
[mysqld]
server-id = 1
report_host = master
log_bin = master-bin
log_error = master-bin.err
binlog_format = mixed
binlog_expire_logs_seconds = 604800
max_binlog_size = 1G
binlog-ignore-db = mysql
binlog-ignore-db = sys
binlog-ignore-db = information_schema
binlog-ignore-db = performance_schema
max_connections = 1000
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
说明:
binlog_format
为主从节点的数据复制方式
statement
模式不会记录每一条更改语句,节约资源但主从数据可能不一致row
模式记录每一条更改的语句,日志量非常大mixed
模式是前两者优点的综合,但日志结构较为复杂binlog_expire_logs_seconds
为日志文件保留时间(秒)binlog-ignore-db
指定不需要同步的数据库vim master_init.sql
create user 'replication'@'%' identified by '123456';
grant replication slave, replication client on *.* to 'replication'@'%';
show grants for 'replication'@'%';
flush privileges;
注意:
replication
为同步用户123456
为 replication
用户的密码vim slave.cnf
[mysqld]
server-id = 2
report_host = slave
replicate_ignore_db = mysql
replicate_ignore_db = sys
replicate_ignore_db = information_schema
replicate_ignore_db = performance_schema
max_connections = 1000
vim slave_init.sql
change master to master_host='192.168.1.100', master_port=13306, master_user='replication', master_password='123456', master_log_file='master-bin.000003', master_log_pos=157, get_master_public_key=1;
set global sql_slave_skip_counter=1;
start slave;
注意:
master_host
为主节点的 IP,因为此处使用了 Docker Swarm 的负载均衡,因此配置为集群任一节点的 IP 即可master_port
为主节点的端口号master_user
为同步时使用的用户master_password
为 master_user 用户的密码master_log_file
为同步的日志文件master_log_pos
为同步的日志文件的行数vim root.key
123456
vim replication.key
123456
docker stack deploy -c compose.yaml mysql_8_cluster
说明:
mysql_8_cluster
为服务的名称docker stack services mysql_8_cluster
docker stack ps mysql_8_cluster
docker service logs mysql_8_cluster_master
docker service logs mysql_8_cluster_slave
show master status;
show slave status;
当 Slave_IO_Running
和 Slave_SQL_Running
都为 Yes
的时候,表示主从同步正常