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