订阅本站
收藏本站
微博分享
QQ空间分享

一键搭建mysql集群系列一

alen 分类:Linux入门 时间:2018/12/21 22:16:53 浏览: 评论: 加入收藏

一键自动安装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.创建目录和下载脚本

123456789 mkdir -p /server/scriptcd /server/scriptwget -c http://192.168.42.26/script/yum.shwget -c http://192.168.42.26/script/install_mysql.shwget -c http://192.168.42.26/script/ntpdate.shwget -c http://192.168.42.26/script/create_bash.shwget -c http://192.168.42.26/script/system_init.shwget -c http://192.168.42.26/script/change_ip.shwget -c http://192.168.42.26/script/change_hostname.sh

2.执行初始化脚本

1 bash system_init.sh

3.执行自动安装mysql脚本

1 bash install_mysql.sh

4.输入mysql命令就能进入mysql操作

注:最好使用干净的系统,避免没必要的错误(如果没有msyql环境变量,需在当前shell执行source /etc/profile.d/mysql.sh 或者 退出当前shell,重新登录)

主要脚本:

ntpdate.sh

12345 #!/bin/bashif ! `rpm -q ntp &>/dev/null` ; then   yum install ntp -yfi/usr/sbin/ntpdate 172.16.0.1 && /usr/sbin/hwclock -w

yum.sh

12345678910111213141516171819202122232425262728293031323334353637383940414243444546 #!/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/functionsYUMPATH="/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     fielse     echo "${REPONAME}  is exits."fi

system_init.sh

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 #!/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/functionsDOWNADD="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}#关闭selinuxfunction 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.bakcat>>/etc/sysctl.conf<<EOFnet.ipv4.tcp_timestamps = 0net.ipv4.tcp_synack_retries = 2net.ipv4.tcp_syn_retries = 2net.ipv4.tcp_mem = 94500000 915000000 927000000net.ipv4.tcp_max_orphans = 3276800net.core.wmem_default = 8388608net.core.rmem_default = 8388608net.core.rmem_max = 16777216net.core.wmem_max = 16777216net.ipv4.tcp_rmem=4096 87380 16777216net.ipv4.tcp_wmem=4096 65536 16777216net.core.netdev_max_backlog = 32768net.core.somaxconn = 32768net.ipv4.tcp_syncookies=1net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_tw_recycle = 1net.ipv4.tcp_fin_timeout=1net.ipv4.tcp_keepalive_time=1200net.ipv4.tcp_max_syn_backlog = 65536net.ipv4.ip_local_port_range = 1024 65535EOF /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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 #!/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  mariadbfi#创建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/nullcd 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        = 3306socket      =/var/lib/mysql/mysql.sockdefault-character-set=utf8mb4 [mysqld]port        = 3306socket      = /var/lib/mysql/mysql.sockbasedir =  /application/mysqldatadir = /application/mysql/dataskip-external-lockingskip_name_resolve=1key_buffer_size = 16Mmax_allowed_packet = 1Mtable_open_cache = 64sort_buffer_size = 512Knet_buffer_length = 8Kread_buffer_size = 256Kread_rnd_buffer_size = 512Kmyisam_sort_buffer_size = 8Mthread_cache_size = 8query_cache_size = 8Mtmp_table_size = 16Mperformance_schema_max_table_instances = 500 explicit_defaults_for_timestamp = true#skip-networkingmax_connections = 500max_connect_errors = 100open_files_limit = 65535 log-bin=mysql-binbinlog_format=mixedserver-id   = 1expire_logs_days = 10early-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 = InnoDBcharacter-set-server=utf8mb4collation-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]quickmax_allowed_packet = 16M [mysql]no-auto-rehash [myisamchk]key_buffer_size = 20Msort_buffer_size = 20Mread_buffer = 2Mwrite_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=utf8mb4socket =/var/lib/mysql/mysql.sockuser=roothost=localhostpassword=rootEOF  #设置权限cd ${APPPATH}/chown -R ${APPUSER}:${APPUSER} mysql-${VERSION} unset passwdunset installfileunset APPNAMEunset APPUSERunset APPPATHunset VERSIONunset APPTARNAMEunset DOWNADDRESS   action "MySQL Installation successful" /bin/true

TAG:

文章评论

留言与评论(共有 0 条评论)
   
验证码: