galera集群启服务脚本,根据需求可自改

这么个玩意儿废了我小半天儿,学如逆水行舟啊

 

[root@galera1 shell_scripts]# cat startdb.sh
##host_list={galera1,galera2,galera3}
echo "task start at: $(date +%Y-%m-%d_%H:%M:%S)"
bootstrap_tmpfile=/tmp/.db_stat.$(date +%Y%m%d%H%M)

stop_db(){
        ssh galera3  'systemctl stop mariadb'
        sleep 1
        ssh galera2  'systemctl stop mariadb'
        sleep 1
        ssh galera1  'systemctl stop mariadb'
}

check_status(){
        for host in {galera1,galera2,galera3};
        do
                echo ${host} $(ssh ${host} "grep safe_to_bootstrap /var/lib/mysql/grastate.dat"|awk '{print $NF}')
        done > ${bootstrap_tmpfile}
}


start_db(){
        if [ $(awk '/ 1/{print $1}' ${bootstrap_tmpfile}|wc -l) = 1 ]; #注意awk关键词里面有个空格
        then
                host1=$(awk '/ 1/{print $1}' ${bootstrap_tmpfile});
        else
                host1=galera1
                ssh $host1 "sed -i '/bootstrap/c\safe_to_bootstrap: 1' /var/lib/mysql/grastate.dat"
                for host in $(awk '{print $1}' ${bootstrap_tmpfile}|grep -v ${host1});do
                        ssh $host "sed -i '/bootstrap/c\safe_to_bootstrap: 0' /var/lib/mysql/grastate.dat"
                done
        fi
        ssh $host1  'galera_new_cluster'
        sleep 3
        for host in $(awk '{print $1}' ${bootstrap_tmpfile}|grep -v ${host1});do
                ssh $host 'systemctl start mariadb'
        done
}
main(){
        stop_db
        check_status
        start_db
        rm -f ${bootstrap_tmpfile}
        echo "task over  at: $(date +%Y-%m-%d_%H:%M:%S)"
}
main

 

posted @ 2024-01-18 11:52  咿呀哒喏  阅读(29)  评论(0编辑  收藏  举报