[文章作者:张宴 本文版本:v1.0 最后修改:2008.07.24 转载请注明原文链接:http://blog.s135.com/read.php/359.htm]

  写了一个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} -h localhost -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
mysql.sh kill 3306

Tags: , , , ,
技术大类 » MySQL数据库 | 评论(4) | 引用(0) | 阅读(2508)
zxpxyz
2008-7-24 09:59
case "$1" in
 start)
       function_start_mysql
       ;;
 stop)
       function_stop_mysql
       ;;
 restart)
       function_restart_mysql
       ;;
 *)
       echo $"Usage: $0 {start|stop|restart}"
       exit 1
esac

试试改成这样
dd_macle
2008-7-25 14:47
谢谢提供这么好的资料!
weiwei Email Homepage
2008-10-24 17:11
很不错,很实用,赞了

http://bbs.woyool.com
aa Email Homepage
2008-11-17 16:27
感觉kill函数部分有问题, 因为用ps -ef 根本不会显示端口的。 所以你在grep ${mysql_port} 什么结果都没有。谢谢
分页: 1/1 第一页 1 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]