mysql 服务启停管理脚本 留存备用

B站影视 2024-12-27 17:47 1

摘要:if [[ ! -z "$AUTH_ENABLE" ]] && [[ "$AUTH_ENABLE" != "" ]]; then

#!/BIN/bash

BIN_PATH=$(cd "$(dirname "$0")"; pwd)

cd $BIN_PATH

umask 077

source ~/.bash_profile

source ./common.sh

export PATH=${INSTALL_DIR}/MYSQL/socat/bin:${INSTALL_DIR}/mysql/xtrabackup/bin:${INSTALL_DIR}/mysql/mysql/bin:$PATH

if [[ "$OS_NAME" != "sles" ]]; then

export LD_LIBRARY_PATH=${INSTALL_DIR}/mysql/mysql/lib:$LD_LIBRARY_PATH

fi

script_name=`echo $0 | awk -F '/' '{print $NF}'`

MYSQL_TEST_PWD=$(base64 -d

RUNNING_USER=`sed '/^user=/!d;s/.*=//' ${INSTALL_DIR}/mysql/mysql/my.cnf | sort -u`

MYSQL_IP=`sed '/^wsrep_node_address=/!d;s/.*=//' ${INSTALL_DIR}/mysql/mysql/my.cnf`

CLUSTER_ADDR=`sed '/^wsrep_cluster_address=/!d;s/.*=//' ${INSTALL_DIR}/mysql/mysql/my.cnf | awk -F '//' '{print $2}'`

CLUSTER_PORT=`sed '/^export CLUSTER_PORT=/!d;s/.*=//' ${INSTALL_DIR}/mysql/proxysql/proxysql-admin.cfg | sed $'s/\'//g'`

AUTH_ENABLE=`sed '/^default_authentication_plugin=/!d;s/.*=//' ${INSTALL_DIR}/mysql/mysql/my.cnf`

DATA_DIR=`sed '/^datadir=/!d;s/.*=//' ${INSTALL_DIR}/mysql/mysql/my.cnf`

GRASTATE_FILE=${DATA_DIR}/grastate.dat

PROXYSQL_INITED=`sed '/^datadir=/!d;s/.*=//' ${INSTALL_DIR}/mysql/proxysql/proxysql.cfg`

MYSQL_EXEC="${INSTALL_DIR}/mysql/mysql/bin/mysql -utest -p$MYSQL_TEST_PWD"

if [[ ! -z "$AUTH_ENABLE" ]] && [[ "$AUTH_ENABLE" != "" ]]; then

MYSQL_EXEC="${INSTALL_DIR}/mysql/mysql/bin/mysql -utest -p$MYSQL_TEST_PWD --ssl-mode=required"

fi

check_status {

pid=`ps -ef | grep -w 'mysqld' | grep -vE "grep|$script_name" | awk '{print $2}'`

if [[ ! -z "$pid" ]]; then

stat=`${MYSQL_EXEC} -e "select 1 val;" 2> /dev/null | grep -v 'val'`

if [[ "$stat" == "1" ]]; then

echo 0

else

echo 1

fi

else

echo 3

fi

}

start_normal {

if [[ -f "$GRASTATE_FILE" ]]; then

sed -i 's/^safe_to_bootstrap.*$/safe_to_bootstrap: 0/' $GRASTATE_FILE

fi

${INSTALL_DIR}/mysql/mysql/bin/mysqld --defaults-file=${INSTALL_DIR}/mysql/mysql/my.cnf --user=$RUNNING_USER &

if [[ "$?" -eq 0 && -n "$PROXYSQL_INITED" ]]; then

sh $INSTALL_DIR/mysql/proxysql/proxysql.sh start

fi

}

start_bootstrap {

if [[ -f "$GRASTATE_FILE" ]]; then

sed -i 's/^safe_to_bootstrap.*$/safe_to_bootstrap: 1/' $GRASTATE_FILE

fi

${INSTALL_DIR}/mysql/mysql/bin/mysqld --defaults-file=${INSTALL_DIR}/mysql/mysql/my.cnf --user=$RUNNING_USER --wsrep-new-cluster &

if [[ "$?" -eq 0 && -n "$PROXYSQL_INITED" ]]; then

sh $INSTALL_DIR/mysql/proxysql/proxysql.sh start

fi

}

do_start {

safe_bootstrap_flag=$(grep 'safe_to_bootstrap' $GRASTATE_FILE | awk -F ':' '{print $2}' | sed 's/^ //g;s/ $//g')

if [[ "$safe_bootstrap_flag" -eq 1 ]]; then

start_bootstrap

else

start_normal

fi

else

start_normal

fi

}

start {

for i in {1..30}

do

stat=`ss -anp | grep LISTEN | grep -E "*.:4567 .*"`

if [[ -z "$stat" ]]; then

break;

else

echo "Port 4567 is on listening, waiting to release!"

fi

if [[ $i -eq 30 ]]; then

echo "Port 4567 is on listening, please try again later!"

exit 1

fi

sleep 2

done

do_start

ret=$(check_status)

retry=0

while [[ "$ret" -ne 0 && "$retry" -lt 60 ]]; do

sleep 2

ret=$(check_status)

retry=$(($retry+1))

done

if [[ "$ret" -ne 0 ]]; then

ps -ef | grep -w 'mysqld' | grep -vE "grep|$script_name" | awk '{print $2}' | xargs kill >/dev/null 2>&1

do_start

ret=$(check_status)

retry=0

sleep 2

ret=$(check_status)

retry=$(($retry+1))

done

if [[ "$ret" -eq 0 ]]; then

echo Start mysql[$MYSQL_IP] success."

fi

fi

if [[ "$ret" -eq 0 && -n "$PROXYSQL_INITED" ]]; then

echo ProxySQL starting....."

return $?

fi

return $ret

}

restart {

pkill mysqld

loop=0

while [[ -n "$(ps -ef | grep -w 'mysqld' | grep -v grep)" && "$loop" -lt 15 ]]; do

sleep 2

loop=$(($loop+1))

done

start

}

stop {

if [[ -n "$(ps aux | grep 'mysql/bin/mysqld' | grep -v grep)" ]]; then

CLUSTER_ADDR_LIST=($(echo "$CLUSTER_ADDR" | tr "," "\n" | sort -r))

if [[ ${#CLUSTER_ADDR_LIST[@]} -gt 1 ]]; then

for ((i=0;i

do

if [[ "$MYSQL_IP" == ${CLUSTER_ADDR_LIST[i]} ]]; then

sleep $(($i*10))

fi

done

fi

pkill mysqld

fi

if [[ -n "$PROXYSQL_INITED" ]]; then

sh $INSTALL_DIR/mysql/proxysql/proxysql.sh stop

fi

}

status {

pid=`ps -ef | grep -w 'mysqld' | grep -vE "grep|$script_name" | awk '{print $2}'`

if [[ -n "$pid" ]]; then

wsrep_cluster_status=`$MYSQL_EXEC -e "show status like 'wsrep_cluster_status';" | sed -n '/wsrep_cluster_status/p' | awk '{print $2}' | tr 'A-Z' 'a-z'`

if [[ "$wsrep_cluster_status" == "non-primary" ]]; then

exit 1

fi

wsrep_cluster_address=(`sed '/^wsrep_cluster_address=/!d;s/.*=//' ${INSTALL_DIR}/mysql/mysql/my.cnf | sed $'s/\'//g' | awk -F '//' '{print $2}' | tr ',' ' '`)

wsrep_incoming_addresses=(`$MYSQL_EXEC -e "show status like 'wsrep_incoming_addresses';" | sed -n '/wsrep_incoming_addresses/p' | awk '{print $2}' | tr ',' ' '`)

if [[ "${#wsrep_cluster_address[*]}" -eq "${#wsrep_incoming_addresses[*]}" ]]; then

cat /dev/null > $BIN_PATH/.state

sh $INSTALL_DIR/mysql/proxysql/proxysql.sh status

if [[ "$?" -eq 0 ]]; then

echo "mysql service available!"

else

echo "proxysql service unavailable!"

exit 1

fi

else

for addr in ${wsrep_cluster_address[@]}

do

if [[ $? != 0 ]]; then

wsrep_cluster_address=(${wsrep_cluster_address[*]/$addr})

fi

done

else

exit 1

fi

else

echo "$(date '+%Y-%m-%d %H:%M:%S.%s'): unavailable" >> $BIN_PATH/.state

if [[ "$(cat $BIN_PATH/.state | grep 'unavailable' | wc -l)" -ge 5 ]]; then

echo "mysql service unavailable!"

exit 1

else

exit 0

fi

fi

fi

else

echo "mysql not running!"

exit 3

fi

}

case $1 in

start)

start

;;

start_bootstrap)

start_bootstrap

;;

start_normal)

start_normal

;;

stop)

stop

;;

restart)

restart

;;

status)

status

;;

esac

来源:小鱼看科技

相关推荐