#!/bin/bash
#
#*****************************************************
#File Name    : create_mysql5.7cluster.sh
#Author       : guokai
#Mail         : xxx@163.com
#Create Time  : 2022-05-07 14:01:20
#Description  : The test script
#*****************************************************

#cat /etc/ssh/sshd_config.d/01-permitrootlogin.conf请修改允许root登录
read -p "请输入从节点的ip：" SLAVE_IP
MASTER_IP=`ifconfig eth0 | awk '/inet /{print $2}'`
PASSWORD=cqt!010@Mysql
PORT=3300
#SERVER_ID=`ifconfig eth0 | awk '/inet /{print $2}' |awk -F "." '{print $NF}'`
SERVER_ID=`echo $SLAVE_IP | awk -F "." '{print $NF}'`
ROOT_PASSWORD=cqt@2020
SSH_PORT=56022

color () {
    RES_COL=60
    MOVE_TO_COL="echo -en \\033[${RES_COL}G"
    SETCOLOR_SUCCESS="echo -en \\033[1;32m"
    SETCOLOR_FAILURE="echo -en \\033[1;31m"
    SETCOLOR_WARNING="echo -en \\033[1;33m"
    SETCOLOR_NORMAL="echo -en \E[0m"
    echo -n "$2" && $MOVE_TO_COL
    echo -n "["
    if [ $1 = "success" -o $1 = "0" ] ;then ${SETCOLOR_SUCCESS}
        echo -n $"  OK  "
    elif [ $1 = "failure" -o $1 = "1" ] ;then ${SETCOLOR_FAILURE}
        echo -n $"FAILED"
    else
        ${SETCOLOR_WARNING}
        echo -n $"WARNING"
    fi
    ${SETCOLOR_NORMAL}
    echo -n "]"
    echo
}

color 0 "主节点的IP为: $MASTER_IP"
color 0 "从节点的IP为: $SLAVE_IP"

nopasswd () {
	color 0 "开始安装ansible，并配置和从节点使用key验证交互"
	#yum install -y epel-release
	dnf install -y ansible-core
cat >> /etc/ansible/hosts <<EOF
[mysql]
$MASTER_IP ansible_ssh_port=$SSH_PORT
$SLAVE_IP  ansible_ssh_port=$SSH_PORT
EOF
cat > /root/grant.sh << EOF
#!/bin/bash
mysql -uroot -p$PASSWORD -e "grant all privileges on *.* to root@'%' identified by '$PASSWORD' with grant option;flush privileges"
EOF
chmod 777 /root/grant.sh

	ssh-keygen -f /root/.ssh/id_rsa -N ""
	sshpass -p $ROOT_PASSWORD ssh-copy-id -p $SSH_PORT  -o StrictHostKeyChecking=no root@$SLAVE_IP

	color 0 "从节点开始修改mysql密码并授权"
	#ssh $SLAVE_IP yum install -y ansible
	#ansible $SLAVE_IP -m shell -a "echo '[mysql]' >> /etc/ansible/hosts"
	#ansible $SLAVE_IP -m shell -a "echo '$MASTER_IP' >> /etc/ansible/hosts"
	#ansible $SLAVE_IP -m shell -a "echo '$SLAVE_IP' >> /etc/ansible/hosts"
	ansible $SLAVE_IP -m shell  -a "mysqladmin -uroot password $PASSWORD"	
	ansible $SLAVE_IP -m script  -a "/root/grant.sh"
	ansible $SLAVE_IP -m shell  -a "mysql -uroot -p$PASSWORD -e 'select version();' 2> /dev/null "	&&  color 0 "从节点密码修改成功 $PASSWORD"
	ansible $SLAVE_IP -m shell  -a "mysql -uroot -p$PASSWORD -e 'SET GLOBAL server_id=$SERVER_ID;'"
}

master () {
	mysqladmin -uroot password $PASSWORD
	#mysql -uroot -p$PASSWORD -e "grant all privileges on *.* to root@'%' identified by $PASSWORD;flush privileges"
	color 0 "修改主从服务器的root密码为: $PASSWORD,并授权了远程登录"

	#sed -i "/server-id/c server-id       =$SERVER_ID"  /etc/my.cnf
	ansible $SLAVE_IP -m shell -a "sed -i '/server-id/c server-id       =$SERVER_ID'  /etc/my.cnf"

	color 0 "开始创建复制用户并授权"
	REPLICATE_USER=repl
	REPLICATE_PASSWORD=123456
	mysql -uroot -p$PASSWORD  -e "grant replication slave on *.* to $REPLICATE_USER@'%' identified by '$REPLICATE_PASSWORD';flush privileges"
	mysql -uroot -p$PASSWORD -h $SLAVE_IP -e "grant replication slave on *.* to $REPLICATE_USER@'%' identified by '$REPLICATE_PASSWORD';flush privileges"

	MASTER_LOG_FILE=`mysql -uroot -p$PASSWORD  -e 'show master status;' 2> /dev/null | grep mysql | awk '{print $1}'` && color 0 "MASTER_LOG_FILE=$MASTER_LOG_FILE"
	MASTER_LOG_POS=`mysql -uroot -p$PASSWORD  -e 'show master status;' 2> /dev/null | grep mysql | awk '{print $2}'` && color 0 "MASTER_LOG_POS=$MASTER_LOG_POS"
	MASTER_HOST=$MASTER_IP && color 0 "MASTER_HOST=$MASTER_HOST" 
	MASTER_PORT=$PORT && color 0 "MASTER_PORT=$MASTER_PORT"
	MASTER_USER=$REPLICATE_USER && color 0 "MASTER_USER=$MASTER_USER"
	MASTER_PASSWORD=$REPLICATE_PASSWORD && color 0 "MASTER_PASSWORD=$MASTER_PASSWORD"
	MASTER_CONNECT_RETRY=10 && color 0 "MASTER_CONNECT_RETRY=$MASTER_CONNECT_RETRY"
	
	color 0 "开始在从服务器上添加主节点"
	mysql -uroot -p$PASSWORD -h $SLAVE_IP -e "CHANGE MASTER TO MASTER_HOST='$MASTER_IP',MASTER_USER='$REPLICATE_USER',MASTER_PASSWORD='$REPLICATE_PASSWORD',MASTER_PORT=$MASTER_PORT,MASTER_LOG_FILE='$MASTER_LOG_FILE',MASTER_LOG_POS=$MASTER_LOG_POS,MASTER_CONNECT_RETRY=$MASTER_CONNECT_RETRY;" && color 0 "$SLAVE_IP 的主节点添加成功"
	mysql -uroot -p$PASSWORD -h $SLAVE_IP -e "start slave;"
	color 0 "在从服务器上看到的同步状态为："
	mysql -uroot -p$PASSWORD -h $SLAVE_IP -e "show slave status\G"
	ansible $SLAVE_IP -m shell  -a "systemctl restart mysqld"
}
nopasswd
master
