详解使用shell脚本实现Docker搭建MySQL主从复制(同步)

目录

环境说明

1.创建my.cnf文件(MySQL的配置文件)

2.创建shell脚本create_mysql.sh(用于删除原有的数据库所有信息,创建新的MySQL主从数据库)

3.创建shell脚本replication.sh(用于对MySQL主从进行配置)

4.正确结果


环境说明

我使用的是VM8centos7.9迷你版,虚拟机IP地址为:192.168.178.102

容器名 节点 端口
mysql_master master 3308
mysql_node1 node1 4001
mysql_node2 node2 4002

你得安装了docker

注意:你可能得修改IP地址,端口号等

1.创建my.cnf文件(MySQL的配置文件)

[mysqld]
server-id=1
#启用二进制日志
log-bin=mysql-bin
# 设置不要复制的数据库(可设置多个)
binlog-ignore-db=mysql
binlog-ignore-db=information_schema

#设置logbin格式
binlog_format=STATEMENT

2.创建shell脚本create_mysql.sh(用于删除原有的数据库所有信息,创建新的MySQL主从数据库)

需要注意的是可第一次执行下面的脚本,可能显示无法连接MySQL;你可以等一会再执行一次,连接不行那我也不知道,就这个BUG

#! /bin/bash

master_name="mysql_master"
master_port=3308
node1_name="mysql_node1"
node1_port=4001
node2_name="mysql_node2"
node2_port=4002
mysql_passwd="110119"

systemctl restart docker

echo "停止并删除mysql集群容器"
docker stop ${master_name} ${node1_name} ${node2_name} > /dev/null
docker rm ${master_name} ${node1_name} ${node2_name} > /dev/null
echo "rm -rf mysql目录"
rm -rf /root/{${master_name},${node1_name},${node2_name}} > /dev/null
echo "执行完成"

echo "创建mysql目录"
mkdir -p /root/{${master_name},${node1_name},${node2_name}}/conf > /dev/null
echo "删除完毕"

echo "mkdir mysql目录"
cp /etc/docker/mysql/my.cnf /root/${master_name}/conf/
cp /etc/docker/mysql/my.cnf /root/${node1_name}/conf/
cp /etc/docker/mysql/my.cnf /root/${node2_name}/conf/
sed -i 's/server-id=1/server-id=2/' /root/${node1_name}/conf/my.cnf
sed -i 's/server-id=1/server-id=3/' /root/${node2_name}/conf/my.cnf
sed -i '$a\relay_log=mall-mysql-relay-bin\nlog_slave_updates=1\nread_only=1' /root/${node1_name}/conf/my.cnf
sed -i '$a\relay_log=mall-mysql-relay-bin\nlog_slave_updates=1\nread_only=1' /root/${node2_name}/conf/my.cnf
echo "创建完毕"

docker run -d -p ${master_port}:3306 \
-v /root/${master_name}/conf:/etc/mysql/ \
-v/root/${master_name}/data:/var/lib/mysql  \
-e MYSQL_ROOT_PASSWORD=${mysql_passwd} --name ${master_name} mysql:5.7
docker run -d -p ${node1_port}:3306 \
-v /root/${node1_name}/conf:/etc/mysql/ \
-v/root/${node1_name}/data:/var/lib/mysql  \
-e MYSQL_ROOT_PASSWORD=${mysql_passwd} --name ${node1_name} mysql:5.7
docker run -d -p ${node2_port}:3306 \
-v /root/${node2_name}/conf:/etc/mysql/ \
-v/root/${node2_name}/data:/var/lib/mysql  \
-e MYSQL_ROOT_PASSWORD=${mysql_passwd} --name ${node2_name} mysql:5.7
echo "创建mysql容器"

create_mysql.sh文件说明,仅仅是说明

这些是创建mysql容器

docker run -d -p ${node2_port}:3306 \
-v /root/${node2_name}/conf:/etc/mysql/ \
-v/root/${node2_name}/data:/var/lib/mysql  \
-e MYSQL_ROOT_PASSWORD=${mysql_passwd} --name ${node2_name} mysql:5.7

这些是修改复制到各自mysql节点下的my.cnf修改内容

sed -i 's/server-id=1/server-id=2/' /root/${node1_name}/conf/my.cnf
sed -i '$a\relay_log=mall-mysql-relay-bin\nlog_slave_updates=1\nread_only=1' /root/${node2_name}/conf/my.cnf

这是复制mysql配置文件my.cnf

cp /etc/docker/mysql/my.cnf /root/${master_name}/conf/

这些是对原有的三个mysql节点容器进行,停止删除

docker stop ${master_name} ${node1_name} ${node2_name} > /dev/null
docker rm ${master_name} ${node1_name} ${node2_name} > /dev/null

这是对mysql挂载目录的删除

rm -rf /root/{${master_name},${node1_name},${node2_name}} > /dev/null

这是创建mysql挂载目录

mkdir -p /root/{${master_name},${node1_name},${node2_name}}/conf > /dev/null

3.创建shell脚本replication.sh(用于对MySQL主从进行配置)

#! /bin/bash

master_name="mysql_master"
master_port=3308
node1_name="mysql_node1"
node1_port=4001
node2_name="mysql_node2"
node2_port=4002
replication_user="slave"
mysql_user="root"
mysql_passwd=110119
mysql_address=192.168.178.102

