shell学习二十一--使用if编写mysql启动脚本
2017-11-17 18:38
543 查看
例子:开发mysql单实例或者多实例启动脚本
已知mysql多实例启动命令为:
mysqld_safe --deafaults-file=/data/3306/my.cnf &
停止命令
mysqladmin -u root -p123 -S /data/3306/mysql.sock shutdown
请完成mysql单实例或者多实例启动脚本编写。
要求:用函数、if语句等实现。
解答:单实例
1、启动:mysql_safe --user=mysql &
2、停止:mysqladmin -u root -p123 shutdown
[root@node01 day7]# mysql -uroot -p123
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
[root@node01 day7]# mysqladmin -uroot -p123 shutdown
[root@node01 day7]# ps -ef|grep mysql
root 6689 93648 0 02:18 pts/5 00:00:00 grep mysql
[root@node01 day7]#
[root@node01 day7]# lsof -i :3306
实际脚本
[root@node01 day8]# cat start_db.sh
#!/bin/sh
function usage(){
echo "$0 {start|stop|restart}"
exit 1
}
[ $# -ne 1 ] && usage
function start_mysql(){
mysqld_safe --user=mysql &
if [ $? -eq 0 ]
then
action "start mysql" /bin/true
else
action "start mysql" /bin/false
fi
}
function stop_mysql(){
mysqladmin -u root -p123 shutdown
if [ $? -eq 0 ]
then
action "stop mysql" /bin/true
else
action "stop mysql" /bin/false
fi
}
if [ "$1" == "start" ]
then
start_mysql
elif [ "$1" == "stop" ]
then
stop_mysql
elif [ "$1" == "restart" ]
then
stop_mysql
start_mysql
else
usage
fi
[root@node01 day8]# sh start_db.sh stop
170803 02:39:44 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
start_db.sh: line 23: action: command not found
[1]+ Done mysqld_safe --user=mysql
[root@node01 day8]# ps -ef|grep mysql
root 7017 93648 0 02:39 pts/5 00:00:00 grep mysql
[root@node01 day8]# lsof -i :3306
[root@node01 day8]# sh start_db.sh start
start_db.sh: line 14: action: command not found
[root@node01 day8]# 170803 02:40:02 mysqld_safe Logging to '/var/log/mysqld.log'.
170803 02:40:02 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
[root@node01 day8]#
[root@node01 day8]#
[root@node01 day8]#
[root@node01 day8]#
[root@node01 day8]#
[root@node01 day8]# lsof -i :3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 7107 mysql 10u IPv4 962425 0t0 TCP *:mysql (LISTEN)
[root@node01 day8]# ps -ef|grep mysql
root 7022 1 0 02:40 pts/5 00:00:00 /bin/sh /usr/bin/mysqld_safe --user=mysql
mysql 7107 7022 0 02:40 pts/5 00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
root 7130 93648 0 02:40 pts/5 00:00:00 grep mysql
[root@node01 day8]# mysql -uroot -p123
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
带入路径(生产中带上路径)
[root@node01 day8]# vi start_db.sh
#!/bin/sh
. /etc/init.d/functions
path=/usr/bin/
function usage(){
echo "$0 {start|stop|restart}"
exit 1
}
[ $# -ne 1 ] && usage
function start_mysql(){
$path/mysqld_safe --user=mysql &
if [ $? -eq 0 ]
then
action "start mysql" /bin/true
else
action "start mysql" /bin/false
fi
}
function stop_mysql(){
mysqladmin -u root -p123 shutdown
if [ $? -eq 0 ]
then
action "stop mysql" /bin/true
else
action "stop mysql" /bin/false
fi
}
if [ "$1" == "start" ]
then
start_mysql
elif [ "$1" == "stop" ]
then
stop_mysql
elif [ "$1" == "restart" ]
then
stop_mysql
start_mysql
"start_db.sh" 43L, 678C written
You have new mail in /var/spool/mail/root
[root@node01 day8]# sh start_db.sh stop
170803 02:44:23 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
stop mysql [ OK ]
[root@node01 day8]# ps -ef|grep mysql
root 7175 93648 0 02:44 pts/5 00:00:00 grep mysql
[root@node01 day8]# sh start_db.sh start
start mysql [ OK ]
[root@node01 day8]# 170803 02:44:32 mysqld_safe Logging to '/var/log/mysqld.log'.
170803 02:44:32 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
[root@node01 day8]#
[root@node01 day8]# ps -ef|grep mysql
root 7179 1 0 02:44 pts/5 00:00:00 /bin/sh /usr/bin//mysqld_safe --user=mysql
mysql 7264 7179 0 02:44 pts/5 00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
root 7278 93648 0 02:44 pts/5 00:00:00 grep mysql
优化脚本
[root@node01 day8]# vi start_db.sh sart
2 files to edit
#!/bin/sh
. /etc/init.d/functions
path=/usr/bin/
pass=123
user=root
function usage(){
echo "$0 {start|stop|restart}"
exit 1
}
[ $# -ne 1 ] && usage
function start_mysql(){
$path/mysqld_safe --user=mysql & >/dev/null 2>&1
if [ $? -eq 0 ]
then
action "start mysql" /bin/true
else
action "start mysql" /bin/false
fi
}
function stop_mysql(){
mysqladmin -u$user -p$pass shutdown >/dev/null 2>&1
if [ $? -eq 0 ]
then
action "stop mysql" /bin/true
else
action "stop mysql" /bin/false
fi
}
if [ "$1" == "start" ]
then
start_mysql
elif [ "$1" == "stop" ]
then
stop_mysql
elif [ "$1" == "restart" ]
then
stop_mysql
start_mysql
[root@node01 day8]# sh start_db.sh start
start mysql [ OK ]
You have new mail in /var/spool/mail/root
[root@node01 day8]# 170803 02:46:55 mysqld_safe Logging to '/var/log/mysqld.log'.
170803 02:46:55 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
去除启动时的log
[root@node01 day8]# cat start_db.sh
#!/bin/sh
. /etc/init.d/functions
path=/usr/bin/
pass=123
user=root
function usage(){
echo "$0 {start|stop|restart}"
exit 1
}
[ $# -ne 1 ] && usage
function start_mysql(){
$path/mysqld_safe --user=mysql >/dev/null 2>&1 &
if [ $? -eq 0 ]
then
action "start mysql" /bin/true
else
action "start mysql" /bin/false
fi
}
function stop_mysql(){
mysqladmin -u$user -p$pass shutdown >/dev/null 2>&1
if [ $? -eq 0 ]
then
action "stop mysql" /bin/true
else
action "stop mysql" /bin/false
fi
}
if [ "$1" == "start" ]
then
start_mysql
elif [ "$1" == "stop" ]
then
stop_mysql
elif [ "$1" == "restart" ]
then
stop_mysql
start_mysql
else
usage
fi
把脚本copy 到/etc/init.d/下
mv start_db.sh mysqld
chmod +x mysqld
实现/etc/init.d/mysql01 start启动,并且通过chkconfig设置开机自启动和关闭。
#!/bin/sh
# chkconfig: 2345 21 60 (启动顺序不要和已有的冲突)
# description: start mysql and stop mysql scripts.
[root@node01 day8]# cp start_db.sh /etc/init.d/mysql01
[root@node01 day8]# chkconfig --add mysql01
[root@node01 day8]# chkconfig --list mysql01
mysql01 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@node01 day8]# ll /etc/rc.d/rc3.d/ |grep mysql01
lrwxrwxrwx 1 root root 17 Aug 3 03:26 S21mysql01 -> ../init.d/mysql01
已知mysql多实例启动命令为:
mysqld_safe --deafaults-file=/data/3306/my.cnf &
停止命令
mysqladmin -u root -p123 -S /data/3306/mysql.sock shutdown
请完成mysql单实例或者多实例启动脚本编写。
要求:用函数、if语句等实现。
解答:单实例
1、启动:mysql_safe --user=mysql &
2、停止:mysqladmin -u root -p123 shutdown
[root@node01 day7]# mysql -uroot -p123
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
[root@node01 day7]# mysqladmin -uroot -p123 shutdown
[root@node01 day7]# ps -ef|grep mysql
root 6689 93648 0 02:18 pts/5 00:00:00 grep mysql
[root@node01 day7]#
[root@node01 day7]# lsof -i :3306
实际脚本
[root@node01 day8]# cat start_db.sh
#!/bin/sh
function usage(){
echo "$0 {start|stop|restart}"
exit 1
}
[ $# -ne 1 ] && usage
function start_mysql(){
mysqld_safe --user=mysql &
if [ $? -eq 0 ]
then
action "start mysql" /bin/true
else
action "start mysql" /bin/false
fi
}
function stop_mysql(){
mysqladmin -u root -p123 shutdown
if [ $? -eq 0 ]
then
action "stop mysql" /bin/true
else
action "stop mysql" /bin/false
fi
}
if [ "$1" == "start" ]
then
start_mysql
elif [ "$1" == "stop" ]
then
stop_mysql
elif [ "$1" == "restart" ]
then
stop_mysql
start_mysql
else
usage
fi
[root@node01 day8]# sh start_db.sh stop
170803 02:39:44 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
start_db.sh: line 23: action: command not found
[1]+ Done mysqld_safe --user=mysql
[root@node01 day8]# ps -ef|grep mysql
root 7017 93648 0 02:39 pts/5 00:00:00 grep mysql
[root@node01 day8]# lsof -i :3306
[root@node01 day8]# sh start_db.sh start
start_db.sh: line 14: action: command not found
[root@node01 day8]# 170803 02:40:02 mysqld_safe Logging to '/var/log/mysqld.log'.
170803 02:40:02 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
[root@node01 day8]#
[root@node01 day8]#
[root@node01 day8]#
[root@node01 day8]#
[root@node01 day8]#
[root@node01 day8]# lsof -i :3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 7107 mysql 10u IPv4 962425 0t0 TCP *:mysql (LISTEN)
[root@node01 day8]# ps -ef|grep mysql
root 7022 1 0 02:40 pts/5 00:00:00 /bin/sh /usr/bin/mysqld_safe --user=mysql
mysql 7107 7022 0 02:40 pts/5 00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
root 7130 93648 0 02:40 pts/5 00:00:00 grep mysql
[root@node01 day8]# mysql -uroot -p123
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
带入路径(生产中带上路径)
[root@node01 day8]# vi start_db.sh
#!/bin/sh
. /etc/init.d/functions
path=/usr/bin/
function usage(){
echo "$0 {start|stop|restart}"
exit 1
}
[ $# -ne 1 ] && usage
function start_mysql(){
$path/mysqld_safe --user=mysql &
if [ $? -eq 0 ]
then
action "start mysql" /bin/true
else
action "start mysql" /bin/false
fi
}
function stop_mysql(){
mysqladmin -u root -p123 shutdown
if [ $? -eq 0 ]
then
action "stop mysql" /bin/true
else
action "stop mysql" /bin/false
fi
}
if [ "$1" == "start" ]
then
start_mysql
elif [ "$1" == "stop" ]
then
stop_mysql
elif [ "$1" == "restart" ]
then
stop_mysql
start_mysql
"start_db.sh" 43L, 678C written
You have new mail in /var/spool/mail/root
[root@node01 day8]# sh start_db.sh stop
170803 02:44:23 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
stop mysql [ OK ]
[root@node01 day8]# ps -ef|grep mysql
root 7175 93648 0 02:44 pts/5 00:00:00 grep mysql
[root@node01 day8]# sh start_db.sh start
start mysql [ OK ]
[root@node01 day8]# 170803 02:44:32 mysqld_safe Logging to '/var/log/mysqld.log'.
170803 02:44:32 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
[root@node01 day8]#
[root@node01 day8]# ps -ef|grep mysql
root 7179 1 0 02:44 pts/5 00:00:00 /bin/sh /usr/bin//mysqld_safe --user=mysql
mysql 7264 7179 0 02:44 pts/5 00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
root 7278 93648 0 02:44 pts/5 00:00:00 grep mysql
优化脚本
[root@node01 day8]# vi start_db.sh sart
2 files to edit
#!/bin/sh
. /etc/init.d/functions
path=/usr/bin/
pass=123
user=root
function usage(){
echo "$0 {start|stop|restart}"
exit 1
}
[ $# -ne 1 ] && usage
function start_mysql(){
$path/mysqld_safe --user=mysql & >/dev/null 2>&1
if [ $? -eq 0 ]
then
action "start mysql" /bin/true
else
action "start mysql" /bin/false
fi
}
function stop_mysql(){
mysqladmin -u$user -p$pass shutdown >/dev/null 2>&1
if [ $? -eq 0 ]
then
action "stop mysql" /bin/true
else
action "stop mysql" /bin/false
fi
}
if [ "$1" == "start" ]
then
start_mysql
elif [ "$1" == "stop" ]
then
stop_mysql
elif [ "$1" == "restart" ]
then
stop_mysql
start_mysql
[root@node01 day8]# sh start_db.sh start
start mysql [ OK ]
You have new mail in /var/spool/mail/root
[root@node01 day8]# 170803 02:46:55 mysqld_safe Logging to '/var/log/mysqld.log'.
170803 02:46:55 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
去除启动时的log
[root@node01 day8]# cat start_db.sh
#!/bin/sh
. /etc/init.d/functions
path=/usr/bin/
pass=123
user=root
function usage(){
echo "$0 {start|stop|restart}"
exit 1
}
[ $# -ne 1 ] && usage
function start_mysql(){
$path/mysqld_safe --user=mysql >/dev/null 2>&1 &
if [ $? -eq 0 ]
then
action "start mysql" /bin/true
else
action "start mysql" /bin/false
fi
}
function stop_mysql(){
mysqladmin -u$user -p$pass shutdown >/dev/null 2>&1
if [ $? -eq 0 ]
then
action "stop mysql" /bin/true
else
action "stop mysql" /bin/false
fi
}
if [ "$1" == "start" ]
then
start_mysql
elif [ "$1" == "stop" ]
then
stop_mysql
elif [ "$1" == "restart" ]
then
stop_mysql
start_mysql
else
usage
fi
把脚本copy 到/etc/init.d/下
mv start_db.sh mysqld
chmod +x mysqld
实现/etc/init.d/mysql01 start启动,并且通过chkconfig设置开机自启动和关闭。
#!/bin/sh
# chkconfig: 2345 21 60 (启动顺序不要和已有的冲突)
# description: start mysql and stop mysql scripts.
[root@node01 day8]# cp start_db.sh /etc/init.d/mysql01
[root@node01 day8]# chkconfig --add mysql01
[root@node01 day8]# chkconfig --list mysql01
mysql01 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@node01 day8]# ll /etc/rc.d/rc3.d/ |grep mysql01
lrwxrwxrwx 1 root root 17 Aug 3 03:26 S21mysql01 -> ../init.d/mysql01
相关文章推荐
- shell学习二十三--case语句编写mysql启动脚本
- 使用if语句编写Shell脚本
- mysql5.6.7多实例安装、配置的详细讲解分析及shell启动脚本的编写
- shell学习十四--编写监控mysql脚本
- shell脚本的使用---if变量编写lamp管理脚本
- Linux-Shell脚本编程-学习-5-Shell编程-使用结构化命令-if-then-else-elif
- oracle学习笔记(二):编写shell脚本启动oracle
- ubuntu编写开机自启动shell脚本
- 创建Shell脚本方便MySQL服务端启动
- 脚本学习-----常用shell脚本if参数
- SHELL编写redis启动脚本
- shell脚本编程之for语句、if语句使用介绍
- 使用交互式shell脚本实现对DNS服务的管理以及启动
- shell脚本编程之for语句、if语句使用介绍
- shell脚本学习-为什么使用 shell 编程
- Shell 学习(一、编写shell脚本和执行)
- 系统启动执行脚本-------shell学习(14)
- hadoop相关启动脚本分析 有助于troubleshoot,顺便学习shell
- 在Shell脚本中编写AWK脚本实现数据提取—注意格式、awk中使用shell变量、awk中数字字符串转换成数字、awk中字符串相等比较
- Shell脚本学习--护眼程序编写心得