#!/bin/bash # cluster install Service(Version 1.0) -- by Fei Yi Song. # This script is used for cluster deployment and MySQL dual master configuration # # Debian/Ubuntu Linux systems. (May 10, 2019) # (GNU/General Public License version 2.0) # # For Centos 7 and up, Linux lzone_zkong_cluster_01 3.10.0-862.el7.x86_64. # # ...And away we go! set -xu echo "----------------------------环境变量区域--------------------------" user=$USER host=$HOSTNAME MySQL_Confugure="/usr/local/esl/my.cnf" Auto=`awk -F "-" 'NR==2{print $(NF-1)}' /usr/local/esl/mysql/auto.cnf` MySQL_User="root" MySQL_PASSWD="Zkong_1234" MySQL_Host="127.0.0.1" Mastet_IP="192.168.100.101" #修改IP为Master IP #ChangMaster="192.168.100.103" #ChangMasterSlave="192.168.100.101" Storage=`docker ps|grep esl_storage_1|wc -l` Tracker=`docker ps|grep esl_tracker_1|wc -l` echo "---------------------------函数区域-----------------------------" Install_Mysql() { Mysql_Version=`mysql -V` if [ $? -ne 0 ];then printf "No MySQL command " yum -y install mysql >/dev/null else printf "There are MySQL commands " fi } Check_sshpass() { Command_sshpass=`sshpass -V` if [[ ! $? -eq 0 ]]; then yum -y install sshpass else echo "sshpass命令已安装" fi } echo "---------------------MySQL双主配置-----------------------------" Configure_Mysql() { if [[ ! $(docker ps |grep esl_mysql_1|wc -l) -ne 1 ]]; then echo "----------------------------------------------" echo "- Modify MySQL master database configuration -" echo "----------------------------------------------" eval sed -i '5iserver-id=1' $MySQL_Confugure eval sed -i '6ilog-bin=mysql-bin' $MySQL_Confugure eval sed -i '7iinlog-do-db=demo' $MySQL_Confugure eval sed -i '8ienforce-gtid-consistency' $MySQL_Confugure #Modify main library UUID eval sed -i 's#$Auto#24f3#g' /usr/local/esl/mysql/auto.cnf if [ "$?" == "0" ]; then printf "Master 创建主从同步用户 " read -p "请输入Slave 主机IP: " Slave_IP mysql -u$MySQL_User -p$MySQL_PASSWD -h $MySQL_Host -e "grant replication slave on *.* to 'repl'@'$Slave_IP' identified by 'Zkong_1234';" mysql -u$MySQL_User -p$MySQL_PASSWD -h $MySQL_Host -e "flush privileges;" echo "$Slave_IP" docker restart esl_mysql_1 echo "开始随机等待 1-10 秒..." sleep $[ ( $RANDOM % 10 ) + 1 ] echo "等待后继续" echo "---------------------------------------------" echo "- Slave library create synchronization user -" echo "---------------------------------------------" printf "连接到Slave机器..." read -p "请输入 Slave 主机IP: " Host_IP #Mastet_IP="192.168.100.101" read -p "请输入Slave 服务器密码:" command_pass sshpass -p$command_pass ssh root@$Host_IP "yum -y install mysql" ssh -tt root@$Host_IP << remotessh #Modify MySQL master database configuration eval sed -i '5iserver-id=2' $MySQL_Confugure eval sed -i '6ilog-bin=mysql-bin' $MySQL_Confugure eval sed -i '7iinlog-do-db=demo' $MySQL_Confugure eval sed -i '8ienforce-gtid-consistency' $MySQL_Confugure #Slave library create synchronization user mysql -u$MySQL_User -p$MySQL_PASSWD -h $MySQL_Host -e "grant replication slave on *.* to 'repl'@'$Mastet_IP' identified by 'Zkong_1234';" mysql -u$MySQL_User -p$MySQL_PASSWD -h $MySQL_Host -e "flush privileges;" { docker restart esl_mysql_1;exit; } remotessh else exit -1 fi else { printf "MySQL No Running! ";exit -1;} fi } Command() { read -p "请输入Master IP:" ChangMaster read -p "请输入Slave IP:" ChangMasterSlave Master_File=`mysql -u$MySQL_User -p$MySQL_PASSWD -h$MySQL_Host -e "show master status;"|awk '/mysql-bin/{print $1}'` Master_Position=`mysql -u$MySQL_User -p$MySQL_PASSWD -h$MySQL_Host -e "show master status;"|awk '/mysql-bin/{print $2}'` echo Mastart:$Master_File Master_Position:$Master_Position Slave_File=`mysql -u$MySQL_User -p$MySQL_PASSWD -h$ChangMaster -e "show master status;"|awk '/mysql-bin/{print $1}'` Slave_Position=`mysql -u$MySQL_User -p$MySQL_PASSWD -h$ChangMaster -e "show master status;"|awk '/mysql-bin/{print $2}'` echo Slave_file:$Slave_File Slave_Position:$Slave_Position Master_command=`mysql -u$MySQL_User -p$MySQL_PASSWD -h$ChangMasterSlave -e "change master to master_host='$ChangMaster',master_port=3306,master_user='repl',master_password='Zkong_1234',master_log_file='$Slave_File',master_log_pos=$Slave_Position;"` Master_command_start=`mysql -u$MySQL_User -p$MySQL_PASSWD -h$ChangMasterSlave -e "start slave;"` Slave_command=`mysql -u$MySQL_User -p$MySQL_PASSWD -h$ChangMaster -e "change master to master_host='$ChangMasterSlave',master_port=3306,master_user='repl',master_password='Zkong_1234',master_log_file='$Master_File',master_log_pos=$Master_Position;"` slave_command_start=`mysql -u$MySQL_User -p$MySQL_PASSWD -h$ChangMaster -e "start slave;"` } Check_Master() { Slave_IO_Running=`mysql -u$MySQL_User -p$MySQL_PASSWD -h$MySQL_Host -e 'show slave statusG'|egrep 'Slave_IO_Running'|awk '{print $2}'` Slave_SQL_Running=`mysql -u$MySQL_User -p$MySQL_PASSWD -h$MySQL_Host -e 'show slave statusG'|egrep 'Slave_SQL_Running'|awk 'NR==1{print $2}'` #检查MySQL 主库状态 if [ "$Slave_IO_Running" = "Yes" ] && [ "$Slave_SQL_Running" = "Yes" ]; then ( set -x printf "Master and Slave Status YES." ) else ( set -x printf "MySQL Master and Slave ERROR!" ) fi } echo "------------------------------EMQX-Master 配置-------------------------------" EMQX_Configure_Master(){ emqx_config="/usr/local/esl/emqtt/etc/emqx.conf" read -p "Please enter the emqx node1 IP:" Node_name1 read -p "Please enter the emqx node2 IP:" Node_name2 sed 's#cluster.discovery = manual#cluster.discovery = static#g' $emqx_config -i sed "s#node.name = emqx@127.0.0.1#node.name = emqx1@$Node_name1#g" $emqx_config -i sed "52 icluster.static.seeds= emqx1@$Node_name1,emqx2@$Node_name2" $emqx_config -i echo "--------------------------------------" echo "- 正在重启$Node_name1 emqtt... -" echo "--------------------------------------" docker restart esl_emqtt_1 } echo "-------------------------EMQX-Slave 配置--------------------------------" EMQX_Configure_Slave(){ emqx_config="/usr/local/esl/emqtt/etc/emqx.conf" read -p "Please enter the emqx node2 IP:" Node_name2 read -p "请输入EMQX node2 服务器密码: " EMQX_Password read -p "Please enter the emqx node1 IP:" Node_name1 sshpass -p$EMQX_Password ssh root@$Node_name2 "sed 's#cluster.discovery = manual#cluster.discovery = static#g' $emqx_config -i" sshpass -p$EMQX_Password ssh root@$Node_name2 "sed 's#node.name = emqx@127.0.0.1#node.name = emqx2@$Node_name2#g' $emqx_config -i" sshpass -p$EMQX_Password ssh root@$Node_name2 "sed '52 icluster.static.seeds= emqx1@$Node_name2,emqx2@$Node_name1' $emqx_config -i" echo "--------------------------------------" echo "- 正在重启$Node_name1 emqtt... -" echo "--------------------------------------" docker restart esl_emqtt_1 printf "开始随机等待1-10秒..." sleep $[ ( $RANDOM % 10 ) + 1 ] echo "等待后执行..." echo "-------------------------------------" echo "- EMQX集群配置 -" echo "-------------------------------------" ssh -tt root@$Node_name2 << EOF docker exec esl_emqtt_1 bash { cd /usr/local/emqx/bin >/dev/null 2>&1;./emqx_ctl cluster join emqx1@$Node_name1 >/dev/null 2>&1;exit -1;docker restart esl_emqtt_1; } echo "-----------------------------------------" echo "- 检查EMQX 集群状态 -" echo "-----------------------------------------" exit EOF exit } echo "---------------------------------------------FastDFS集群配置区域------------------------------------" Fastdfs_Master(){ if [ "$Tracker" = "1" ] && [ "$Storage" = "1" ]; then ( set -x printf "Running." { docker rm -f esl_storage_1;docker rm -f esl_tracker_1; } ) else ( set -x echo "The storage tacker container does not exist" ) fi } Fastdfs_Slave(){ read -p "请输入Slave IP:" fast_IP read -p "请输入Slave 服务器密码:" PassWord sshpass -p$PassWord ssh root@$fast_IP 'docker rm -f esl_tracker_1 && docker rm -f esl_storage_1' } Fastdfs_cluster(){ Tracker_Path="/usr/local/esl/fastdfs/tracker/" Storage_Path="/usr/local/esl/fastdfs/storage/" Trakcer_Base_Path="/export/fastdfs/tracker" Storage_Base_Path="/export/fastdfs/storage" if [ ! -d "$Tracker_Path" ] && [ ! -d "$Storage_Path" ] ;then mkdir -p $Tracker_Path mkdir -p $Storage_Path else echo "目录已存在" fi read -p "请输入Tracker Server01 IP:" Tracker_Server01 read -p "请输入Tracker Server02 IP:" Tracker_Server02 docker run -dti -e TZ="Asia/Shanghai" --network=host --name esl_tracker_1 --restart=always -v $Tracker_Path:/export/fastdfs/tracker -e TRACKER_BASE_PATH=$Trakcer_Base_Path -e TRACKER_PORT=8300 gzlj2018/fastdfs:5.11 sh /usr/local/src/tracker.sh docker run -dti -e TZ="Asia/Shanghai" --network=host --name esl_storage_1 --restart=always -v $Storage_Path:/export/fastdfs/storage -e STORAGE_PORT=8400 -e STORAGE_BASE_PATH=$Storage_Base_Path -e STORAGE_PATH0=$Storage_Base_Path -e TRACKER_SERVER=$Tracker_Server01:8300,$Tracker_Server02:8300 -e GROUP_COUNT=1 -e HTTP_SERVER_PORT=8410 -e GROUP_NAME=group1 gzlj2018/fastdfs:5.11 sh /usr/local/src/storage.sh read -p "请输出Slave IP:" Create_dir read -p "请输入Slave 服务器密码: " Slave_Password sshpass -p$Slave_Password ssh root@$Create_dir "[ -d $Storage_Path ] && echo "目录存在" || echo "目录不存在" && mkdir -p $Storage_Path" sshpass -p$Slave_Password ssh root@$Create_dir "[ -d $Tracker_Path ] && echo "目录存在" || echo "目录不存在" && mkdir -p $Tracker_Path" sshpass -p$Slave_Password ssh root@$Create_dir "docker run -dti -e TZ="Asia/Shanghai" --network=host --name esl_tracker_1 --restart=always -v $Tracker_Path:/export/fastdfs/tracker -e TRACKER_BASE_PATH=$Trakcer_Base_Path -e TRACKER_PORT=8300 gzlj2018/fastdfs:5.11 sh /usr/local/src/tracker.sh" sshpass -p$Slave_Password ssh root@$fast_IP "docker run -dti -e TZ="Asia/Shanghai" --network=host --name esl_storage_1 --restart=always -v $Storage_Path:/export/fastdfs/storage -e STORAGE_PORT=8400 -e STORAGE_BASE_PATH=$Storage_Base_Path -e STORAGE_PATH0=$Storage_Base_Path -e TRACKER_SERVER=$Tracker_Server02:8300,$Tracker_Server01:8300 -e GROUP_COUNT=1 -e HTTP_SERVER_PORT=8410 -e GROUP_NAME=group1 gzlj2018/fastdfs:5.11 sh /usr/local/src/storage.sh" if [ $? = "0" ];then echo "Fastdfs slave create tracker Server" exit 1 fi } echo "--------------------------------------使用帮助区域----------------------------------------" help(){ # Print help information using less utility: less << _EOF_ GNU bash,版本 4.2.46(2)-release-(x86_64-redhat-linux-gnu) Press "q" to exit this Help page. Note: run test environment CentOS 7.6 Writing time September 19, 2021 Script version v1.0 Functions include MySQL dual master environment, emqx cluster environment and fastdfs cluster environment Usage: sh [file] input: -m, --mysql 安装MySQL双主环境 -e, --emqx 安装EMQX集群环境 -f, --fastdfs 安装Storage、Tracker集群环境 -h, --help 使用帮助 请输入 sh file scripts 以获得关于 Shell 选项的更多操作信息 _EOF_ } echo "----------------------------------------执行函数区域------------------------------" Read_Input() { cat <<EOF echo "-------------------------------------------------" echo "- h | --help 安装帮助 -" echo "- m | --mysql 安装MySQL 双主环境 -" echo "- e | --emqx 安装EMQX 集群环境 -" echo "- f | --fastdfs 安装EMQX 集群环境 -" echo "-------------------------------------------------" EOF read -p "请选择安装服务 :" Server } Show_Status () { case $Server in m | --mysql) Check_sshpass echo "安装MySQL服务..." set -x Install_Mysql Configure_Mysql Command printf "开始随机等待1-10秒..." sleep $[ ( $RANDOM % 10 ) + 1 ] echo "等待后执行检查双主状态..." Check_Master ;; e | --emqx) Check_sshpass echo "配置emqx 集群" set -x EMQX_Configure_Master printf "开始随机等待1-10秒..." sleep $[ ( $RANDOM % 10 ) + 1 ] echo "等待后执行..." EMQX_Configure_Slave ;; f | --fastdfs) Fastdfs_Master Fastdfs_Slave Fastdfs_cluster ;; h | --help) Check_sshpass echo "help帮助手册" help ;; *) echo "参考安装手册" exit 1 ;; esac } #执行函数 main(){ Read_Input Show_Status } main