本文主要是简化 mysql 主从搭建,同时给需要学习 mysql 主从的同学一个现成的环境。
1. 编写 master.sh 脚本
此脚本主要是根据环境变量,创建出用于主从同步的用户。关于为什么要创建出 master,slave 脚本,是为了区分 master 和 slave 环境,因为只要把 sql 和 sh 文件放到 master 目录下面,master 在第一次初始化的时候,会自动执行里面的文件内容
mkdir -p init init/master init/slave
cat > init/master/master.sh <<EOF
#!/bin/bash
#定义用于同步的用户名
MASTER_SYNC_USER=\${MASTER_SYNC_USER:-sync_admin}
#定义用于同步的用户密码
MASTER_SYNC_PASSWORD=\${MASTER_SYNC_PASSWORD:-123456}
#定义用于登录mysql的用户名
ADMIN_USER=\${ADMIN_USER:-root}
#定义用于登录mysql的用户密码
ADMIN_PASSWORD=\${ADMIN_PASSWORD:-123456}
#定义运行登录的host地址
ALLOW_HOST=\${ALLOW_HOST:-%}
#定义创建账号的sql语句
CREATE_USER_SQL="CREATE USER '\$MASTER_SYNC_USER'@'\$ALLOW_HOST' IDENTIFIED BY '\$MASTER_SYNC_PASSWORD';"
#定义赋予同步账号权限的sql,这里设置两个权限,REPLICATION SLAVE,属于从节点副本的权限,REPLICATION CLIENT是副本客户端的权限,可以执行show master status语句
GRANT_PRIVILEGES_SQL="GRANT SELECT,REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO '\$MASTER_SYNC_USER'@'\$ALLOW_HOST';"
#定义刷新权限的sql
FLUSH_PRIVILEGES_SQL="FLUSH PRIVILEGES;"
#执行sql
mysql -u"\$ADMIN_USER" -p"\$ADMIN_PASSWORD" -e "\$CREATE_USER_SQL \$GRANT_PRIVILEGES_SQL \$FLUSH_PRIVILEGES_SQL"
EOF
2. 编写 slave.sh 脚本
slave 主要是去 master 查询最新的 pos 位置和 binlog 文件名称,然后创建同步需要的相关信息,然后执行 start slave;
cat >init/slave/slave.sh<<EOF
#定义连接master进行同步的账号
SLAVE_SYNC_USER="\${SLAVE_SYNC_USER:-sync_admin}"
#定义连接master进行同步的账号密码
SLAVE_SYNC_PASSWORD="\${SLAVE_SYNC_PASSWORD:-123456}"
#定义slave数据库账号
ADMIN_USER="\${ADMIN_USER:-root}"
#定义slave数据库密码
ADMIN_PASSWORD="\${ADMIN_PASSWORD:-123456}"
#定义连接master数据库host地址
MASTER_HOST="\${MASTER_HOST:-%}"
#连接master数据库,查询二进制数据,并解析出logfile和pos,这里同步用户要开启 REPLICATION CLIENT权限,才能使用SHOW MASTER STATUS;
RESULT=\`mysql -u"\$SLAVE_SYNC_USER" -h\$MASTER_HOST -p"\$SLAVE_SYNC_PASSWORD" -e "SHOW MASTER STATUS;" | grep -v grep |tail -n +2| awk '{print \$1,\$2}'\`
#解析出logfile
LOG_FILE_NAME=\`echo \$RESULT | grep -v grep | awk '{print \$1}'\`
#解析出pos
LOG_FILE_POS=\`echo \$RESULT | grep -v grep | awk '{print \$2}'\`
#设置连接master的同步相关信息
SYNC_SQL="change master to master_host='\$MASTER_HOST',master_user='\$SLAVE_SYNC_USER',master_password='\$SLAVE_SYNC_PASSWORD',master_log_file='\$LOG_FILE_NAME',master_log_pos=\$LOG_FILE_POS,get_master_public_key=1;"
#开启同步
START_SYNC_SQL="start slave;"
#查看同步状态
STATUS_SQL="show slave status\G;"
mysql -u"\$ADMIN_USER" -p"\$ADMIN_PASSWORD" -e "\$SYNC_SQL \$START_SYNC_SQL \$STATUS_SQL"
EOF
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
获取更多技术信息,请关注公众号 微技术之家。