已知MySQL多实例启动命令为:
mysqld_safe --defaults-file=/data/3306/my.cnf &
停止命令为:
mysqladmin -uroot -pchang123 -S /data/3306/mysql.sock shutdown
请完成mysql多实例的启动脚本的编写:
问题分析:
要想写出脚本,必须对MySQL服务很熟悉。
1)单实例:
bash
# Mysql启动
mysqld_safe &
# Mysql停止
mysqld_admin -u root -p Chang123 shutdown
bash
# 设置root密码
[root@vm1 scripts]# mysqladmin -uroot password "Chang123"
# 先将mariadb停止
[root@vm1 scripts]# mysqladmin -uroot -pChang123 shutdown
# 检查进程是否停止
[root@vm1 scripts]# netstat -atunlp |grep 3306
# 再启动mysql
[root@vm1 scripts]# mysqld_safe --user=mysql &
[1] 11535
[root@vm1 scripts]# 230802 21:49:37 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
230802 21:49:37 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
# 再检查mysql进程是否已启动
[root@vm1 scripts]# netstat -atunlp |grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 11681/mysqld
先是单实例的启动脚本的编写:
bash
[root@vm1 scripts]# cat start_db.sh
#!/bin/bash
#
[ -f /etc/init.d/functions ] && . /etc/init.d/functions || echo 1
usage(){
echo "USAGE: $0 {start|stop|restart}
exit 1
}
if [ $# -ne 1 ]
then
usage
fi
start() {
mysqld_safe --user=mysql >/dev/null 2>&1 &
if [ $? -eq 0 ]
then
action "start mysql" /bin/true
else
action "start mysql" /bin/false
fi
}
stop() {
mysqladmin -uroot -pChang123 shutdown >/dev/null 2>&1
if [ $? -eq 0 ]
then
action "stop mysql" /bin/true
else
action "stop mysql" /bin/false
fi
}
restart() {
stop
sleep 2
start
}
if [ "$1" == "start" ]
then
start
elif [ "$1" == "stop" ]
then
stop
elif [ "$1" == "restart" ]
then
restart
else
usage
fi
执行结果:
bash
[root@vm1 scripts]# netstat -atunlp |grep 3306
[root@vm1 scripts]#
[root@vm1 scripts]# sh start_db.sh start
start mysql [ OK ]
[root@vm1 scripts]# 230802 22:17:56 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
230802 22:17:56 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
[root@vm1 scripts]# sh start_db.sh restart
stop mysql [ OK ]
start mysql [ OK ]
[root@vm1 scripts]# 230802 22:18:19 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
230802 22:18:19 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
[root@vm1 scripts]#
将一些信息都不显示出来:
bash
[root@vm1 scripts]# sh start_db.sh start
start mysql [ OK ]
[root@vm1 scripts]# sh start_db.sh stop
stop mysql [ OK ]
[root@vm1 scripts]# sh start_db.sh restart
stop mysql [ OK ]
start mysql [ OK ]
[root@vm1 scripts]# netstat -atunlp |grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 13173/mysqld
多实例启动脚本:
bash
[root@vm1 scripts]# cat mysql_manage.sh
#!/bin/bash
#
Port=3306
MysqlUser="root"
MysqlPassword="Chang123"
CmdPath="/usr/local/mysql/bin"
#start function
start() {
if [ `netstat -auntlp |grep "$Port" | wc -l` -eq 0 ]
then
echo "Starting MySQL..."
/bin/sh ${CmdPath}/mysqld_safe --defaults-fle=/data/${Port}/my.cnf 2>&1 >/dev/null &
else
echo "MySQL is running..."
}
#stop function
stop(){
if [ `netstat -atunlp |grep "$Port" | wc -l` -eq 0 ]
then
echo "Stopping MySQL..."
${CMDPath}/mysqladmin -u ${MysqlUser} -p {$MysqlPassword} -S /data/${Port}/mysql.sock shutdown
else
echo "MySQL is stopped..."
fi
}
#restart function
restart(){
echo "Restarting MySQL..."
stop
sleep 2
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo "USAGE: $0 {start|stop|restart}"
esac
代码说明:
1)其中使用了case语句。
把这个脚本放到/etc/init.d/目录中,实现/etc/init.d/mysqld01 start 启动,并通过chkconfig对其设置开机自启动和关闭。
如果自己写脚本,也就是放到/etc/init.d目录中,作为启动脚本。
在脚本中添加这块内容,设置mysql的开机自启动。
bash
# chkconfig: 2345 20 80
# description: Start mysql and stop mysql script.
man chkconfig,看看chkconfig的帮助文档。
2345是运行的级别。
20:启动优先级
80: 停止优先级
你应该能指出descripion的内容。\ 反斜线是换行继续。忽略掉在行前面的空格。
多看man帮助文档。
看看/etc/init.d/rpcbind
需要注意的是:数字可设置成不一样的,但是要确保启动优先级在rc.d的子目录中不要有冲突。切记。
企业面试题:
怎么把自己写的脚本添加到服务里面,即可以使用service命令来调用?
chkconfig 2345 21 60
description: Save and restores system entropy pool for \