MySQL系列之一键安装脚本----单实例/多实例

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用系列 2核4GB
简介:

   最近在搞MySQL,由于经常测试一些东西。因此,就写了一个一键安装脚本。


脚本用途:

1
用于在CentOS /RHEL  6.x系统上快速部署出Mysql的单实例或者多实例环境


脚本说明:

1
2
3
4
该脚本运行情况良好
针对脚本中,每一步命令执行的正误判断以及提醒非常醒目,可协助执行者快速定位错误源
脚本诸多内容都以声明变量,增加了脚本的灵活性和扩展性
脚本以做模块化处理,对应功能对应函数,方便SA快速更改和了解该脚本


该脚本使用注意事项:

1
2
3
4
5
6
1、能够通公网或者mysql源码包已经放置到 /usr/local/src 目录下
2、本脚本运行环境要求yum源已经配好
3、注意使用的mysql版本,为了稳定期间,本脚本暂稳定支持5.5以后的mysql源码包。本脚本默认使用5.6.16版本的源码包。如果你需要使用其它版本,请更改MYSQL_SOFT变量以及源码包的下载路径
4、mysql安装默认位置为 /usr/local/mysql ,如需更改请自行修改INSTALL_PATH变量
5、由于我基本每条命令都有做注释,因此其它一些参数的修改,请自己研究,此处不再啰嗦
6、系统环境要求CentOS /RHEL  6.x版本


以下为脚本内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/bin/bash
#
# The script used in CentOS/RHEL 6. X system automatically deploy mysql single instance and multiple instances of the environment
# Written by sunsky
# Mail : nolinux@126.com
# QQ   : 274546888
# Date : 2014-7-19 14:23:00
#
/etc/init .d /functions
 
tac () {
if  [ $? == 0 ]; then
action  ''  /bin/true
else
action  ''  /bin/false
fi
}
 
pre_instance () {
echo  '  -- Add MySQL User<1>'
useradd  -r -u 306 mysql;tac
echo  '  -- Install Some Packages<1>'
yum  install  wget  make  cmake gcc gcc-c++ ncurses ncurses-devel perl -y &>  /dev/null ;tac
echo  '  -- Downloading MySQL<2>' 
cd  /usr/local/src ;tac
#wget http://cdn.mysql.com/Downloads/MySQL-5.6/${MYSQL_SOFT}.tar.gz;tac
echo  '  -- Unpack the source code package<2>'
tar  -zxf  /usr/local/src/ ${MYSQL_SOFT}. tar .gz -C  /usr/local/src/ ;tac
cd  /usr/local/src/ ${MYSQL_SOFT};tac
echo  '  -- Install MySQL<3>'
cmake -DCMAKE_INSTALL_PREFIX=$INSTALL_PATH -DINSTALL_DATADIR=$DATA_DIR -DDEFAULT_CHARSET=utf8 -DWITH_EXTRA_CHARSETS=all -DMYSQL_USER=mysql -DDEFAULT_COLLATION=utf8_general_ci &>  /dev/null ;tac
make  &> /dev/null ;tac
make  install  &>  /dev/null ;tac
echo  '  -- Change the directory owner and group<1>'
chown  -R mysql.mysql  /usr/local/mysql ;tac
echo  '  -- Create my.cnf<1>'
echo  | cp  /usr/local/src/ ${MYSQL_SOFT} /support-files/my-default .cnf  /etc/my .cnf ;tac
echo  '  -- Create Mysqld Scripts<2>'
echo  | cp  /usr/local/mysql/support-files/mysql .server  /etc/init .d /mysqld ;tac
chmod  +x  /etc/init .d /mysqld ;tac
#echo '  -- Start Mysqld Service Test<1>'
#sleep 3
#/etc/init.d/mysqld start > /dev/null;tac
#echo '  -- View MySQL Database Status<1>'
#/etc/init.d/mysqld status | grep "SUCCESS" > /dev/null;tac
#if [ $? == 0 ];then action '  -- MySQL Install Done!' /bin/true;else action '  -- MySQL Install Failed!' false;fi
#action '  -- MySQL Command Global Path<2>'
#echo 'export PATH=/usr/local/mysql/bin/:$PATH' >> /etc/profile;tac
#sleep 1
#source /etc/profile;tac
}
 
single_instance () {
echo  '   -- Initialized MySQL database, the default port 3306<1>'
$INIT_DB --datadir=$DATA_DIR &>  /dev/null ;tac 
echo  '   -- Start Mysqld Service Test<1>'
/etc/init .d /mysqld  start >  /dev/null ;tac
echo  '   -- View MySQL Database Status<1>'
/etc/init .d /mysqld  status |  grep  "SUCCESS"  /dev/null ;tac
if  [ $? == 0 ]; then  action  '   -- MySQL Install Done!'  /bin/true ; else  action  '   -- MySQL Install Failed!'  false ; fi
action  '   -- MySQL Command Global Path<2>'
echo  'export PATH=/usr/local/mysql/bin/:$PATH'  >>  /etc/profile ;tac
sleep  1
source  /etc/profile ;tac
}
 
single_of_multiple () {
action  '    -- Add Mysqld_Multi User<2>'
user= "mysql" ;tac
password= "sunsky" ;tac
action  '    -- Stop MySQL Database<1>'
/etc/init .d /mysqld  stop &>  /dev/null ;tac
action  "    -- Mysqld_multi Configure<2>"
sed  -i  's/^[^#]/#/g'  /etc/my .cnf;tac
cat  /etc/my .cnf << EOF
[mysqld_multi] 
mysqld =  /usr/local/mysql/bin/mysqld_safe
mysqladmin =  /usr/local/mysql/bin/mysqladmin
user = $user 
password = $password 
 
[mysqld3306]
server- id  = 1
port = 3306
socket =  /tmp/mysql3306 .sock
pid- file  /tmp/mysql3306 .pid
basedir =  /usr/local/mysql
datadir =  /usr/local/mysql/data
key_buffer_size = 16k
max_allowed_packet = 1M
table_open_cache = 4
sort_buffer_size = 64K
read_buffer_size = 256K
read_rnd_buffer_size = 256K
net_buffer_length = 2K
thread_stack = 128K
 
EOF
tac
echo  '    -- prepare my_default_print<1>'
echo  cp  /usr/local/mysql/bin/my_print_defaults  /usr/bin/ ;tac
sleep  1
action  "    -- Initialize multiple instance of the 3306 instance<1>"
echo  '    -- Start multiple instances of the 3306 instance<1>'
$MULTI_DB 3306;tac
sleep  5
action  "    -- Examples of 3306 authorized users<1>"
$MYSQL_DB -S  /tmp/mysql3306 .sock -e  "grant shutdown on *.* to '$user'@'%' identified by '$password';" ;tac
action  "    -- Instance 3306 has user and password to shutdown<1>"
}
 
multiple_instances () {
user= "mysql"
password= "sunsky"
for  port  in  $*; do
  netstat  -lntp |  grep  $port &>  /dev/null
  if  [ $? - eq  0 ]; then
   action  "    -- Instance $port is running<1>" 
  else
   cat  /etc/my .cnf |  grep  mysqld${port} >  /dev/null
   if  [ $? - eq  0 ]; then
    echo  "    -- $port instance is already exists,please input other port number!"
    $MULTI_DB $port && action  "   -- Instance $port open to complete"  /bin/true  || action  "   -- Instance $port open failed"  /bin/false
   else
cat  >>  /etc/my .cnf << EOF
[mysqld${port}]
server- id  = $[${port}%3305]
port = ${port}
socket =  /tmp/mysql ${port}.sock
pid- file  /tmp/mysql ${port}.pid
basedir =  /usr/local/mysql
datadir =  /usr/local/mysql/data ${port}
key_buffer_size = 16k
max_allowed_packet = 1M
table_open_cache = 4
sort_buffer_size = 64K
read_buffer_size = 256K
read_rnd_buffer_size = 256K
net_buffer_length = 2K
thread_stack = 128K
EOF
echo  "    -- Create $port Instance Datadir<2>"
   mkdir  -p ${DATA_DIR}${port};tac
   chown  -R mysql.mysql ${DATA_DIR}${port};tac
echo  "    -- Initialize multiple instance of the $port instance<1>"
   $INIT_DB --datadir=${DATA_DIR}${port} &>  /dev/null  ;tac 
echo  "    -- Start multiple instances of the $port instance<1>"
   $MULTI_DB $port && action  "    -- Instance $port open to complete"  || action  "    -- Instance $port open failed" ;tac
echo  "    -- Examples of $port authorized users"
   sleep  3
   $MYSQL_DB -S  /tmp/mysql ${port}.sock -e  "grant shutdown on *.* to '$user'@'localhost' identified by '$password';" && action  "    -- Instance $port has user and password to shutdown"  /bin/true || action  "    -- Instance $port has user and password to shutdown"  /bin/false
  fi
fi
done
}
 
 
# MySQL Install Path 
INSTALL_PATH= '/usr/local/mysql'
# MySQL DATA_DR
DATA_DIR= "$INSTALL_PATH/data"
# MySQL Command Path
INSTALL_DB= "$INSTALL_PATH/scripts/mysql_install_db"
MYSQL_DB= "$INSTALL_PATH/bin/mysql"
MYSQL_MULTI= "$INSTALL_PATH/bin/mysqld_multi"
INIT_DB= "$INSTALL_DB --user=mysql --basedir=$INSTALL_PATH --defaults-file=/etc/my.cnf"
MULTI_DB= "$MYSQL_MULTI --defaults-file=/etc/my.cnf start"
MYSQL_SOFT= 'mysql-5.6.16'
 
 
case  $ # in
0)
cat  << EOF
The system administrator, hello!
 
This is a key to  install  mysql single instance and multiple instances of the script
You can use the name  in  the script with the upper slogans, to define several instances installed!
 
Example:
[root@sunsky ~] # bash auto_install_mysql_instance.Sh 3306 3307
Install two mysql instance, port Numbers 3306 and 3307 respectively.
 