echo "master开始"
docker exec -it ${master_name} /bin/bash -c "mysql -u${mysql_user} -p${mysql_passwd} << eof
grant replication slave on *.* to '${replication_user}'@'%' identified by '${mysql_passwd}';
eof"
docker exec -it ${master_name} /bin/bash -c "mysql -h${mysql_address} -P${master_port} -u${mysql_user} -p${mysql_passwd} -e 'flush privileges;'"


# 特别说明:awk需要自己修改,因为你可能显示的不是第二列和第三列
bin_log=`docker exec -it ${master_name} /bin/bash -c "mysql -u${mysql_user} -p${mysql_passwd} -e 'show master status;'" | sed -n '/mysql-bin/p' | awk '{print $2}'`
bin_Position=`docker exec -it ${master_name} /bin/bash -c "mysql -u${mysql_user} -p${mysql_passwd} -e 'show master status;'" | sed -n '/mysql-bin/p' | awk '{print $4}'`
echo "${bin_log}"
echo "${bin_Position}"
echo "配置完毕"

if [ $? -ne 0 ];then
	echo "主库配置错误"
	exit
fi

echo "node1从库配置"
docker exec -it ${node1_name} /bin/bash -c "mysql -u${mysql_user} -p${mysql_passwd} -e 'stop slave;'"
docker exec -it ${node1_name} /bin/bash -c "mysql -u${mysql_user} -p${mysql_passwd} -e 'reset slave;'"
docker exec -it ${node1_name} /bin/bash -c "mysql -u${mysql_user} -p${mysql_passwd} << eof
change master to master_host='${mysql_address}', master_user='${replication_user}', master_password='${mysql_passwd}', master_port=${master_port}, master_log_file='${bin_log}', master_log_pos=${bin_Position}, master_connect_retry=30;
eof"
docker exec -it ${node1_name} /bin/bash -c "mysql -u${mysql_user} -p${mysql_passwd} -e 'start slave;'"
docker exec -it ${node1_name} /bin/bash -c "mysql -u${mysql_user} -p${mysql_passwd} -e 'show slave status \G;'"
echo "配置完毕"

if [ $? -ne 0 ];then
	echo "node1配置错误"
	exit
fi

echo "node2从库配置"
docker exec -it ${node2_name} /bin/bash -c "mysql -u${mysql_user} -p${mysql_passwd} -e 'stop slave;'"
docker exec -it ${node2_name} /bin/bash -c "mysql -u${mysql_user} -p${mysql_passwd} -e 'reset slave;'"
docker exec -it ${node2_name} /bin/bash -c "mysql -u${mysql_user} -p${mysql_passwd} << eof
change master to master_host='${mysql_address}', master_user='${replication_user}', master_password='${mysql_passwd}', master_port=${master_port}, master_log_file='${bin_log}', master_log_pos=${bin_Position}, master_connect_retry=30;
eof"
docker exec -it ${node2_name} /bin/bash -c "mysql -u${mysql_user} -p${mysql_passwd} -e 'start slave;'"
docker exec -it ${node2_name} /bin/bash -c "mysql -u${mysql_user} -p${mysql_passwd} -e 'show slave status \G;'"
echo "配置完毕"

replication.sh文件说明,仅仅是说明

这些是在主mysql外部登录mysql并配置复制用用户slave,刷新权限

docker exec -it ${master_name} /bin/bash -c "mysql -u${mysql_user} -p${mysql_passwd} << eof
grant replication slave on *.* to '${replication_user}'@'%' identified by '${mysql_passwd}';
eof"
docker exec -it ${master_name} /bin/bash -c "mysql -h${mysql_address} -P${master_port} -u${mysql_user} -p${mysql_passwd} -e 'flush privileges;'"

这是获取show master status SQL语句输出的File和Position,并在从库配置主从同步

需要注意的是show master status;查出来的File和Position和以下的配置相同才行

bin_log=`docker exec -it ${master_name} /bin/bash -c "mysql -u${mysql_user} -p${mysql_passwd} -e 'show master status;'" | sed -n '/mysql-bin/p' | awk '{print $2}'`
bin_Position=`docker exec -it ${master_name} /bin/bash -c "mysql -u${mysql_user} -p${mysql_passwd} -e 'show master status;'" | sed -n '/mysql-bin/p' | awk '{print $4}'`
echo "${bin_log}"
echo "${bin_Position}"
+------------------+----------+--------------+--------------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------+-------------------+
| mysql-bin.000005 |      154 |              | mysql,information_schema |                   |
+------------------+----------+--------------+--------------------------+-------------------+
docker exec -it ${node1_name} /bin/bash -c "mysql -u${mysql_user} -p${mysql_passwd} << eof
change master to master_host='${mysql_address}', master_user='${replication_user}', master_password='${mysql_passwd}', master_port=${master_port}, master_log_file='${bin_log}', master_log_pos=${bin_Position}, master_connect_retry=30;
eof"

然后就是开启主从同步

docker exec -it ${node1_name} /bin/bash -c "mysql -u${mysql_user} -p${mysql_passwd} -e 'start slave;'"

4.正确结果

查看从库的状态(show slave status \G;)为如下,即为主从搭建成功

Slave_IO_Running变为Yes
Slave_SQL_Running变为Yes

你可能感兴趣的:(mysql数据库,mysql,数据库,服务器)