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

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://nolinux.blog.51cto.com/4824967/1440414

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


脚本用途:

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

脚本说明:

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

该脚本使用注意事项:

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版本

以下为脚本内容:

#!/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。

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

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

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

帮助信息如下:

1.jpg

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

2.jpg

正常多实例安装

3.jpg

4.jpg

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

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

转自:http://nolinux.blog.51cto.com/4824967/1440414

原创文章,作者:s19930811,如若转载,请注明出处:http://www.178linux.com/1956

(1)
上一篇 2016-08-15 12:12
下一篇 2016-08-15 12:12

相关推荐

  • LAMP+NFS实现双web服务负载均衡

        一、实验拓扑          二、系统环境      1、主机A、主机B、主机C:CentOS 6.5        测试PC:         Windows 7 旗舰…

    Linux干货 2015-07-06
  • 关于大型网站技术演进的思考(十二)–网站静态化处理—缓存(4)

    原文出处: 夏天的森林   上篇我补充了下SSI的知识,SSI是一个十分常见的技术,记得多年前我看到很多门户网站页面的后缀是.shtml,那么这就说明很多门户网站都曾经使用过SSI技术,其实现在搜狐网站也还在用shtml,如下图所示: 由此可见SSI在互联网的应用还是非常广泛的。其实互联网很多网页如果我们按照动静分离策略拆分,绝…

    2015-03-11
  • 初学Linux之文本处理工具和正则表达

    1、各种文本工具来查看、分析、统计文本 
    2、grep 
    3、正则表达式和扩展正则表达式 
    4、egrep

    2017-12-09
  • N22-第二周作业

    Linux上的文件管理类命令有哪些,其常用使用方法及其相关示例演示。 文件管理类命令有:cp,mv,rm cp:复制文件或者目录   单源复制:cp [OPTION]… [-T] SOURCE DEST        多源复制:cp [OPTION]… SOURCE… DI…

    Linux干货 2016-09-19
  • shell脚本执行

    shell脚本执行 对于我们这些小白而讲,shell脚本无疑是最令人头疼的事了,我再这里讲一下shell脚本运行要注意的哪些。 shell脚本就好像c++,java代码一样,按照从上到下然后从左到右的顺序执行。就像我们日常读书一样。如果shell命令遇见了子进程(嵌套代码),程序就会跟着顺序进入子进程。当子进程完成后,回到父进程,并且把父进程所需要的参数提交…

    2017-06-05
  • 文件搜索之find的使用

    一.find介绍:   find是Linxu中一个十分常用的文件查找工具,其工作原理是通过遍历启起始路径下文件层级结构完成的,其特点是查找速度慢,能做到实时查找和精确查找。 二.使用方法: find  start_path search_conditions  action 1.statr_path:所要查找的文件的起始位置,默…

    Linux干货 2016-11-01