If you are familiar with shell, are  free  to change the script!If you are not familiar with, please  do  not change!
EOF
;;
1)
echo  '> Now begin to single instance database initialization'
echo  '>> STEP ONE : Prepare the MySQL Environment'
pre_instance
echo  '>>> STEP TWO : Install the MySQL single instance'
single_instance
echo  '>>>> SETP THREE : MySQL single instance installation is complete!' ;;
*)
echo  '> Now start multi-instance database initialization'
echo  '>> STEP ONE : Prepare the MySQL Environment'
pre_instance
single_instance
echo  '>>> STEP TWO : Install the MySQL single of multiple instance'
single_of_multiple
echo  '>>>> STEP THREE : Install the MySQL others of multiple instance'
multiple_instances $*
echo  '>>>>> SETP FOUR : MySQL multiple instance installation is complete'
;;
esac


以上就是脚本的内容,该脚本主体为5个函数組,分别是tac、pre_instance、single_instance、single_of_multiple和multiple_instances。

1
2
3
4
5
tac负责做脚本中命令执行的正误判断,方便定位错误源
pre_instance负责准备mysql安装的环境,比如用户创建、相关工具安装、mysql源码包准备、mysql软件安装等
single_instance负责初始化单实例数据库
single_of_multiple负责多实例环境下,对纯单实例的实例做修改
multiple_instances负责创建多实例环境


脚本中间声明了若干变量,这里不做介绍!

下面一个case语句用来调度执行我们整个脚本。如果你直接执行脚本,什么都不跟的话,默认会显示出帮助信息。

帮助信息如下:

wKiom1PKr7-yQ5yJAAI_JZ0lIVg872.jpg


下面我列举,正常安装单实例

wKioL1PKsHvgOi10AAKHaviAtBk955.jpg


正常多实例安装

wKiom1PKr3njKWdLAAH4PxM6vzg755.jpg

wKioL1PKsJXw6IlxAAKH2LRmaWo672.jpg


希望该脚本能对大家有所帮助!

如果发现脚本里面有什么欠妥当的地方,请及时告知我!谢谢!










本文转自 aaao 51CTO博客,原文链接:http://blog.51cto.com/nolinux/1440414,如需转载请自行联系原作者

相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
打赏
0
0
0
0
235
分享
相关文章
Docker Compose V2 安装常用数据库MySQL+Mongo
以上内容涵盖了使用 Docker Compose 安装和管理 MySQL 和 MongoDB 的详细步骤,希望对您有所帮助。
212 42
CentOS7仅安装部署MySQL80客户端
通过上述步骤,你可以在CentOS 7上成功安装并配置MySQL 8.0客户端。这个过程确保你能够使用MySQL客户端工具连接和管理远程的MySQL数据库,而不需要在本地安装MySQL服务器。定期更新MySQL客户端可以确保你使用的是最新的功能和安全修复。
327 16
【MySQL基础篇】MySQL概述、Windows下载MySQL8.0超详细图文安装教程
在这一章节,主要介绍两个部分,数据库相关概念及MySQL数据库的介绍、下载、安装、启动及连接。接着,详细描述了MySQL 8.0的版本选择与下载,推荐使用社区版(免费)。安装过程包括自定义安装路径、配置环境变量、启动和停止服务、以及客户端连接测试。此外,还提供了在同一台电脑上安装多个MySQL版本的方法及卸载步骤。最后,解释了关系型数据库(RDBMS)的特点,即基于二维表存储数据,使用SQL语言进行操作,格式统一且便于维护。通过具体的结构图展示了MySQL的数据模型,说明了数据库服务器、数据库、表和记录之间的层次关系。
【MySQL基础篇】MySQL概述、Windows下载MySQL8.0超详细图文安装教程
《docker高级篇(大厂进阶):1.Docker复杂安装详说》包括:安装mysql主从复制、安装redis集群
《docker高级篇(大厂进阶):1.Docker复杂安装详说》包括:安装mysql主从复制、安装redis集群
157 14
《docker基础篇:8.Docker常规安装简介》包括:docker常规安装总体步骤、安装tomcat、安装mysql、安装redis
《docker基础篇:8.Docker常规安装简介》包括:docker常规安装总体步骤、安装tomcat、安装mysql、安装redis
172 7
Windows Server 安装 MySQL 8.0 详细指南
安装 MySQL 需要谨慎,特别注意安全配置和权限管理。根据实际业务需求调整配置,确保数据库的性能和安全。
446 9
Linux安装jdk、mysql、redis
Linux安装jdk、mysql、redis
275 7
【YashanDB知识库】原生mysql驱动配置连接崖山数据库
【YashanDB知识库】原生mysql驱动配置连接崖山数据库
【YashanDB知识库】原生mysql驱动配置连接崖山数据库
docker拉取MySQL后数据库连接失败解决方案
通过以上方法,可以解决Docker中拉取MySQL镜像后数据库连接失败的常见问题。关键步骤包括确保容器正确启动、配置正确的环境变量、合理设置网络和权限,以及检查主机防火墙设置等。通过逐步排查,可以快速定位并解决连接问题,确保MySQL服务的正常使用。
128 82