🗼😀本站将逐步迁移至新站点,点击访问 -->

[SHELL脚本练习]MySQL主从同步配置脚本

8,646次阅读
没有评论
#!/bin/bash

MYSQL=/usr/bin/mysql
MASTER_LOG_FILE="mysql-bin.000002"
MASTER_LOG_POS="154"
MYSQL_PASS="MDV6LnHh."

if [ ! -e $MYSQL ] || [ ! -x $MYSQL ];then
	echo " mysql binary does't exist" ; echo " mysql binary does't exist"
	exit 1
fi
read -p "Please Input Master IP address: " MASTER_IP

if ! echo "" | nc -v -w 3 $MASTER_IP 3306 &> /dev/null;then
	echo "Error : Connect to $MASTER_IP 3306 (tcp) failed: Connection refused"
	exit 1
fi
if cat /anchnet/passwd.txt | grep "Slave Status:OK" &> /dev/null ;then
   echo "Error : Slave already exists !!!"
   exit 1
fi


${MYSQL} -p${MYSQL_PASS} -e "show databases;" --connect-expired-password &> /dev/null
if [ $? != 0 ];then
	read -p "Please Input MySQL Password: " MYSQL_PASS
fi
read -p "Please Input User Repl Password: " REPL_PASS
${MYSQL} -h${MASTER_IP} -urepl -p${REPL_PASS} -e "show databases;" --connect-expired-password &> /dev/null
if [ $? != 0 ];then
   echo ""
   echo "Error : Master repl password error! To the master see /anchnet/passwd.txt"
   exit 1
fi

REPL_SQL="CHANGE MASTER TO MASTER_HOST=\"$MASTER_IP\", MASTER_USER=\"repl\", MASTER_PASSWORD=\"$REPL_PASS\",MASTER_LOG_FILE=\"$MASTER_LOG_FILE\",MASTER_LOG_POS=$MASTER_LOG_POS;"
${MYSQL} -p${MYSQL_PASS} -e "$REPL_SQL" --connect-expired-password
	if [ $? = 0 ];then
		${MYSQL} -p${MYSQL_PASS} -e "start slave;" --connect-expired-password
		echo "Slave create successed"
                echo "Slave Status:OK"  >> /anchnet/passwd.txt
	else 
		echo "Slave create failed"
	fi

 

Honest1y
版权声明:本站原创文章,由Honest1y于2017年08月08日发表,共计1619字。
转载提示:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)