一键搭建mysql集群系列一

一键自动安装mysql 5.7

shell脚本自动化安装二进制mysql-5.7

本节主要用到四个shell脚本 和 一台资料存储机器(IP:192.168.42.26)

  1. install_mysql.sh 自动化安装mysql脚本
  2. ntpdate.sh 时间同步脚本
  3. system_init.sh 系统初始化脚本
  4. 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

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

(3)
sraybansrayban
上一篇 2017-05-13
下一篇 2017-05-13

相关推荐

  • 基于ansible role实现LAMP平台批量部署

    前言 作为运维人员,当面对几十台或上百台服务器需要修改某个参数或部署某个平台,你将从何入手呢?ansible的出现很好的解决了这一困扰,ansible基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。本文带来的是基于Ansible Role…

    Linux干货 2015-06-11
  • linux网络管理

    在 /etc/udev/rules.d/路径下有个70-persistent-ipoib.rules文件,;里边有关于网卡信息的配置,,,在里边可以更改网卡的名字 注意:修改完之后要在/etc/sysconfig/network-scripts/ifcfg-eth0中修改,不然不能使用。 在Centos6以后有一个服务NetworkManager,他可以自动…

    Linux干货 2016-09-06
  • 文件、目录练习

    将/etc/issue文件的内容转换为大写保存到/tmp/issue.out文件中: 将当前系统登录用户信息转换为大写后保存至/tmp/who.out文件中: 将/root/下文件列表,显示成一行,文件名之间用空格隔开: file1文件的内容为“1 2 3 4 5 6 7 8 9 10”计算出所有数字的总和: 处理字符串“xt.,| 1 jr#!$mn 2 …

    Linux干货 2016-08-05
  • vim的使用说明

    vim的使用说明 vim是一款功能丰富而强大的文本编辑器,vim是从 vi 发展出来的一个文本编辑器。其代码补全、编译及错误跳转等方便编程的功能特别丰富,在程序员中被广泛的使用。 vim的使用非常方便,使用方法为:vim [options] [file ..] 1、vim可以对指定文件进行编辑,也可以直接启动vim编辑器进行编辑,对于打开指定文件编辑时可以加…

    Linux干货 2016-08-12
  • 系统启动流程

    linux系统启动流程 内核的设计结构单内核:linux(线程–lwp轻量级进程)微内核:windows(支持真正意义上的多线程) 单内核:很多功能驱动都集成在一起 微内核:内核很小,功能单一。模块化 linux为了适应众多用户的不同硬件需求,linux内核在设计上采用模块化设计。可以动态加载模块。核心模块:ko 内核所独有的。共享对象:so 红…

    Linux干货 2016-09-19
  • Python from entry to abandon 4

        python from entry to abandon系列的收官之作    本篇博客将会介绍《简明Python教程》的最后四章内容。并在最后附上对于本书的个人评价和下阶段自学Python系列博客更新的计划。    13.异常   &nbsp…

    Linux干货 2017-04-07