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.www58058.com/1956

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

相關推薦

  • VIM編輯器用法及練習

    VIM編輯器用法及練習 什么是vim? vim是從vi發展出來的一個文本編輯器。代碼補全、編譯及錯誤跳轉等方便編程的功能特別豐富,在程序員中被廣泛使用,和Emacs并列成為類Unix系統用戶最喜歡的文本編輯器。 Vim的第一個版本由布萊姆·米勒在1991年發布。最初的簡稱是Vi IMitation,隨著功能的不斷增加,正式名稱改成了Vi IMproved?,F…

    Linux干貨 2016-10-30
  • 馬哥教育網絡班20期+第2周課程練習

    1、Linux上的文件管理類命令都有哪些,其常用的使用方法及其相關示例演示。   答:文件操作     ls 列出目錄下的文件名      ls -l /     touch 文件不存在時生成該文件      touch a.txt   &…

    Linux干貨 2016-06-23
  • rsyslog講解

    rsyslog: 日志:歷史日志 syslog(服務):syslogd(系統日志)、klogd(內核日志) c/s架構;服務,可監聽于某套接字,幫其他主機記錄日志信息 日志格式 /etc/rsyslog.conf facitlity.priority         &…

    Linux干貨 2016-10-28
  • linux文本處理三劍客—grep

      cat:concatenate 文本文件查看工具 cat [option] filename… -n:給顯示出來的文本行加上編號 -b:非空行編號 -V:顯示 ^ -E:顯示行結束符$ -T:顯示制表符 -A:顯示所有控制符-A=-VET -s:壓縮連續…

    系統運維 2016-08-05
  • Linux之進程和計劃任務

    進程的概念     內核的功用:進程管理、文件系統、網絡功能、內存管理、驅動程序、 安全功能等     Process: 運行中的程序的一個副本,是被載入內存的一個指令集合進程ID(Process ID,PID)號碼被用來標記各個進程    &nbs…

    Linux干貨 2016-10-09
  • RAID簡介

    一,什么是raid? RAID(Redundant Arrays of Independent Disks)中文叫磁盤陣列。 簡單來講就是把很多的硬盤組織在一起來使用。        RAID技術分為幾種不同的等級,分別可以提供不同的速度,安全性和性價比。根據實際情況選擇適當的RAID級別可以滿足用戶對存儲系統可用性、…

    Linux干貨 2016-06-22
欧美性久久久久