使用Docker自动化部署MySQL集群
短信预约 -IT技能 免费直播动态提醒
1. 编写master.sh脚本
此脚本主要是根据环境变量,创建出用于主从同步的用户。关于为什么要创建出master,slave脚本,是为了区分master和slave环境,因为只要把sql和sh文件放到master目录下面,master在第一次初始化的时候,会自动执行里面的文件内容
mkdir -p init init/master init/slave
cat > init/master/master.sh <
2. 编写slave.sh脚本
slave主要是去master查询最新的pos位置和binlog文件名称,然后创建同步需要的相关信息,然后执行start slave;
cat >init/slave/slave.sh<
3. 编写docker-compose.yml脚本
version: '3'
services:
master:
image: mysql:8
container_name: mysql-master
ports:
- '3306:3306'
restart: always
hostname: mysql-master
environment:
MYSQL_ROOT_PASSWORD: "123456"
MASTER_SYNC_USER: "sync"
MASTER_SYNC_PASSWORD: "123456"
ADMIN_USER: "root"
ADMIN_PASSWORD: "123456"
TZ: "Asia/Shanghai"
deploy:
resources:
limits:
memory: 512M
cpus: 50m
healthcheck:
test: ["CMD","mysqladmin","-uroot","-p$${MYSQL_ROOT_PASSWORD}","ping","-h","localhost"]
timeout: 2s
interval: 10s
retries: 5
start_period: 5s
logging:
options:
max-file: '1'
max-size: '128k'
command:
- "--server-id=1"
- "--character-set-server=utf8mb4"
- "--collation-server=utf8mb4_unicode_ci"
- "--log-bin=mysql-bin"
- "--sync_binlog=1"
- "--binlog-ignore-db=mysql"
- "--binlog-ignore-db=sys"
- "--binlog-ignore-db=performance_schema"
- "--binlog-ignore-db=information_schema"
- "--sql_mode=NO_AUTO_VALUE_ON_ZERO,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION,PIPES_AS_CONCAT,ANSI_QUOTES"
volumes:
- ./init/master:/docker-entrypoint-initdb.d
- ./data/master:/var/lib/mysql
slave:
image: mysql:8
container_name: mysql-slave
ports:
- '3307:3306'
restart: always
hostname: mysql-slave
environment:
MYSQL_ROOT_PASSWORD: "123456"
SLAVE_SYNC_USER: "sync"
SLAVE_SYNC_PASSWORD: "123456"
ADMIN_USER: "root"
ADMIN_PASSWORD: "123456"
MASTER_HOST: "mysql-master"
TZ: "Asia/Shanghai"
healthcheck:
test: ["CMD","mysqladmin","-uroot","-p$${MYSQL_ROOT_PASSWORD}","ping","-h","localhost"]
timeout: 2s
interval: 10s
retries: 5
start_period: 5s
deploy:
resources:
limits:
memory: 512M
cpus: 50m
logging:
options:
max-file: '1'
max-size: '128k'
depends_on:
master:
condition: service_healthy
command:
- "--server-id=2"
- "--character-set-server=utf8mb4"
- "--collation-server=utf8mb4_unicode_ci"
- "--sql_mode=NO_AUTO_VALUE_ON_ZERO,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION,PIPES_AS_CONCAT,ANSI_QUOTES"
volumes:
- ./init/slave:/docker-entrypoint-initdb.d
- ./data/slave:/var/lib/mysql
启动脚本
docker-compose up -d
后期维护过程中可能用到的脚本
STOP SLAVE;
RESET SLAVE;
CHANGE MASTER TO master_log_pos=0;
START SLAVE;
SHOW SLAVE STATUS;
写在后面,基于docker-compose搭建mysql主从已经结束,这里主要演示了如何一键搭建mysql主从,同时测试在各种环境下,还能保证主从同步。但是单主从同步肯定也无法满足企业的需求,有兴趣的同学可以研究一些主主双写同步,然后通过nginx搭建主备模式,达到高可用。
这里提供一个判断的方法,编写#boot.sh脚本文件,这时候需要重写dockerfile文件,使用myslq debian的镜像文件。并通过apt添加netcat工具。具体不再详述了,以下为判断脚本。
#!/bin/bash
wait_for() {
echo Waiting for $1 to listen on $2...
sleep 1
while ! nc -vz $1 $2
do
echo waiting...;
sleep 1s;
done
}
if [ -e "/slave" ]; then
echo "slave..."
cp /slave.cnf /etc/mysql/conf.d/
else
echo "master..."
cp /master.cnf /etc/mysql/conf.d/
fi
echo "master config ... "
/master.sh
echo "complete. "
if [ -n "$MASTER_HOST" ]; then
echo "replicaof $MASTER_HOST"
wait_for $MASTER_HOST $MASTER_PORT
/slave.sh
echo "Slave config complete."
fi