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

732次阅读

共计 1248 个字符,预计需要花费 4 分钟才能阅读完成。

#!/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

 

正文完
 
mervinwang
版权声明:本站原创文章,由 mervinwang 2017-08-08发表,共计1248字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
文章搜索