在文章上一篇文章中
https://blog.51cto.com/8355320/2466817
我使用的是手动使用mysql_multi方式安装mysql5.7.28多实例,安装过程比较耗时,这里我编写了shell脚本来自动化安装,请大家参考,脚本经测试没有出现问题,脚本我有二次修改里面的实例名与变量名,可能存在不一致,请自行对比确认下。脚本中有什么可以优化的还请多多指点,谢谢!!以下是脚本中的几点说明:
1、mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz包上传路径: /opt
如果没有此软件包,请将此脚本中如下代码行去掉 # 号注释,自动联网下载(下载过程很慢,建议提前下载并上传到/opt路径下)

wget -c https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz

2、mysql数据库脚本上传路径: /opt 包含如下文件(需要修改sql文件中的实例名)

testone.sql
testtwo.sql

4、根据服务器磁盘分配情况,创建对应软链接,例如:

mkdir /home/data
ln -s /home/data/ /data

5、修改(确保局域网内值唯一)

mysql3306.cnf 的 server_id = 3306
mysql3307.cnf 的 server_id = 3307

6、注意修改预设的root密码与apps应用连接密码

Multi_PWD="Multi.db"

dbone_appspwd="testone3306"
dbtwo_appspwd="testtwo3307"

7、修改testone.sql数据库实例 testone 用户名 testone 及对应的密码 testone3306

create database testone default character set utf8 collate utf8_bin;
grant select,insert,update,delete,create,execute on testone.* to 'testone'@'%' identified by 'testone3306';

8、修改testone.sql导入数据库时的数据库实例名 testone

$MYSQL_CMD_DIR/mysql -uroot -p"${Multi_PWD}" -S ${DbOneSockfile} --comments testone < testone.sql

9、修改testtwo.sql数据库实例 testtwo 用户名 testtwo 及对应的密码 testtwo3307

create database testtwo default character set utf8 collate utf8_bin;
grant select,insert,update,delete,create,execute on testtwo.* to 'testtwo'@'%' identified by 'testtwo3307';

10、修改testtwo.sql导入数据库时的数据库实例名 testtwo

$MYSQL_CMD_DIR/mysql -uroot -p"${Multi_PWD}" -S ${DbTwoSockfile} --comments testtwo < testtwo.sql

 #!/bin/bash
################################################################################################
# Install software -- Install Mysql 5.7 on CentOS 7
# Author:Danrtsey
# mail:[email protected]
# History: 2020/01/15 Asa release
################################################################################################
[ -f /etc/init.d/functions ]&& . /etc/init.d/functions

###Check if user is root
if [ $UID -ne 0 ]; then
    echo "Error: This script must be executed as root."
    exit 1
fi

echo "################################################################################################"
echo "                      自动安装   MySQL 5.7      on Redhat/CentOS Linux                           "
echo "################################################################################################"

#set mysql root password
echo "#########################       预设 Mysql root用户密码           ###############################"
Multi_PWD="Multi.db"

#set mysql apps password
echo "#########################       预设 Mysql apps用户密码           ###############################"
dbone_appspwd="testone3306"
dbtwo_appspwd="testtwo3307"

echo "################################       定义目录路径      #######################################"

##define mysql directory configuration variable
DbOneDatadir=/data/mysql/dbone3306/data
DbOneBinlogdir=/data/mysql/dbone3306/binlog
DbOneLogdir=/data/mysql/dbone3306/logs
DbOneSockfile=/data/mysql/dbone3306/mysql3306.sock
DbOnepidfile=/data/mysql/dbone3306/mysqld3306.pid

DbTwoDatadir=/data/mysql/dbtwo3307/data
DbTwoBinlogdir=/data/mysql/dbtwo3307/binlog
DbTwoLogdir=/data/mysql/dbtwo3307/logs
DbTwoSockfile=/data/mysql/dbtwo3307/mysql3307.sock
DbTwopidfile=/data/mysql/dbtwo3307/mysqld3307.pid

BaseDir=/usr/local/mysql
MYSQL_CMD_DIR=$BaseDir/bin
Mycnf=/etc/my.cnf
Dbdir=/data
Softwaredir=/opt

mkdir -pv /data/mysql/{dbone3306,dbtwo3307}
mkdir -v /data/mysql/dbone3306/{logs,data,binlog}
mkdir -v /data/mysql/dbtwo3307/{logs,data,binlog}

echo "################################################################################################"
echo "                                        修改系统参数                                             "
echo "################################################################################################"

###set the ip in hosts
hostsset() {
echo "############################   Ip&Hosts Configuration  #######################################"
hostname=`hostname`
ip=`ip a|grep 'inet '|grep -v '127.0.0.1'|awk '{print $2}'|awk -F '/' '{print $1}'`
for i in ${ip}
do
    a=`grep "${i}" /etc/hosts`
    if [ ! -n "${a}" ];then
        echo "${i} ${hostname}" >> /etc/hosts 
    else
        break
    fi
done
}

