添加重启后执行任务
crontab -e
加入
@reboot nohup sh /root/scripts/init.sh &
编辑init.sh内容
#!/bin/bash
#导入jre必须手动导入 不然启动不了(巨坑)
export JAVA_HOME=/app/jdk1.8.0_211
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
echo 'init...'
systemctl stop firewalld.service
sleep 5
nohup sh /root/scripts/startup.sh &
sleep 5
nohup sh /root/scripts/startupApollo.sh>>/root/scripts/apollo.log &
echo 'init success'
startup.init控制mysql和主主模式配置并监控mysql服务
#author by xiaokai 2019-07-31
#启动另外一个脚本
#count=`ps -ef | grep demo1.sh | grep -v "grep" | wc -l`
#echo $count
#sh demo1.sh>demo1.log &
while true
do
sleep 5
count=`ps -ef | grep mysql | grep -v "grep" | wc -l`
#echo $count
if [ 0 == $count ]; then
#进程不存在
time2=$(date "+%Y-%m-%d %H:%M:%S")
# echo "mysql服务宕机,正在重新启动"
echo 'mysql服务宕机,宕机时间为:'$time2>>error.log
sleep 2
service mysqld restart
sleep 15
sh setMysql.sh
sleep 5
echo 'mysql重新启动成功!!!'
#else
#rest进程存在,杀掉进程
# echo "mysql服务运行正常...持续监控中"
fi
# time=$(date "+%Y-%m-%d %H:%M:%S")
# echo "本轮监测完毕,当前时间为:"$time
done
startupApollo.sh 开机启动apollo并监控apollo服务(支持自动重启)
#!/bin/bash
#author by xiaokai 2019-07-31
#自动重启apollo服务脚本
#count=`ps -ef | grep demo1.sh | grep -v "grep" | wc -l`
#echo $count
#sh demo1.sh>demo1.log &
export JAVA_HOME=/app/jdk1.8.0_211
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
while true
do
sleep 5
count=`ps -ef | grep 10010 | grep -v "grep" | wc -l`
#echo $count
if [ 0 == $count ]; then
#进程不存在
time2=$(date "+%Y-%m-%d %H:%M:%S")
# echo "mysql服务宕机,正在重新启动"
echo 'apolloadmin服务宕机,宕机时间为:'$time2>>error.log
sleep 5
sh /app/apolloadmin/scripts/startup.sh>>apollo.log
sleep 5
echo 'apolloadmin重新启动成功!!!'
fi
#检测apolloconfig进程
sleep 1
count2=`ps -ef | grep 10020 | grep -v "grep" | wc -l`
#echo $count
if [ 0 == $count2 ]; then
#进程不存在
time2=$(date "+%Y-%m-%d %H:%M:%S")
echo 'apolloconfig服务宕机,宕机时间为:'$time2>>error.log
sleep 5
sh /app/apolloconfig/scripts/startup.sh>>apollo.log
sleep 5
echo 'apolloconfig重新启动成功!!!'
fi
#检测apollopor进程
sleep 1
count3=`ps -ef | grep 10030 | grep -v "grep" | wc -l`
#echo $count
if [ 0 == $count3 ]; then
#进程不存在
time2=$(date "+%Y-%m-%d %H:%M:%S")
# echo "mysql服务宕机,正在重新启动"
echo 'apolloportal服务宕机,宕机时间为:'$time2>>error.log
sleep 5
sh /app/apolloportal/scripts/startup.sh>>apollo.log
sleep 5
echo 'apolloportal重新启动成功!!!'
fi
#检测haproxy进程
count3=`ps -ef | grep haproxy | grep -v "grep" | wc -l`
#echo $count
if [ 0 == $count3 ]; then
#进程不存在
time2=$(date "+%Y-%m-%d %H:%M:%S")
# echo "haproxy服务宕机,正在重新启动"
echo 'haproxy服务宕机,宕机时间为:'$time2>>error.log
sleep 2
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cnf
sleep 5
echo 'haproxy重新启动成功!!!'
fi
done
setmysql.sh 配置mysql 主主模式脚本
#!/bin/bash
#author by xiaokai 2019-07-29
#-*- coding:utf-8 -*-
#重启数据库
#echo 'restart mysql1 node...'
#docker restart mysql1
#echo 'restart mysql2 node...'
#docker restart mysql2
echo -e "\e[1;34m connect mysql node1... \n"
echo 'connect mysql node1...'
#数据库ip
HOSTNAME1="172.26.2.114"
#数据库端口
PORT1="3306"
#数据库用户名
USERNAME1="xiaokai"
#数据库密码
PASSWORD1="123456"
#数据库名
DBNAME1="mysql"
#查询语句
select_sql="show master status"
#*#执行查询*
result1=`mysql -h${HOSTNAME1} -P${PORT1} -u${USERNAME1} -p${PASSWORD1} ${DBNAME1} -Bse "${select_sql}"`
node1_file_position=`echo ${result1} | awk '{print $1}'` #获取到的第一个值
node1_Postion=`echo ${result1} | awk '{print $2}'` #获取到的第二个值
node1_comtend=`echo ${result1} | awk '{print $n}'` #获取到的第N个值
echo 'node1_file_position:'$node1_file_position
echo 'node1_Postion:'$node1_Postion
echo 'node1_comtend:'$node1_comtend
echo '***************************'
echo 'connect mysql node2...'
#数据库ip
HOSTNAME2="172.26.2.115"
#数据库端口
PORT2="3306"
#数据库用户名
USERNAME2="xiaokai"
#数据库密码
PASSWORD2="123456"
#数据库名
DBNAME2="mysql"
#查询语句
select_sql="show master status"
#*#执行查询*
result2=`mysql -h${HOSTNAME2} -P${PORT2} -u${USERNAME2} -p${PASSWORD2} ${DBNAME2} -Bse "${select_sql}"`
node2_file_position=`echo ${result2} | awk '{print $1}'` #获取到的第一个值
node2_Postion=`echo ${result2} | awk '{print $2}'` #获取到的第二个值
node2_comtend=`echo ${result2} | awk '{print $n}'` #获取到的第N个值
echo 'node2_file_position:'$node2_file_position
echo 'node2_Postion:'$node2_Postion
echo 'node2_comtend:'$node2_comtend
#创建同步帐号并授权
create_acount_sql="create user 'xiaokai'@'%' identified by '123456"
`mysql -h${HOSTNAME1} -P${PORT1} -u${USERNAME1} -p${PASSWORD1} ${DBNAME1} -Bse "${create_acount_sql}"`
grant_acount_sql="grant all on *.* to 'xiaokai'@'%' identified by '123456'"
`mysql -h${HOSTNAME1} -P${PORT1} -u${USERNAME1} -p${PASSWORD1} ${DBNAME1} -Bse "${grant_acount_sql}"`
`mysql -h${HOSTNAME2} -P${PORT2} -u${USERNAME2} -p${PASSWORD2} ${DBNAME2} -Bse "${create_acount_sql}"`
`mysql -h${HOSTNAME2} -P${PORT2} -u${USERNAME2} -p${PASSWORD2} ${DBNAME2} -Bse "${grant_acount_sql}"`
echo '同步帐号授权完毕'
#配置从节点写入
stop_slave_sql="stop slave"
`mysql -h${HOSTNAME1} -P${PORT1} -u${USERNAME1} -p${PASSWORD1} ${DBNAME1} -Bse "${stop_slave_sql}"`
`mysql -h${HOSTNAME2} -P${PORT2} -u${USERNAME2} -p${PASSWORD2} ${DBNAME2} -Bse "${stop_slave_sql}"`
#配置node1
change_master_node1="CHANGE MASTER TO MASTER_HOST='${HOSTNAME1}',MASTER_PORT=${PORT1},MASTER_USER='${USERNAME1}',MASTER_PASSWORD='${PASSWORD1}',MASTER_LOG_FILE='${node1_file_position}',MASTER_LOG_POS=${node1_Postion}"
change_master_node2="CHANGE MASTER TO MASTER_HOST='${HOSTNAME2}',MASTER_PORT=${PORT2},MASTER_USER='${USERNAME2}',MASTER_PASSWORD='${PASSWORD2}',MASTER_LOG_FILE='${node2_file_position}',MASTER_LOG_POS=${node2_Postion}"
echo ${change_master_node1}
echo ${change_master_node2}
echo '==============================================='
start_slave_sql="start slave"
`mysql -h${HOSTNAME2} -P${PORT2} -u${USERNAME2} -p${PASSWORD2} ${DBNAME2} -Bse "${change_master_node1}"`
sleep 1s
`mysql -h${HOSTNAME2} -P${PORT2} -u${USERNAME2} -p${PASSWORD2} ${DBNAME2} -Bse "${start_slave_sql}"`
sleep 1s
`mysql -h${HOSTNAME1} -P${PORT1} -u${USERNAME1} -p${PASSWORD1} ${DBNAME1} -Bse "${change_master_node2}"`
sleep 1s
`mysql -h${HOSTNAME1} -P${PORT1} -u${USERNAME1} -p${PASSWORD1} ${DBNAME1} -Bse "${start_slave_sql}"`
#回调slave状态
show_slave_sql="show slave status"
slave1_status=`mysql -h${HOSTNAME1} -P${PORT1} -u${USERNAME1} -p${PASSWORD1} ${DBNAME1} -Bse "${show_slave_sql}"`
slave_node1_status=`echo ${slave1_status} | awk '{print $n}'`
echo 'node1:'${slave_node1_status}
echo '执行完毕'
echo -e '\e[0m'
#查询语句
#select_sql2='select now()'
#dateResult=`mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -Bse "${select_sql2}"`
#time2=`echo ${dateResult}|awk '{print $n}'`
#echo 'time:'$time2