Linux基礎入門之mysql集群搭建實戰(一)
一鍵自動安裝mysql 5.7
shell腳本自動化安裝二進制mysql-5.7
本節主要用到四個shell腳本 和 一臺資料存儲機器(IP:192.168.42.26)
- install_mysql.sh 自動化安裝mysql腳本
- ntpdate.sh 時間同步腳本
- system_init.sh 系統初始化腳本
- yum.sh yum源配置腳本
執行步驟:
1.創建目錄和下載腳本
mkdir -p /server/script
cd /server/script
wget -c http://192.168.42.26/script/yum.sh
wget -c http://192.168.42.26/script/install_mysql.sh
wget -c http://192.168.42.26/script/ntpdate.sh
wget -c http://192.168.42.26/script/create_bash.sh
wget -c http://192.168.42.26/script/system_init.sh
wget -c http://192.168.42.26/script/change_ip.sh
wget -c http://192.168.42.26/script/change_hostname.sh
2.執行初始化腳本
bash system_init.sh
3.執行自動安裝mysql腳本
bash install_mysql.sh
4.輸入mysql命令就能進入mysql操作
注:最好使用干凈的系統,避免沒必要的錯誤(如果沒有msyql環境變量,需在當前shell執行source /etc/profile.d/mysql.sh 或者 退出當前shell,重新登錄)
主要腳本:
ntpdate.sh
#!/bin/bash
if ! `rpm -q ntp &>/dev/null` ; then
yum install ntp -y
fi
/usr/sbin/ntpdate 172.16.0.1 && /usr/sbin/hwclock -w
yum.sh
#!/bin/bash
# Filename: yum.sh
# Revision: 1.1
# Date: 2017/05/03
# Author: Srayban
# Email: 626612631@qq.com
# Website: no
# Description: 自動生成yum源
. /etc/init.d/functions
YUMPATH="/etc/yum.repos.d"
DIRNAME="back"
REPONAME="CentOS-Base.repo"
EPELNAME="epel.repo"
YUMDOWN="http://192.168.42.26/install_package/down/"
function error(){
if [ $1 -ne 0 ];then
echo $2
exit 4
fi
}
if [ ! -d ${YUMPATH}/${DIRNAME} ];then
mkdir -p ${YUMPATH}/${DIRNAME} &> /dev/null
error $? "${YUMPATH}/${DIRNAME} is failed"
fi
/usr/bin/rm -f ${YUMPATH}/$REPONAME &> /dev/null
/usr/bin/rm -f ${YUMPATH}/$EPELNAME &>/dev/null
if [ ! -f ${YUMPATH}/${REPONAME} ];then
cd ${YUMPATH}
find ./ -maxdepth 1 -type f | xargs -i mv {} back/ &> /dev/null
error $? "mv script is failed."
wget -c ${YUMDOWN}/${REPONAME} &> /dev/null
wget -c ${YUMDOWN}/${EPELNAME} &> /dev/null
if [ $? -eq 0 ];then
action "${REPONAME} create is success." /bin/true
else
action "${REPONAME} create is failed." /bin/false
fi
else
echo "${REPONAME} is exits."
fi
system_init.sh
#!/bin/bash
# Filename: system_init.sh
# Revision: 1.1
# Date: 2017/05/13
# Author: Srayban
# Email: 626612631@qq.com
# Website: no
# Description: 系統初始化
. /etc/init.d/functions
DOWNADD="http://192.168.42.26/script"
#錯誤顯示
function error_show(){
echo "$1"
exit 3
}
#下載腳本功能
if [ "$1" == "wget" ];then
wget -c $DOWNADD/$2 &> /dev/null
[ $? -eq 0 ] && echo "$2 download is ok" && echo 0 || error_show "$1 download error."
fi
#關閉防火墻
function close_firewalld(){
/usr/bin/systemctl stop firewalld.service &> /dev/null
/usr/bin/systemctl disable firewalld.service &> /dev/null
}
#關閉seLinux
function close_seLinux(){
setenforce 0
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/seLinux/config
}
#內核調優
function sysctl_optimization(){
[ -f /etc/sysctl.conf.bak ] && error_show "sysctl.conf.back is exist."
/usr/bin/cp /etc/sysctl.conf /etc/sysctl.conf.bak
cat>>/etc/sysctl.conf<<EOF
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem=4096 87380 16777216
net.ipv4.tcp_wmem=4096 65536 16777216
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768
net.ipv4.tcp_syncookies=1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout=1
net.ipv4.tcp_keepalive_time=1200
net.ipv4.tcp_max_syn_backlog = 65536
net.ipv4.ip_local_port_range = 1024 65535
EOF
/sbin/sysctl -p
}
#初始化方法
function init(){
[ -f yum.sh ] && /bin/bash yum.sh || error_show "Missing yum.sh file."
[ -f ntpdate.sh ] && /bin/bash ntpdate.sh || error_show "Missing ntpdate.sh file."
close_firewalld;
sysctl_optimization
}
init
unset DOWNADD
install_mysql.sh
#!/bin/bash
# Filename: install_mysql.sh
# Revision: 1.1
# Date: 2017/05/13
# Author: Srayban
# Email: 626612631@qq.com
# Website: no
# Description: 一鍵安裝mysql
. /etc/init.d/functions
APPNAME="mysql"
APPUSER="mysql"
APPPATH="/application"
VERSION="5.7.16"
APPTARNAME="mysql-${VERSION}.tar.gz"
DOWNADDRESS="http://192.168.42.26/install_package/down"
#查看是否安裝mariadb 有就卸載
if `rpm -q mariadb &>/dev/null` ; then
rpm -e --nodeps mariadb
fi
#創建mysql用戶
if ! `id ${APPUSER} &>/dev/null` ; then
/usr/sbin/useradd -s /sbin/nologin -M ${APPUSER}
fi
#創建應用目錄
[ ! -d ${APPPATH} ] && mkdir ${APPPATH}
cd ${APPPATH}
#下載mysql二進制安裝包
if [ ! -f ${APPPATH}/${APPTARNAME} ] ;then
wget -c ${DOWNADDRESS}/${APPTARNAME}
fi
#安裝expect
/usr/bin/yum install expect expect-devel -y
#解壓安裝
[ -d mysql-${VERSION} ] && rm -rf mysql-${VERSION}
/usr/bin/tar xvf ${APPTARNAME}
/usr/bin/mv mysql-${VERSION}-* mysql-${VERSION}
cd ${APPPATH}/mysql-${VERSION}
installfile=/application/mysql.install.txt
./bin/mysqld --user=${APPUSER} --basedir=/application/mysql-${VERSION}/ --datadir=/application/mysql-${VERSION}/data --initialize &> $installfile
cat $installfile
passwd=`cat ${installfile} | tail -1 | cut -d" " -f11`
/usr/bin/mv -rf /etc/my.cnf /etc/my.cnf.back.old &>/dev/null
cd support-files/
[ -f /etc/my.cnf ] && mv /etc/my.cnf{,.back.$(date +"%Y%d%m%H%M%S")} &> /dev/null
/usr/bin/cp my-default.cnf /etc/my.cnf
/usr/bin/cp mysql.server /etc/init.d/mysqld
cd ${APPPATH}/
ln -s mysql-${VERSION} mysql
#編輯配置文件
cat << EOF >/etc/my.cnf
[client]
port = 3306
socket =/var/lib/mysql/mysql.sock
default-character-set=utf8mb4
[mysqld]
port = 3306
socket = /var/lib/mysql/mysql.sock
basedir = /application/mysql
datadir = /application/mysql/data
skip-external-locking
skip_name_resolve=1
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
thread_cache_size = 8
query_cache_size = 8M
tmp_table_size = 16M
performance_schema_max_table_instances = 500
explicit_defaults_for_timestamp = true
#skip-networking
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
log-bin=mysql-bin
binlog_format=mixed
server-id = 1
expire_logs_days = 10
early-plugin-load = ""
#loose-innodb-trx=0
#loose-innodb-locks=0
#loose-innodb-lock-waits=0
#loose-innodb-cmp=0
#loose-innodb-cmp-per-index=0
#loose-innodb-cmp-per-index-reset=0
#loose-innodb-cmp-reset=0
#loose-innodb-cmpmem=0
#loose-innodb-cmpmem-reset=0
#loose-innodb-buffer-page=0
#loose-innodb-buffer-page-lru=0
#loose-innodb-buffer-pool-stats=0
#loose-innodb-metrics=0
#loose-innodb-ft-default-stopword=0
#loose-innodb-ft-inserted=0
#loose-innodb-ft-deleted=0
#loose-innodb-ft-being-deleted=0
#loose-innodb-ft-config=0
#loose-innodb-ft-index-cache=0
#loose-innodb-ft-index-table=0
#loose-innodb-sys-tables=0
#loose-innodb-sys-tablestats=0
#loose-innodb-sys-indexes=0
#loose-innodb-sys-columns=0
#loose-innodb-sys-fields=0
#loose-innodb-sys-foreign=0
#loose-innodb-sys-foreign-cols=0
default_storage_engine = InnoDB
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci
innodb_file_per_table = 1
#innodb_data_home_dir = /application/mysql/data
#innodb_data_file_path = ibdata1:10M:autoextend
#innodb_log_group_home_dir = /application/mysql/data
#innodb_buffer_pool_size = 16M
#innodb_log_file_size = 5M
#innodb_log_buffer_size = 8M
#innodb_flush_log_at_trx_commit = 1
#innodb_lock_wait_timeout = 50
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
[mysqld_safe]
pid-file=/application/mysql/data/mysqld.pid
EOF
#添加環境變量
echo "export PATH="'$PATH'":${APPPATH}/mysql/bin" >/etc/profile.d/${APPNAME}.sh
/bin/bash /etc/profile.d/${APPNAME}.sh && . /etc/profile.d/${APPNAME}.sh
service mysqld start
#加入開機自啟動
systemctl is-enabled mysqld
#修改mysql 密碼
function change_mysql_passwd(){
/bin/expect -c "
set time 30
spawn /application/mysql/bin/mysqladmin -u root -p password \"root\"
expect {
\"*yes/no\" { send \"yes\r\"; exp_continue }
\"*password:\" { send \"$passwd\r\" }
}
interact
expect eof " >/dev/null 2>&1 ;
if [ $? -eq 0 ];then
action "mysql password changes succeeded" /bin/true
else
action "mysql password changes fail" /bin/false
fi
}
change_mysql_passwd
cat << EOF >/root/.my.cnf
[client]
default-character-set=utf8mb4
socket =/var/lib/mysql/mysql.sock
user=root
host=localhost
password=root
EOF
#設置權限
cd ${APPPATH}/
chown -R ${APPUSER}:${APPUSER} mysql-${VERSION}
unset passwd
unset installfile
unset APPNAME
unset APPUSER
unset APPPATH
unset VERSION
unset APPTARNAME
unset DOWNADDRESS
action "MySQL Installation successful" /bin/true