1. 程式人生 > >快捷啟動、停止、重啟、殺死指定埠MySQL程序的sh指令碼

快捷啟動、停止、重啟、殺死指定埠MySQL程序的sh指令碼

寫了一個shell指令碼,可以在同一臺Linux伺服器的不同埠,執行多個MySQL服務的情況下,快捷啟動、停止、重啟、殺死指定埠的MySQL程序。

vi /usr/local/bin/mysql.sh
  輸入以下內容(因各伺服器的MySQL配置不同,可能需要修改的部分已用紅色標註):
#!/bin/sh

mysql_port=$2
mysql_username="root"
mysql_password="123456"

function_start_mysql()
{
    printf "Starting MySQL...\n"
    /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/mysql/${mysql_port}/my.cnf 2>&1 > /dev/null &
}

function_stop_mysql()
{
    printf "Stoping MySQL...\n"
    /usr/local/mysql/bin/mysqladmin
-u ${mysql_username} -p${mysql_password} -hlocalhost -P ${mysql_port} shutdown
}

function_restart_mysql()
{
    printf "Restarting MySQL...\n"
    function_stop_mysql
    function_start_mysql
}

function_kill_mysql()
{
    kill -9 $(ps -ef | grep 'bin/mysqld_safe' | grep ${mysql_port} | awk '{printf $2}')
    kill -9 $(ps -ef | grep 'libexec/mysqld' | grep ${mysql_port} | awk '{printf $2}')
}

if [ "$1" = "start" ]; then
    function_start_mysql
elif [ "$1" = "stop" ]; then
    function_stop_mysql
elif [ "$1" = "restart" ]; then
    function_restart_mysql
elif [ "$1" = "kill" ]; then
    function_kill_mysql
else
    printf "Usage: mysql.sh {start|stop|restart|kill}\n"
fi

  賦予指令碼可執行許可權:
chmod +x /usr/local/bin/mysql.sh

  指令碼執行方法:
mysql.sh start 3306
mysql.sh stop 3306
mysql.sh restart 3306