需要两台独立服务器,如果在一台服务器上运行,注意修改脚本中的端口映射避免端口冲突,修改挂载目录。如遇imagePullEerror问题,修改脚本中镜像源。
#!/bin/bash
read -p "请输入主数据库所在服务器的ip: " MASTER_IP
read -p "请输入主数据库所在服务器的密码: " MASTER_PASSWD
read -p "请输入从数据库所在服务器的ip: " SLAVE_IP
read -p "请输入从数据库所在服务器的密码: " SLAVE_PASSWD
function docker_install(){
docker --version &> /dev/null
if [ $? -eq 0 ]; then
echo "master node Docker 已安装。"
else
echo "--------------mysql主服务器上Docker 未安装, 正在安装中...--------------"
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
yum makecache fast
yum -y install docker-ce
mkdir -p /etc/docker
echo '{
"registry-mirrors": ["https://dockerhub.icu"]
}' > /etc/docker/daemon.json
systemctl start docker
systemctl enable docker
echo "Docker 安装完成并已启动。"
fi
ssh -Tq root@$SLAVE_IP << EOF
docker --version &> /dev/null
if command -v docker >/dev/null 2>&1; then
echo "slave node Docker 已安装。"
else
echo "--------------mysql从服务器上Docker 未安装, 正在安装中...--------------"
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
yum makecache fast
yum -y install docker-ce
mkdir -p /etc/docker
echo '{
"registry-mirrors": ["https://dockerhub.icu"]
}' > /etc/docker/daemon.json
systemctl start docker
systemctl enable docker
echo "Docker 安装完成并已启动。"
fi
EOF
}
function ssh-key(){
yum install expect -y
expect <<EOF
spawn ssh-keygen -t rsa -b 2048 -N "" -f $HOME/.ssh/id_rsa
expect {
"Overwrite (y/n)?" {
send "y\r"
expect eof
}
eof
}
EOF
}
function ssh-copy(){
expect <<EOF
spawn ssh-copy-id -i $HOME/.ssh/id_rsa.pub root@$SLAVE_IP
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$SLAVE_PASSWD\n" }
}
expect "password" { send "PASSWD\n" }
EOF
}
function mysql_install_master(){
mkdir -p /root/data/mysql/{data,etc}
cat << EOF >> /root/data/mysql/etc/my.cnf
[mysqld]
secure-file-priv=NULL
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
pid-file=/var/run/mysqld/mysqld.pid
#binlog-ignore-db binlog-do-db
server-id=1
read-only=0
[client]
socket=/var/lib/mysql/mysql.sock
EOF
docker pull mysql:8.0
docker run --name mysql-master -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -v /root/data/mysql/data:/var/lib/mysql -v /root/data/mysql/etc/my.cnf:/etc/my.cnf -p 3306:3306 -d mysql:8.0
while ! timeout 2 bash -c "cat < /dev/null > /dev/tcp/$MASTER_IP/3306"
do
echo "等待端口3306 在 mysql主服务器上打开..."
sleep 5
done
echo "端口 3306 已经在mysql主服务器上打开。"
}
function mysql_install_slave(){
ssh root@$SLAVE_IP "mkdir -p /root/data/mysql/{data,etc}"
scp /root/data/mysql/etc/my.cnf root@$SLAVE_IP:/root/data/mysql/etc/my.cnf
ssh root@$SLAVE_IP "sed -i -e 's/server-id=1/server-id=2/' -e 's/read-only=0/read-only=1/' /root/data/mysql/etc/my.cnf"
ssh root@$SLAVE_IP "docker pull mysql:8.0"
ssh root@$SLAVE_IP "docker run --name mysql-slave -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -v /root/data/mysql/data:/var/lib/mysql -v /root/data/mysql/etc/my.cnf:/etc/my.cnf -p 3306:3306 -d mysql:8.0"
ssh root@$SLAVE_IP "while ! timeout 2 bash -c 'cat < /dev/null > /dev/tcp/$SLAVE_IP/3307'
do
echo 等待端口3306在从mysql服务器上上打开...
sleep 5
done
echo 端口3306已经在mysql从服务器上打开"
}
function my_copy(){
docker exec mysql-master mysql -uroot -e "CREATE USER 'replica'@'%' IDENTIFIED WITH mysql_native_password AS ''; GRANT REPLICATION SLAVE ON *.* TO 'replica'@'%'; FLUSH PRIVILEGES;"
BIN_LOG=$(docker exec mysql-master mysql -uroot -e "show master status" | grep binlog | awk '{print $1}')
POSITION=$(docker exec mysql-master mysql -uroot -e "show master status" | grep binlog | awk '{print $2}')
ssh -Tq root@$SLAVE_IP <<EOF
echo "Connecting to remote server"
docker exec mysql-slave mysql -uroot -e "STOP SLAVE;"
docker exec mysql-slave mysql -uroot -e "CHANGE MASTER TO MASTER_HOST='$MASTER_IP', MASTER_USER='replica', MASTER_PASSWORD='', MASTER_LOG_FILE='$BIN_LOG', MASTER_LOG_POS=$POSITION;"
docker exec mysql-slave mysql -uroot -e "START SLAVE;"
EOF
}
echo "----------------------------开始执行密钥生成-------------------------------------------------"
ssh-key
echo "-----------------------------------密钥添加成功!!!----------------------"
echo "-----------------------------------开始进行密钥添加----------------------"
ssh-copy
echo "-----------------------------------密钥添加成功!!!----------------------"
echo "-----------------------------开始检测是否需要安装docker------------------------"
docker_install
echo "-------------------------------开始为本地安装mysql-master容器--------------------------"
mysql_install_master
echo "-------------------------------本地安装mysql-master容器完成!!!--------------------------"
echo "-------------------------------开始安装mysql-slave容器--------------------------"
mysql_install_slave
echo "-------------------------------安装mysql-master容器完成!!!--------------------------"
echo "-------------------------------开始进行主从复制-----------------------------"
my_copy
echo "----------------------------------主从复制完成!!!----------------------------"