编写mysql多实例启动脚本
脚本原理:
启动MySQL动作:
mysqld_safe来执行启动
停止MySQL动作:
使用mysqladmin来执行停止动作
重启的MySQL动作:
原理就是先停止,然后再启动
但是要注意:简单的sleep来等待停止和启动过程极可能出现在短时间内重复运行重启,mysqld_safe报错提示已经启动了一个实例的问题
这里可以参考MySQL提供的默认启动脚本mysqld.service中的wait_for_pid()这个函数。
以下是启动脚本,效果如图:
脚本:
1 #!/bin/sh
2 #init
3 port=3306
4 mysql_user="root"
5 mysql_pwd="test"
6 CmdPath="/usr/local/mysql-5.5.62/bin/"
7 mysql_sock="/data/${port}/mysqld.sock"
8 pid_file="/data/${port}/mysqld.pid"
9 service_startup_timeout=900
10 PATH=/sbin:/usr/sbin:/bin:/usr/bin:$CmdPath:$PATH
11 export PATH
12 #print success
13 echo_info_ok(){
14 echo -e "[\e[0;32;1m success \e[0;32;0m]"
15 }
16
17 #print faild
18 echo_info_false(){
19 echo -e "[\e[0;31;1m failed \e[0;31;0m]"
20 }
21 #wait for pid
22 wait_for_pid(){
23 verb="$1" # created | removed
24 pid="$2" # process ID of the program operating on the pid-file
25 pid_file_path="$3" # path to the PID file.
26 i=0
27 flag="by checking again"
28 while test $i -ne $service_startup_timeout;do
29 case "$verb" in
30 'created')
31 # wait for a PID-file to pop into existence.
32 test -s "$pid_file_path" && i='' && break
33 ;;
34 'removed')
35 # wait for this PID-file to disappear
36 test ! -s "$pid_file_path" && i='' && break
37 ;;
38 *)
39 echo "wait_for_pid () usage: wait_for_pid created|removed pid pid_file_path"
40 exit 1
41 ;;
42 esac
43
44 # if server isn't running, then pid-file will never be updated
45 if test -n "$pid"; then
46 if kill -0 "$pid" 2>/dev/null; then
47 : # the server still runs
48 else
49 # The server may have exited between the last pid-file check and now.
50 if test -n "$avoid_race_condition"; then
51 flag=""
52 continue # Check again.
53 fi
54 return 1 # not waiting any more.
55 fi
56 fi
57 sleep 1
58 done
59 }
60
61 #startup function
62 function_start_mysql(){
63 if [ ! -e "$mysql_sock" ];then
64 printf "Starting MySQL..."
65 /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null &
66 wait_for_pid created "$!" "$pid_file"
67 if [ $? -eq 0 ];then
68 echo_info_ok
69 else
70 echo_info_false
71 fi
72 else
73 printf "MySQL is running...\n"
74 exit
75 fi
76 }
77
78 #stop function
79 function_stop_mysql(){
80 if [ ! -e "$mysql_sock" ];then
81 printf "MySQL is stopped...\n"
82 exit
83 else
84 printf "Stoping MySQL..."
85 ${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S ${mysql_sock} shutdown
86 wait_for_pid removed "$!" "$pid_file"
87 if [ $? -eq 0 ];then
88 echo_info_ok
89 else
90 echo_info_false
91 fi
92 fi
93 }
94
95 #restart function
96 function_restart_mysql(){
97 if [ ! -e $mysql_sock ];then
98 printf "Restarting MySQL..\n"
99 function_start_mysql
100 else
101 printf "Restarting MySQL...\n"
102 function_stop_mysql
103 if [ $? -eq 0 ];then
104 function_start_mysql
105 else
106 printf "Starting MySQL"
107 echo_info_false
108 fi
109 done
110 fi
111 }
112
113 #main
114 case $1 in
115 start)
116 function_start_mysql
117 ;;
118 stop)
119 function_stop_mysql
120 ;;
121 restart)
122 function_restart_mysql
123 ;;
124 *)
125 printf "Usage: /data/${port}/mysql {start|stop|restart}\n"
126 esac
编写mysql多实例启动脚本的更多相关文章
- MySQL 多实例启动和关闭脚本
DB: 5.5.14 OS:CentOS 6.3 在MySQL多实例中,一个一个启动灰常麻烦,因此写个简单的启动和关闭脚本 启动脚本:start.sh #!/bin/bash for port in ...
- mysql 5.7 启动脚本
最近这段时间,在看mysql,安装了,也应用过,对于生产环境中,一般都选择使用source code安装,在安装的时候可以自定义相关路径和内容,对于生产环境来说更有效.相对于mysql 5.5的安装, ...
- mysql多实例启动过程
单机多实例,是基本的测试环境 01.myslq提供单机管理多节点 02.启动mysql多实例 03.观察进程
- mysql多实例启动、关闭
启动(指定参数文件): [root@mysql01 ~]# mysqld_safe --defaults-file=/data/3306/my.cnf & [root@mysql01 ~]# ...
- MySQL多实例启动停止
原文地址:http://wolfword.blog.51cto.com/4892126/1241304/ 说明:本实验以MySQL 5.1为例来实验. 1.安装MySQL 5.1 yum instal ...
- mysql多实例安装脚本
#! /bin/bash # v.mysql-5.6.30-linux-glibc2.5-x86_64.tar.gz # only install master mysql # time:2016-0 ...
- Mariadb多实例启动脚本
#!/bin/bash port=3306 mysql_user="root" mysql_pwd="centos" cmd_path="/app/m ...
- mysql多实例(个人的情况,不是大众的)里面有配置好的脚本+主从复制
[root@DB-S ~]# ll /usr/local/|grep mysql lrwxrwxrwx. 1 root root 21 Jun 14 01:52 mysql -> /alidat ...
- Mysql多实例 安装以及配置
MySQL多实例 1.什么是MySQL多实例 简单地说,Mysql多实例就是在一台服务器上同时开启多个不同的服务端口(3306.3307),同时运行多个Mysql服务进程,这些服务进程通过不同的soc ...
随机推荐
- 深入理解java虚拟机笔记Chapter4
JDK命令行工具 其中的重中之重是 jstat 命令!而它最常用的参数就是 -gcutil,使用格式如下: jstat -gcutil [pid] [intervel] [count] 输出如下 D: ...
- Java如何使用while和for嵌套循环控制输出数据,使数据奇偶行不同
/* 题目1 使用Eclipse编写控制台应用程, 使用while循环在控制台打印10行10列的如下图形 □ □ □ □ □ □ □ □ □ □ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ □ □ □ □ ...
- python学习笔记05-条件分支与循环1
思考问题:100分制,90分以上为A,80-90为B,60-80为C,60以下为D,当用户输入成绩后,打印对应的字母 temp=input('请输入你的成绩:') score=int(temp) if ...
- SpringCloud 面试题 (持续更新、吐血推荐)
文章很长,建议收藏起来,慢慢读! 疯狂创客圈为小伙伴奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : <Netty Zookeeper Redis 高并发实战> 面试必备 + 大厂必备 ...
- SQL 查询语句总是先执行 SELECT?你们都错了
很多 SQL 查询都是以 SELECT 开始的.不过,最近我跟别人解释什么是窗口函数,我在网上搜索"是否可以对窗口函数返回的结果进行过滤"这个问题,得出的结论是"窗口函数 ...
- 小白学k8s(7)helm[v3]使用了解
helm使用 什么是helm 安装helm Helm V2 & V3 架构设计 配置kube config helm使用 添加仓库 helm安装nginx helm的核心概念 Chart Co ...
- 有趣的开源项目集结完毕,HelloGitHub 月刊第 63 期发布啦!
兴趣是最好的老师,HelloGitHub 让你对编程感兴趣! 简介 分享 GitHub 上有趣.入门级的开源项目. 这里有实战项目.入门教程.黑科技.开源书籍.大厂开源项目等,涵盖多种编程语言 Pyt ...
- 计算机网络的参考模型与5G协议
一.分层思想 二.OSI七层参考模型 三.FPC/IP五层模型 四.数据的封装过程与PDU(协议数据单元) 五.数据的解封装过程 六.各层间通信与设备与层的对应关系 七.总结 一.分层思想 将复杂的 ...
- javascript的getTime函数
<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content=&q ...
- Redis的数据安全与性能保障
1.持久化选项 Redis提供了2种不同的持久化方法来将数据存储到硬盘里面.一种方法叫快照(snapshotting),它可以将存在于某一时刻的所有数据都写入硬盘里.另一种方法叫只追加文件(appen ...