建立管理MySQL資料庫的shell指令碼

markzy5201190發表於2012-12-26
//建立管理MySQL資料庫的shell指令碼;
#!/bin/bash

mysql_port=3306
mysql_username="admin"
mysql_password="123456"

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

//停止MySQL
function_stop_mysql(){
printf "Stop MySQL...\n" OR  echo -e "Stop MySQL..."
/usr/local/webserver/mysql/bin/mysqladmin -h localhost -P${mysql_port} -u${mysql_username} -p${mysql_password} shutdown 
OR
    /usr/local/webserver/mysql/bin/mysqladmin -h localhost -P${mysql_port} -u${mysql_username} -p${mysql_password} -S /path/mysql.sock shutdown
}
//重啟MySQL
function_restart_mysql(){
printf "Restarting MySQL...\n"
function_stop_mysql
sleep 5
function_start_mysql
}

//非正常關閉MySQL
function_kill_mysql(){
kill -9 $(ps -ef | grep 'bin/mysqld_safe' | grep ${mysql_port} | awk '{print $2}')
kill -9 $(ps -ef | grep 'libexec/mysqld | grep ${mysql_port}' | awk '{print $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:/usr/mysql {start|stop|restart|kill}\n"
fi

chmod +x /usr/local/shell/mysqld

/usr/local/shell/mysqld start
/usr/local/shell/mysqld stop
/usr/local/shell/mysqld kill
.....

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26855487/viewspace-751687/,如需轉載,請註明出處,否則將追究法律責任。

相關文章