ntp() {
yum -y install ntp >/dev/null 2>&1
systemctl enable ntpd
echo 'server ntp1.aliyun.com' >> /etc/ntp.conf
echo 'server ntp2.aliyun.com' >> /etc/ntp.conf
systemctl start ntpd
if [ $? != 0 ]; then
   errorExit 'ntp 启动未成功'
   exit 2
fi
  return 0
}

syspro() {
sed -i 's/SELINUX=enforcing/SELINUX=permissive/g' /etc/selinux/config
setenforce 0
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
echo 'LANG="en_US.UTF-8"' >> /etc/profile && source /etc/profile

cat >>/etc/security/limits.conf<> /etc/sysctl.conf</dev/null 2>&1
    yum -y remove mariadb* >/dev/null 2>&1
if [ $? -eq 0 ];then
   echo -e " \033[32m mariadb remove success!! \033[0m"
else
  echo -e "\e[31;47;5m mariadb remove failed!! \e[0m"
  exit 3  
fi

#Backup old my.cnf
if [ -s /etc/my.cnf ]; then
    mv /etc/my.cnf /etc/my.cnf.`date +%Y%m%d%H%M%S`.bak
fi
}

# 添加用户和组
addusers() {
grep mysql /etc/passwd
RETVAL=$?
if [ $RETVAL -ne 0 ];then
   groupadd mysql
   useradd mysql -g mysql -s /sbin/nologin -M

     action "mysql user added successfully" /bin/true
  else
      action " $(echo -e " mysql user already exists ")" /bin/true
      exit 4
fi

cat >> /etc/profile</dev/null 2>&1
if [ $? -eq 0 ];then
   echo -e " \033[32m yum install success!! \033[0m"
else
  echo -e "\e[31;47;5m yum install failed!! \e[0m"
  exit 5  
fi
cd $Softwaredir
#wget -c https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
tar xf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
if [ $? -eq 0 ];then
   echo -e " \033[32m tar.gz decompression success!! \033[0m"
else
  echo -e "\e[31;47;5m tar.gz decompression failed!! \e[0m"
  exit 6  
fi
mv mysql-5.7.28-linux-glibc2.12-x86_64 $BaseDir

chown -R mysql:mysql $BaseDir
chown -R mysql:mysql $Dbdir
}

#set my.cnf configfile
setmycnf() {
echo "################################    配置  my.cnf      #######################################"
cat >$Mycnf< $DbOneLogdir/error.log 2>&1
if [ $? -eq 0 ];then
   echo -e " \033[32m dbone initialize success!! \033[0m"
else
  echo -e "\e[31;47;5m dbone initialize failed!! \e[0m"
  exit 7  
fi
less $DbOneLogdir/error.log|grep 'A temporary password is generated for root@localhost:'
A=$?
count=0
while [ $count -lt 10 ]
do
    if [ $A -eq 0 ];then
        echo -e " \033[32m mysql initialize success!! \033[0m"
        break
    else
        echo -e "\e[31;47;5m the result is null,check again!! \e[0m"
        count=$[${count}+1]
    fi
done

#create ssl_rsa
echo "################################      dbone 生成ssl授权     #######################################"
$MYSQL_CMD_DIR/mysql_ssl_rsa_setup --user=mysql --basedir=$BaseDir --datadir=$DbOneDatadir
if [ $? -eq 0 ];then
   echo -e " \033[32m create ssl_rsa success!! \033[0m"
else
  echo -e "\e[31;47;5m create ssl_rsa failed!! \e[0m"
  exit 8
fi
}

#dbtwo initialize
dbtwoinitialize() {
echo "################################      dbtwo 初始化     #######################################"
$MYSQL_CMD_DIR/mysqld --defaults-file=$Mycnf --initialize --basedir=$BaseDir --user=mysql --datadir=$DbTwoDatadir > $DbTwoLogdir/error.log 2>&1
if [ $? -eq 0 ];then
   echo -e " \033[32m dbtwo initialize success!! \033[0m"
else
  echo -e "\e[31;47;5m dbtwo initialize failed!! \e[0m"
  exit 9
fi
less $DbTwoLogdir/error.log|grep 'A temporary password is generated for root@localhost:'
B=$?
count=0
while [ $count -lt 10 ]
do
    if [ $B -eq 0 ];then
        echo -e " \033[32m mysql initialize success!! \033[0m"
        break
    else
        echo -e "\e[31;47;5m the result is null,check again!! \e[0m"
        count=$[${count}+1]
    fi
done

#create ssl_rsa
echo "################################      dbtwo 生成ssl授权     #######################################"
$MYSQL_CMD_DIR/mysql_ssl_rsa_setup --user=mysql --basedir=$BaseDir --datadir=$DbTwoDatadir
if [ $? -eq 0 ];then
   echo -e " \033[32m create ssl_rsa success!! \033[0m"
else
  echo -e "\e[31;47;5m create ssl_rsa failed!! \e[0m"
  exit 10
fi
}

