一键配置mysql主从

首先配置主服务器

#!/bin/bash
echo "关闭防火墙"
systemctl stop firewalld.service
setenforce 0
yum -y install mariadb mariadb-server &> /dev/null
if [ $? -eq 0 ];then
	echo "mysql安装成功"
else
	echo "mysql安装失败,请重新安装"
	yum -y install mariadb mariadb-server &> /dev/null
fi
sed -i '10aserver-id=1' /etc/my.cnf
sed -i '11alog-bin=mysql-bin' /etc/my.cnf
sed -i '12arelay-log=relays' /etc/my.cnf
systemctl start mariadb
if [ $? -eq 0 ];then
	echo "mysql启动成功"
else
	echo "mysql启动失败,请重启"
	systemctl restart mariadb
fi
mysql -e "grant all on *.* to tom@'%' identified by '123456';"
if [ $? -eq 0 ];then
	echo "授权成功"
else
	echo "授权失败,请重试"
	mysql -e "DROP  USER tom@'%';"
	mysql -e "grant all on *.* to tom@'%' identified by '123456';"
fi
mysql -e "flush privileges;"
mysql -e "show master status;"

然后再进行从服务器的配置

#!/bin/bash
echo "关闭防火墙"
systemctl stop firewalld.service
setenforce 0
yum -y install mariadb mariadb-server &> /dev/null
if [ $? -eq 0 ];then
	echo "mysql安装成功"
else
	echo "mysql安装失败,请重新安装"
	yum -y install mariadb mariadb-server &> /dev/null
fi
sed -i '10aserver-id=2' /etc/my.cnf
sed -i '11alog-bin=mysql-bin' /etc/my.cnf
sed -i '12arelay-log=relays' /etc/my.cnf
systemctl start mariadb
if [ $? -eq 0 ];then
	echo "mysql启动成功"
else
	echo "mysql启动失败,请重启"
	systemctl restart mariadb
fi
mysql -e "stop slave;"
	echo "停止slave"
read -ep "请输入主数据库的ip:" ip
read -ep "请到主数据库查看log_file,并且输入:" file
read -ep "请到主数据库查看pos,并且输入:" pos
mysql -e "change master to master_user='tom', master_host='$ip', master_password='123456', master_log_file='$file', master_log_pos=$pos;"
mysql -e "start slave;"
echo "启动slave"
IO=$(mysql -e "show slave status \G;" | grep Slave_IO_Running | awk '{print $2}')
SQL=$(mysql -e "show slave status \G;" | grep Slave_SQL_Running | awk '{print $2}')
if [ $IO == "Yes" -a $SQL == "Yes" ];then
	echo "主从配置成功"
else
	echo "主从配置失败,请检查配置"
fi

上一篇:MySQL远程连接丢失问题解决方法


下一篇:修改docker中mysql登入密码(包括容器内和本地远程登入的密码)