[root@vmtest ~]# cd /disk2/mysql_multi_instances/3306
[root@vmtest 3306]# vi mysql_server.sh
#!/bin/bash
#2016-01-15 version 1
#
#2016-01-15 version 2
#the script has modified, no need to place with the socket file and my.cnf file any more. and the
#usage is changed also. Exemple: mysql_server start 3307; mysql_server restart 3307;
export ACTION=$1
export PORT=$2
export MULTI_THREAD_DIR=/disk2/mysql_multi_instances
export CNFFILE=$MULTI_THREAD_DIR/$PORT/my.cnf
export SOCKFILE=$MULTI_THREAD_DIR/$PORT/mysql.sock
function start()
{
[ -r $CNFFILE -a -s $CNFFILE ] && mysqld_safe --defaults-file=$CNFFILE &>/dev/null &
if [ $?==0 ]; then
echo 'Mysql started!'
else
echo 'Mysql start failed...'
fi
}
function stop()
{
if [ -S $SOCKFILE ]; then
echo -e 'MySql is running.\nPlease enter root password to shut it down';
mysqladmin -uroot -p -S $SOCKFILE shutdown ;
if [ $? -ne 0 ]; then
echo 'Wrong Password! Please redo the command';
exit 64 ;
else
echo 'MySql graceful shutdown!'
fi
else
echo "MySql isn't running.."
fi
}
case $ACTION in
start)
start ;
;;
stop)
stop ;
;;
restart)
stop ;
start ;
;;
*)
echo "Usage $0 start portnum|stop portnum|restart portnum"
;;
esac
运行结果
优雅关闭[root@vmtest 3306]# ss -tlnp | grep 330
0 128 *:3306 *:* users:(("mysqld",13363,12))
0 128 *:3307 *:* users:(("mysqld",30319,12))
0 128 *:3308 *:* users:(("mysqld",32016,12))
[root@vmtest 3306]# ./mysql_server.sh stop 3306
MySql is running.
Please enter root password to shut it down
Enter password:
MySql graceful shutdown!
[root@vmtest 3306]# ss -tlnp | grep 330
0 128 *:3307 *:* users:(("mysqld",30319,12))
0 128 *:3308 *:* users:(("mysqld",32016,12))
启动[root@vmtest 3306]# ./mysql_server.sh start
Mysql started!
[root@vmtest 3306]# ss -tlnp | grep 330
0 128 *:3306 *:* users:(("mysqld",14834,12))
0 128 *:3307 *:* users:(("mysqld",30319,12))
0 128 *:3308 *:* users:(("mysqld",32016,12))
[root@vmtest 3306]# ./mysql_server.sh restart 3306
MySql is running.
Please enter root password to shut it down
Enter password:
MySql graceful shutdown!
Mysql started!
[root@vmtest 3306]# ss -tlnp | grep 330
0 128 *:3306 *:* users:(("mysqld",15585,12))
0 128 *:3307 *:* users:(("mysqld",30319,12))
0 128 *:3308 *:* users:(("mysqld",32016,12))
重启(注意上面的3306的pid是15585)[root@vmtest 3306]# ./mysql_server.sh restart 3306
MySql is running.
Please enter root password to shut it down
Enter password:
MySql graceful shutdown!
Mysql started!
[root@vmtest 3306]# !ss
ss -tlnp | grep 330
0 128 *:3306 *:* users:(("mysqld",16337,12))
0 128 *:3307 *:* users:(("mysqld",30319,12))
0 128 *:3308 *:* users:(("mysqld",32016,12))
如果输错了密码,程序会推出[root@vmtest 3306]# ./mysql_server.sh restart 3306
MySql is running.
Please enter root password to shut it down
Enter password:
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: YES)'
Wrong Password! Please redo the command