偷得浮生 偷得浮生

记录精彩的人生

目录
MySQL多实例二进制部署SHELL脚本
/  

MySQL多实例二进制部署SHELL脚本

#!/bin/bash
#
#********************************************************************
#Author:zhangzhuo
#QQ: 1191400158
#Date: 2021-01-30
#FileName:mysql5.7-8.0_install_multi.sh
#URL: https://www.zhangzhuo.ltd
#Description:The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
. /etc/init.d/functions

DIR=`pwd`
#URL可修改可写包下载地址http或者https,或者包名称,写包名称在哪执行脚本放在那
URL=https://repo.huaweicloud.com/mysql/Downloads/MySQL-5.7/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
NAME=`echo $URL | grep -Eo '[^/]+$'`
DIRNAME=`echo $NAME | sed -rn 's/(.*).tar.*/\1/p'`
#主数据库安装位置,可修改
DATA_DIR=/data/mysql
#多实例创建几个实例,可修改
NUM=5
NUM=$[3305+${NUM}]
FULL_NAME=${DIR}/${NAME}
#数据库root密码,可修改
PASSWORD=123456


install_mysql_wget(){
rpm -q wget &>/dev/null || yum install -y wget
yum install -y -q libaio perl-Data-Dumper autoconf
if [ -f ${FULL_NAME} ];then
    action "安装文件已经存在不需要下载"
else  
    action "安装文件不存在开始下载" false
    wget $URL || { action "下载失败,异常退出" false;exit 10; }
fi
}


install_mysql_user(){
if id mysql &>/dev/null;then
    action "MYSQL用户已经存在,无需从新创建!"
else  
    useradd -r -s /sbin/nologin  mysql
    action "mysql用户创建成功"
fi
}


install_mysql_datadir(){ 
for i in `seq 3306 $NUM`;do
    if [ -d ${DATA_DIR}/$i ];then
        action "${DATA_DIR}/$i目录已经存在,无需从新创建!"
        chown mysql: $DATA_DIR/$i
    else
        mkdir -p ${DATA_DIR}/$i
        action "${DATA_DIR}/$i目录创建完毕!"
        chown mysql: $DATA_DIR/$i
    fi
done
}

install_mysql_my(){
cat <<EOF >/etc/my.cnf
[mysqld_multi]
mysqld=/usr/local/mysql/bin/mysqld_safe
mysqladmin=/usr/local/mysql/bin/mysqladmin
EOF
for i in `seq 3306 $NUM`;do
cat <<EOF >>/etc/my.cnf
[mysqld$i]
datadir=${DATA_DIR}/$i
port=$i
socket=${DATA_DIR}/$i/mysql${i}.sock
pid-file=${DATA_DIR}/$i/mysql${i}.pid
log-error=${DATA_DIR}/$i/mysql${i}.log
EOF
done
action "配置文件配置完成"
}

install_mysql_enable(){
cat <<EOF >>/etc/rc.d/rc.local
. /etc/profile.d/mysql.sh
mysqld_multi start 3306-$NUM 
EOF
chmod +x /etc/rc.d/rc.local
action "mysql开机启动设置完毕!"
}

install_mysql_multi(){
if [ -e /usr/local/mysql ];then
    action "mysql已经安装,无法安装" false
    exit
else
    tar -xf $FULL_NAME -C /usr/local
    cd /usr/local/
    ln -s $DIRNAME mysql
    chown -R mysql: /usr/local/mysql/
    echo "PATH=/usr/local/mysql/bin":'$PATH' >/etc/profile.d/mysql.sh
    source /etc/profile.d/mysql.sh

    for i in `seq 3306 $NUM`;do
        mysqld --initialize-insecure  --user=mysql --datadir=${DATA_DIR}/$i &>/dev/null
        action "$i数据库原始数据文件生成完毕"
    done

    install_mysql_my

    mysqld_multi start 3306-$NUM
    action "正在启动...等待20秒";sleep 20
    for i in `seq 3306 $NUM`;do
    mysqladmin -S ${DATA_DIR}/$i/mysql${i}.sock ping >/dev/null
        if [ $? = 0 ];then
            action "$i数据库启动成功"
            mysqladmin -S ${DATA_DIR}/$i/mysql${i}.sock password $PASSWORD &>/dev/null
            if [ $? = 0 ];then
                action "$i数据库root密码修改成功"
            else
                action "$i数据库root密码修改失败" false
            fi
        else
            action "$i数据库启动失败" false
        fi
    done
fi
}

install_mysql_wget
install_mysql_user
install_mysql_datadir
install_mysql_multi
install_mysql_enable

使用方法:

  • 适用于mysql5.7及以上版本,以下版本切勿使用
  • 适用于centos7,8系统centos6没有测试过
  • 任何方式安装过mysql主机请勿使用