阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Linux基础入门之mysql集群搭建实战(一)

219次阅读
没有评论

共计 7654 个字符,预计需要花费 20 分钟才能阅读完成。

一键自动安装 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

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-12-03发表,共计7654字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中