MySQL5.7多例項自動化部署指令碼
一、安裝說明
------------------------------------------------------
mysql5.7.10_onekey_install.sh自動化部署指令碼支援mysql5.7.10初始化安裝,多例項建立,且使用經過優化後的my.cnf配置檔案和mysql.server啟動指令碼,該SHELL指令碼在CentOS6.5_x86_64作業系統測試通過。部署示意圖如下:
1、安裝方式
需要準備的檔案如下,放到同一個目錄下,然後執行shell指令碼
執行方式:
1 2 |
.
/mysql5
.7.10_onekey_install.sh 3307
埠自定義,要求整數,且不和伺服器埠衝突
|
1 2 3 4 5 6 |
# tree mysql5.7.10_onekey_install
mysql5.7.10_onekey_install
├── my.cnf
├── mysql-5.7.10-linux-glibc2.5-x86_64.
tar
.gz
├── mysql5.7.10_onekey_install.sh └── mysql.server
|
1、如果是首次安裝mysql,則會構建所需的所有預安裝環境並建立第一個例項;
2、如果不是首次安裝mysql,則會在原有基礎上建立多例項;
2、安裝軟體版本
- 作業系統:CentOS6.5_x86_64
- MySQL版本:mysql-5.7.10-linux-glibc2.5-x86_64.tar.gz (最新版本5.7.10)
3、安裝目錄規劃
上傳軟體包目錄:/home/zkyw/install
mysql程式目錄:/opt/application/mysql
mysql例項資料檔案目錄:/data/$Port
4、程式啟停操作
啟動mysql例項:/data/$Port/mysql.server start
停止mysql例項: /data/$Port/mysql.server stop
檢視mysql例項狀態:/data/$Port/mysql.server status
5、指令碼執行過程
第一步:對傳入的引數(埠號)做判斷,要求為整數且不能與伺服器已有埠號衝突;
第二步:如果伺服器還沒有安裝過mysql,則進行初始化安裝,自動配置所需安裝環境;
第三步:如果伺服器上已經安裝了mysql,則會建立多例項;
第四步:初始化安裝過程:刪除msyql相關的rpm包→安裝libaio包→建立安裝所需的使用者目錄等→安裝mysql→配置mysql
第五步:建立多例項過程:建立目錄並授權→安裝mysql→配置mysql
二、自動化部署指令碼
--------------------------------------------------------
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 |
#!/bin/sh
#auto install mysql5.7.10
#setting default directory
Port=$1
SoftDir=
"/home/zkyw/install"
tarbag=
"mysql-5.7.10-linux-glibc2.5-x86_64"
BaseDir=
"/opt/application/mysql"
DataDir=
"/data/$Port"
#remove before being installed mysql
function
rmMysql() {
yum -y erase mysql >
/dev/null
2>&1
yum -y erase mysql-server >
/dev/null
2>&1
ret=`rpm -aq |
grep
-i
"mysql-5"
|
wc
-l`
num=`rpm -aq |
grep
-i
"mysql-server"
|
wc
-l`
test
$ret -
eq
1 &&
echo
"mysql uninstall failed"
&&
exit
1
test
$num -
eq
1 &&
echo
"mysql-server uninstall failed"
&&
exit
2
}
#libaio package is needed for mysql5.7.10
function
chkEnv() {
yum -y
install
libaio >
/dev/null
2>&1
res=`rpm -aq|
grep
libaio |
wc
-l`
test
$res -
ne
1 &&
echo
"libaio package install failed..."
&&
exit
3
}
#create mysql user and group, authorization, extract
function
preInstall() {
/usr/sbin/groupadd
mysql
/usr/sbin/useradd
-r -g mysql -s
/bin/false
mysql
mkdir
-p $BaseDir
mkdir
-p $DataDir
/data
chown
mysql.mysql $DataDir
if
test
-f $SoftDir/$tarbag.
tar
.gz
then
cd
$SoftDir &&
tar
-zxf $tarbag.
tar
.gz
cd
$SoftDir/$tarbag &&
cp
-r * $BaseDir
else
echo
"$tarbag.tar.gz is not found..."
exit
10
fi
}
function
multPreInstall() {
mkdir
-p $DataDir
/data
chown
mysql.mysql $DataDir
}
function
install_mysql() {
#initialize mysql database
$BaseDir
/bin/mysqld
\
--initialize \
--user=mysql \
--basedir=$BaseDir \
--datadir=$DataDir
/data
\
--character-
set
-server=utf8 \
--collation-server=utf8_general_ci \
--initialize-insecure >
/dev/null
2>&1
}
#get my.cnf and start/stop script, attention alter parameters by your envionment
function
conf_mysql() {
cp
$SoftDir
/my
.cnf $DataDir
cp
$SoftDir
/mysql
.server $DataDir
/usr/bin/vim
$DataDir
/my
.cnf<<EOF >
/dev/null
2>&1
:%s
/3306/
$Port
/g
:wq
EOF
sed
-i
"s/port=3306/port=$Port/"
$DataDir
/mysql
.server
sed
-i
"s%CmdPath=\"\"%CmdPath=\"${BaseDir}\/bin\"%"
$DataDir
/mysql
.server
sed
-i
"s%mysql_sock=\"\"%mysql_sock=\"${DataDir}\/mysql.sock\"%"
$DataDir
/mysql
.server
chmod
600 $DataDir
/my
.cnf
chmod
700 $DataDir
/mysql
.server
$DataDir
/mysql
.server start >
/dev/null
2>&1
sleep
3
# ren=`netstat -natp|grep mysqld | grep "$1" | wc -l`
if
test
-e $DataDir
/mysql
.sock;
then
echo
"$DataDir/mysql.sock"
echo
-e
"\033[33;1mmysql install success...\033[0m"
pro=`
grep
$BaseDir
/root/
.bash_profile |
wc
-l`
if
test
"$pro"
-
ne
1;
then
sed
-i
"s%PATH=\$PATH\:\$HOME\/bin%PATH=\$PATH\:\$HOME\/bin\:$BaseDir\/bin%"
/root/
.bash_profile
source
/root/
.bash_profile
fi
else
echo
-e
"\033[31;1mmysql install failed...\033[0m"
fi
}
if
[[
"$1"
=~ ^[0-9]+$ ]];
then
inPort=`
netstat
-natp |
grep
"mysqld"
|
grep
"LISTEN"
|
awk
'{print $4}'
|
cut
-b 4-`
if
test
! -z
"$inPort"
;
then
for
myPort
in
$inPort
do
if
test
"$myPort"
-
eq
"$1"
;
then
echo
-e
"\033[33;1m$1 instance has already existed...\033[0m"
exit
1
fi
done
echo
-e
"\033[32;1m===========================================\033[0m"
echo
-e
"\033[32;1mStarting create new instance $1\033[0m"
multPreInstall
install_mysql
echo
-e
"\033[32;1m===========================================\033[0m"
echo
-e
"\033[32;1mconfiguration and starting $1 instance...\033[0m"
conf_mysql
else
echo
-e
"\033[32;1m===========================================\033[0m"
echo
-e
"\033[32;1mremove before being installed mysql...\033[0m"
rmMysql
echo
-e
"\033[32;1m===========================================\033[0m"
echo
-e
"\033[32;1minstall libaio package...\033[0m"
chkEnv
echo
-e
"\033[32;1m===========================================\033[0m"
echo
-e
"\033[32;1mget ready install envionment...\033[0m"
preInstall
echo
-e
"\033[32;1m===========================================\033[0m"
echo
-e
"\033[32;1mStarting install mysql ver5.7.10...\033[0m"
install_mysql
echo
-e
"\033[32;1m===========================================\033[0m"
echo
-e
"\033[32;1mconfiguration mysql and starting mysql...\033[0m"
conf_mysql
fi
else
echo
"Usage: $0 Port (Port is inteager)"
fi
|
三、MySQL優化後的配置檔案(my.cnf)
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 |
[client]
port = 3306
socket =
/data/3306/mysql
.sock
character-
set
-server = utf8mb4
[mysql]
no-auto-rehash
[mysqld]
user = mysql
port = 3306
character-
set
-server = utf8mb4
socket =
/data/3306/mysql
.sock
basedir =
/opt/application/mysql
datadir =
/data/3306/data
explicit_defaults_for_timestamp =
true
skip-ssl
secure-
file
-priv = NULL
lower_case_table_names = 1
back_log = 300
max_connections = 3000
max_connect_errors = 100
table_open_cache = 4096
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 16M
join_buffer_size = 16M
thread_cache_size = 16
query_cache_size = 128M
query_cache_limit = 4M
ft_min_word_len = 8
thread_stack = 512K
transaction_isolation = REPEATABLE-READ
tmp_table_size = 128M
max_heap_table_size = 128M
###*** slow query parameters
long_query_time = 6
slow_query_log
slow_query_log_file =
/data/3306/slow
.log
###*** binlog parameters
log-bin =
/data/3306/mysql-bin
binlog_cache_size = 4M
max_binlog_cache_size = 16M
max_binlog_size = 32M
binlog_format = row
expire_logs_days = 7
###*** relay-log parameters
#relay-log = /data/3307/relay-bin
#relay-log-info-file = /data/3307/relay-log.info
#master-info-repository = table
|