一台服务器上安装多个MySQL实例之后,实例的启动关闭不能再用service mysqld start/stop/restart命令,所以编写如下脚本用于启动关闭对应端口的实例。
这个脚本适用于多实例版本,一台服务器上安装多个实例,不同端口,其中my.cnf文件放在mysql的databasedir目录下面:
例如开启3310端口的mysql:./mysql_start start 3310
#cat mysql_start
#!/bin/bash
start_mode=$
mysql_port=$
function_start_mysql()
{
stat=$(netstat -antlp|grep ${mysql_port}|grep LISTEN|grep mysql)
if [ -z "$stat" ]; then
printf "Starting MySQL...\n"
/usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql${mysql_port}/my.cnf >& >/dev/null &
else
printf "MySQL Started\n"
fi
}
function_stop_mysql()
{
stat=$(netstat -antlp|grep ${mysql_port}|grep LISTEN|grep mysql)
if [ -n "$stat" ]; then
printf "Stoping MySQL...\n"
/usr/local/mysql/bin/mysqladmin -S /data/mysql${mysql_port}/mysql.sock shutdown
else
printf "MySQL Stoped\n"
fi
}
function_restart_mysql()
{
printf "Restarting MySQL...\n"
function_stop_mysql
function_start_mysql
}
function_kill_mysql()
{
kill - $(ps -ef|grep ${mysql_port}|grep bin/mysqld_safe|awk '{print $2}')
kill - $(ps -ef|grep ${mysql_port}|grep bin/mysqld|awk '{print $2}')
}
case $ in
start)
function_start_mysql;;
stop)
function_stop_mysql;;
restart)
function_restart_mysql;;
kill)
function_kill_mysql;;
*)
echo "Usage: /data/mysql${mysql_port}/mysqld {start|stop|restart|kill}";;
esac