# start dbone service && change tmptroot pwd
dbonestart() {
echo "################################      dbone 服务启动     #######################################"
cat << EOF >> /etc/profile
export PATH=\$PATH:${BaseDir}/bin
EOF
source /etc/profile
chown -R mysql:mysql $DbOneLogdir
$MYSQL_CMD_DIR/mysqld_multi start 3306
if [ $? -eq 0 ];then
count=0
while [ $count -lt 120 ]
do
cat $DbOneLogdir/error.log |grep '/data/mysql/dbone3306/mysql3306.sock'
C=$?
    if [ $C -eq 0 ];then
        echo -e " \033[32m dbone start success!! \033[0m"
        break
    else
        count=$[${count}+1]
        echo -e "\e[31;47;5m 3306 is not exist check the $count time!! \e[0m"
        sleep 1
    fi
done
else
  echo -e "\e[31;47;5m dbone use mysqld_multi start failed,please check the error.log !! \e[0m"
  cat $DbOneLogdir/error.log |grep ERROR
  exit 11
fi

echo "################################   获取 dbone root用户临时密码     #######################################"
DbOnedbTempPass=$(less $DbOneLogdir/error.log|grep 'A temporary password is generated for root@localhost:' |awk  '{print $NF}')
echo $DbOnedbTempPass

echo "################################     自动修改 dbone root用户初始密码       ###############################"
$MYSQL_CMD_DIR/mysqladmin -uroot -p"$DbOnedbTempPass" -S $DbOneSockfile password $Multi_PWD
if [ $? -eq 0 ];then
   echo -e " \033[32m dbone rootpwd changed success!! \033[0m"
else
  echo -e "\e[31;47;5m dbone rootpwd changed failed!! \e[0m"
  exit 12
fi

echo "################################     验证 dbone root用户初始密码       ###############################"
MYSQL_CMD="$MYSQL_CMD_DIR/mysql -uroot -p"${Multi_PWD}" -S ${DbOneSockfile}"
$MYSQL_CMD -e "show databases;" | grep information_schema
if [ $? -eq 0 ];then
   echo -e " \033[32m dbone rootpwd is correct!! \033[0m"
else
  echo -e "\e[31;47;5m dbone rootpwd is  wrong!! \e[0m"
  exit 13
fi

echo "################################  修改 my.cnf multi 启动用户与密码  #######################################"
sed -i "s@^#user=root@user=root@g" /etc/my.cnf
sed -i "s@^#pass=@pass=${Multi_PWD}@g" /etc/my.cnf
if [ $? -eq 0 ];then
   echo -e " \033[32m my.cnf changed success!! \033[0m"
else
  echo -e "\e[31;47;5m my.cnf changed failed!! \e[0m"
  exit 14
fi

echo "################################   验证 dbone 服务关闭是否正常     #######################################"
$MYSQL_CMD_DIR/mysqld_multi stop 3306
netstat -tnlp|grep 3306
if [ $? -ne 0 ];then
   echo -e " \033[32m dbone stop success!! \033[0m"
else
  echo -e "\e[31;47;5m dbone stop failed!! \e[0m"
  exit 15
fi
}

