[iyunv@vmtest ~]# cd /disk2/mysql_multi_instances/3306
[iyunv@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;
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
}
[iyunv@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))
[iyunv@vmtest 3306]# ./mysql_server.sh stop 3306
MySql is running.
Please enter root password to shut it down
Enter password:
MySql graceful shutdown!
[iyunv@vmtest 3306]# ss -tlnp | grep 330
0 128 *:3307 *:* users:(("mysqld",30319,12))
0 128 *:3308 *:* users:(("mysqld",32016,12))
启动
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[iyunv@vmtest 3306]# ./mysql_server.sh start
Mysql started!
[iyunv@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))
[iyunv@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!
[iyunv@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)
1
2
3
4
5
6
7
8
9
10
11
[iyunv@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!
[iyunv@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))
如果输错了密码,程序会推出
1
2
3
4
5
6
7
[iyunv@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