MySQL operation and maintenance -3, multi-instance control with the use of scripts written

Experimental environment
this article in the "MySQL operation and maintenance -2, multi-instance deployment", as also on an experimental test environment after the deployment is successful with the environment.

Use
1, scripting, named mysqld, let it have execute permissions
2, copy mysqld to a different directory instance

[]# tree -L 2 /data/

/data/
├── 3306
│   ├── dbfile
│   ├── my.cnf
│   └── mysqld
└── 3307
├── dbfile
├── my.cnf
└── mysqld

3, modify the port in mysqld, belongs to the same instance allowed
, executed with no parameters 4

[]# /data/3306/mysqld start
MySQL is starting...
[]# /data/3306/mysqld stop
MySQL is stopping...

5, write the script / etc / rc / local, so that it runs automatically open

[]# vim /etc/rc.local

/data/3306/mysqld start
/data/3307/mysqld start

Script content

[]# vim /data/3306/mysqld
#/bin/sh

m_user='root'                         //定义使用哪个用户控制MySQL
m_password='1qaz3edc'       //定义控制MySQL的用户密码
m_port='3306'                        //定义MySQL的端口号,用于区分是哪个实例,主要修改此变量
m_cmddir='/app/mysql/bin'    //定义MySQL的环境目录
#start_function                       //定义启动MySQL的函数
function_mysql_start()
{
if [ -e /data/${m_port}/mysql.sock ];then
printf 'MySQL is running...\n'
exit 1
else
printf 'MySQL is starting...\n'
${m_cmddir}/mysqld_safe --defaults-file=/data/${m_port}/my.cnf 2>&1 >/dev/null &        //核心命令,启动MySQL
//注意gt;是大于号,51cto无法正常显示大于号。
fi
}
#stop_function                       //定义关闭MySQL的函数
function_mysql_stop()
{
if [ -e /data/${m_port}/mysql.sock ];then
printf 'MySQL is stopping...\n'
${m_cmddir}/mysqladmin -S /data/${m_port}/mysql.sock -u${m_user} -p${m_password} shutdown                   //核心命令,关闭MySQL
else
printf "MySQL is stoped...\n"
exit 1
fi
}
#do it                                      //针对不同的参数,引用不同的函数。
case $1 in
start)
function_mysql_start
;;
stop)
function_mysql_stop
;;
*)
printf "arguments is error,please enter start or stop。"
;;
esac

Guess you like

Origin blog.51cto.com/yuanshuai/2484845