# start dbtwo service && change tmptroot pwd
dbtwostart() {
echo "################################      dbtwo 服务启动     #######################################"
source /etc/profile
chown -R mysql:mysql $DbTwoLogdir
$MYSQL_CMD_DIR/mysqld_multi start 3307
if [ $? -eq 0 ];then
count=0
while [ $count -lt 120 ]
do
cat $DbTwoLogdir/error.log |grep '/data/mysql/dbtwo3307/mysql3307.sock'
D=$?
    if [ $D -eq 0 ];then
        echo -e " \033[32m dbtwo start success!! \033[0m"
        break
    else
        count=$[${count}+1]
        echo -e "\e[31;47;5m 3307 is not exist check the $count time!! \e[0m"
        sleep 1
    fi
done
else
  echo -e "\e[31;47;5m dbtwo use mysqld_multi start failed,please check the error.log  !! \e[0m"
  cat $DbTwoLogdir/error.log |grep ERROR
  exit 16
fi

echo "################################  获取 dbtwo root用户临时密码     #######################################"
DbTwodbTempPass=$(less $DbTwoLogdir/error.log|grep 'A temporary password is generated for root@localhost:' |awk  '{print $NF}')
echo $DbTwodbTempPass

echo "################################     自动修改 dbtwo root用户初始密码       ###############################"
$MYSQL_CMD_DIR/mysqladmin -uroot -p"$DbTwodbTempPass" -S $DbTwoSockfile password $Multi_PWD
if [ $? -eq 0 ];then
   echo -e " \033[32m dbtwo rootpwd changed success!! \033[0m"
else
  echo -e "\e[31;47;5m dbtwo rootpwd changed failed!! \e[0m"
  exit 17
fi

echo "################################     验证 dbtwo root用户初始密码       ###############################"
MYSQL_CMD="$MYSQL_CMD_DIR/mysql -uroot -p"${Multi_PWD}" -S ${DbTwoSockfile}"
$MYSQL_CMD -e "show databases;" | grep information_schema
if [ $? -eq 0 ];then
   echo -e " \033[32m dbtwo rootpwd is correct!! \033[0m"
else
  echo -e "\e[31;47;5m dbtwo rootpwd is  wrong!! \e[0m"
  exit 18
fi

echo "################################   验证 dbtwo 服务关闭是否正常     #######################################"
$MYSQL_CMD_DIR/mysqld_multi stop 3307
netstat -tnlp|grep 3307
if [ $? -ne 0 ];then
   echo -e " \033[32m dbtwo stop success!! \033[0m"
else
  echo -e "\e[31;47;5m dbtwo stop failed!! \e[0m"
  exit 19
fi
}

dboneimp() {
echo "###############################  再次重启 dbone 数据库确认系统服务正常  ##################################"
source /etc/profile
cat >$DbOneLogdir/error.log< /tmp/dbone_create.log
cat /tmp/dbone_create.log |grep "Table 'mysql.servers' doesn't exist"
if [ $? -eq 0 ]
 then
   echo -e "\e[31;47;5m 创建 testone 数据库失败!\e[0m"
   exit 21
fi
$MYSQL_CMD -e "show databases;" | grep testone
if [ $? -eq 0 ]
 then
   echo -e " \033[32m 创建 testone 数据库成功!! \033[0m"
else
   echo -e "\e[31;47;5m 创建 testone 数据库失败!\e[0m"
   exit 22
fi
echo "###############################           testone 导入数据            ##################################"
cd $Softwaredir
$MYSQL_CMD_DIR/mysql -uroot -p"${Multi_PWD}" -S ${DbOneSockfile} testone < testone.sql
if [ $? -eq 0 ]
 then
   echo -e " \033[32m 导入数据成功!! \033[0m"
else
   echo -e "\e[31;47;5m 导入数据失败!\e[0m"
   exit 23
fi
}

dbtwoimp() {
echo "###############################   再次重启 dbtwo 数据库确认系统服务正常   ##################################"
source /etc/profile
cat >$DbTwoLogdir/error.log< /tmp/dbtwo_create.log
cat /tmp/dbtwo_create.log |grep "Table 'mysql.servers' doesn't exist"
if [ $? -eq 0 ]
 then
   echo -e "\e[31;47;5m 创建 testtwo 数据库失败!\e[0m"
   exit 25
fi
$MYSQL_CMD -e "show databases;" | grep testtwo
if [ $? -eq 0 ]
 then
   echo -e " \033[32m 创建 testtwo 数据库成功!! \033[0m"
else
   echo -e "\e[31;47;5m 创建 testtwo 数据库失败!\e[0m"
   exit 26
fi
echo "###############################           testtwo 导入数据            ##################################"
cd $Softwaredir
$MYSQL_CMD_DIR/mysql -uroot -p"${Multi_PWD}" -S ${DbTwoSockfile} testtwo < testtwo.sql
if [ $? -eq 0 ]
 then
   echo -e " \033[32m 导入数据成功!! \033[0m"
else
   echo -e "\e[31;47;5m 导入数据失败!\e[0m"
   exit 27
fi
}

main() {

hostsset

ntp

syspro

fwport

clean

addusers

dbinstall

setmycnf

dboneinitialize

dbtwoinitialize

dbonestart

dbtwostart

dboneimp

dbtwoimp
}

main

echo "#######################        安装完成 (请记录dbone数据库testone信息)          ##############################"
echo "root密码:"
echo -e "\e[31;47;5m $Multi_PWD \e[0m"
echo "数据库实例连接密码:"
echo -e "\e[30;47;5m $dbone_appspwd \e[0m"

echo "#######################        安装完成 (请记录dbtwo数据库testtwo信息)          ##############################"
echo "root密码:"
echo -e "\e[31;47;5m $Multi_PWD \e[0m"
echo "数据库实例连接密码:"
echo -e "\e[30;47;5m $dbtwo_appspwd \e[0m"

source /etc/profile