首页 > 数据库技术 > 详细

Linux学习笔记:Mysql的启动与关闭脚本

时间:2016-01-16 19:40:34      阅读:230      评论:0      收藏:0      [点我收藏+]

该脚本只针对多实例的MySQL,并且有一定的限制,是基于http://coosh.blog.51cto.com/6334375/1735271 这篇安装结果。

[root@vmtest ~]# cd /disk2/mysql_multi_instances/3306
[root@vmtest 3306]# vi mysql_server.sh
#!/bin/bash
#2016-01-15 version 1
#Author Coosh
#This script is used in multi thread environment to start|stop|restart the mysqld deamon. One thing should be noticed, we don‘t use "kill" to shutdown the mysqld process, but "mysqladmin shutdown".
#This script should be place in the same directory with the socket file and my.cnf file
export ACTION=$1
export SOCKFILE=./mysql.sock
export CNFFILE=./my.cnf
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 ./mysql.sock 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|stop|restart"
        ;;
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
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
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
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
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


Linux学习笔记:Mysql的启动与关闭脚本

原文:http://coosh.blog.51cto.com/6334375/1735536

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!