#!/bin/bash
function menu() {
clear
echo -e "\033[1;3;34m搭建MySQL主从复制比较复杂,请按顺序执行操作,否则可能会失败呢...\033[0m\n"
echo -e "\t\t\033[43mMySQL主从复制搭建\033[0m"
echo -e "\t\t\033[32m0、Exit menu\033[0m"
echo -e "\t\t\033[32m1、搭建主服务器\033[0m"
echo -e "\t\t\033[32m2、进入主服务器\033[0m"
echo -e "\t\t\033[32m3、搭建从服务器\033[0m"
echo -e "\t\t\033[32m4、进入从服务器\033[0m"
echo -e "\t\t\033[32m5、搭建主服务器\033[0m\n"
echo -en "\033[46;37mEnter option:\033[0m\t"
read -n 1 option
}
menu
MASTER_ADDR='/data/volume/ms_mysql/mysql-master'
MASTER_NAME='mysql-master'
PWD_ADDR='MYSQL_ROOT_PASSWORD'
MASTER_PWD='root'
MASTER_PORT='3307'
M_VERSION='mysql:5.7'
function master_command() {
docker run -p ${MASTER_PORT}:3306 --name ${MASTER_NAME} --privileged=true -v ${MASTER_ADDR}/log:/var/log/mysql -v ${MASTER_ADDR}/data:/var/lib/mysql -v ${MASTER_ADDR}/conf:/etc/mysql -e ${PWD_ADDR}=${MASTER_PWD} -d ${M_VERSION}
}
function master_conf() {
cd ${MASTER_ADDR}/conf
touch my.cnf
echo '[mysqld]' >> my.cnf
echo '# 设置server_id,同一局域网中需要唯一' >> my.cnf
echo 'server_id=101' >> my.cnf
echo '# 指定不需要同步的数据库名称' >> my.cnf
echo 'binlog-ignore-db=mysql' >> my.cnf
echo '# 开启二进制日志功能' >> my.cnf
echo 'log-bin=mall-mysql-bin' >> my.cnf
echo '# 设置二进制日志使用内存大小(事务)' >> my.cnf
echo 'binlog_cache_size=1M' >> my.cnf
echo '# 设置二进制日志格式(max,statement,row)' >> my.cnf
echo 'binlog_format=mixed' >> my.cnf
echo '# 二进制日志过期清理时间,默认为0,表示不自动清理' >> my.cnf
echo 'expire_logs_days=7' >> my.cnf
echo '# 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断' >> my.cnf
echo '# 如:1062错误是指一些主键重复,1032错误是因为主从数据库不一致' >> my.cnf
echo 'slave_skip_errors=1062' >> my.cnf
}
IS_MYSQL=`docker images | grep mysql | grep 5.7`
function master() {
if [[ ${IS_MYSQL} ]];
then
master_command
master_conf
else
docker pull mysql:5.7
master_command
master_conf
fi
}
function exec_master() {
docker exec -it ${MASTER_NAME} env LANG=C.UTF-8 /bin/bash
}
SLAVE_ADDR='/data/volume/ms_mysql/mysql-slave'
SLAVE_NAME='mysql-slave'
PWD_ADDR='MYSQL_ROOT_PASSWORD'
SLAVE_PWD='root'
SLAVE_PORT='3308'
M_VERSION='mysql:5.7'
function slave_command() {
docker run -p ${SLAVE_PORT}:3306 --name ${SLAVE_NAME} --privileged=true -v ${SLAVE_ADDR}/log:/var/log/mysql -v ${SLAVE_ADDR}/data:/var/lib/mysql -v ${SLAVE_ADDR}/conf:/etc/mysql -e ${PWD_ADDR}=${SLAVE_PWD} -d ${M_VERSION}
}
function slave_conf() {
cd ${SLAVE_ADDR}/conf
touch my.cnf
echo '[mysqld]' >> my.cnf
echo '# 设置server_id,同一局域网中需要唯一' >> my.cnf
echo 'server_id=102' >> my.cnf
echo '# 指定不需要同步的数据库名称' >> my.cnf
echo 'binlog-ignore-db=mysql' >> my.cnf
echo '# 开启二进制日志功能' >> my.cnf
echo 'log-bin=mall-mysql-slave1-bin' >> my.cnf
echo '# 设置二进制日志使用内存大小(事务)' >> my.cnf
echo 'binlog_cache_size=1M' >> my.cnf
echo '# 设置二进制日志格式(max,statement,row)' >> my.cnf
echo 'binlog_format=mixed' >> my.cnf
echo '# 二进制日志过期清理时间,默认为0,表示不自动清理' >> my.cnf
echo 'expire_logs_days=7' >> my.cnf
echo '# 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断' >> my.cnf
echo '# 如:1062错误是指一些主键重复,1032错误是因为主从数据库不一致' >> my.cnf
echo 'slave_skip_errors=1062' >> my.cnf
echo '# relay_log配置中继日志' >> my.cnf
echo 'relay_log=mail-mysql-relay-bin' >> my.cnf
echo '# log_slave_updates表示slave将复制事件写进自己的二进制文件' >> my.cnf
echo 'log_slave_updates=1' >> my.cnf
echo '# slave设置为只读(具有super权限的用户除外)' >> my.cnf
echo 'read_only=1' >> my.cnf
}
function slave() {
if [[ ${IS_MYSQL} ]];
then
slave_command
slave_conf
else
docker pull mysql:5.7
slave_command
slave_conf
fi
}
function exec_slave() {
docker exec -it ${SLAVE_NAME} env LANG=C.UTF-8 /bin/bash
}
HOST_IP=`ifconfig ens33 | awk 'NR==2{print $2}'`
case $option in
0)
echo -e "\n\n\n\n\033[3;1;35m已退出!!!\033[0m\n\n"
exit;;
1)
echo ''
master
docker restart ${MASTER_NAME}
echo '';;
2)
echo -e "\n\033[1;3;36m创建slave数据同步用户【已创建请忽略】\033[0m\n"
echo -e "\033[32m查看数据库中有哪些用户:SELECT user, host FROM mysql.user;\033[0m"
echo -e "\033[32mcreate user 'slave'@'%' identified by '123456';\033[0m"
echo -e "\033[32mgrant replication slave, replication client on *.* to 'slave'@'%';\033[0m\n"
echo -e "\033[32m搭建完slave之后,可使用show master status;查看master状态\033[0m\n"
exec_master
echo '';;
3)
echo ''
slave
docker restart ${SLAVE_NAME}
echo '';;
4)
echo -e "\n\033[1;3;36m配置主从复制【已配置请忽略】\033[0m\n"
echo -e "\033[32mmaster_log_file和master_log_pos的值,以master上,show master status出来的为准\033[0m\n"
echo -e "\033[32mchange master to master_host='${HOST_IP}', master_user='slave', master_password='123456', master_port=3307, master_log_file='mail-mysql-bin.00006', master_log_pos=1287, master_connect_retry=30;\033[0m\n"
echo -e "\033[32m查看状态:show slave status \G;\n"
echo -e "\033[32m开启主从同步:start slave;\n"
exec_slave
echo -e '\n';;
*)
clear
echo -e "\n\n\033[3;1;35m键入参数有误,GoodBey!!!\033[0m\n\n";;
esac
|