例子:开发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