CentOS7.5源码shell脚本自动安装MySQL5.7.28多实例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS7.5源码shell脚本自动安装MySQL5.7.28多实例相关的知识,希望对你有一定的参考价值。

在文章上一篇文章中https://blog.51cto.com/8355320/2463218
我使用的是手动源码安装mysql5.7.28,安装过程还是比较耗时,我编写了shell自动安装脚本,请大家参考,脚本我测试没有出现问题,脚本我有二次修改里面的实例名与变量名,可能存在不一致,请自行对比确认下。脚本中有什么可以优化的还请多多指点,谢谢!!以下是脚本中的几点说明:
1、mysql5.7.28软件包(脚本中配置wget下载,如果下载比较慢,可以事先下载好,上传到 /opt 路径下,并注释对应行的wget下载代码,另外源码安装过程比较耗时,安装请耐心等待),软件包名称如下:
#boost_1_59_0.tar.gz
#mysql-boost-5.7.28.tar.gz

2、两个mysql数据库脚本上传路径: /opt 包含如下文件(注意sql脚本中的实例名),sql测试脚本名称如下:
#testone.sql
#testtwo.sql
实例dbtwo有添加lower_case_table_names = 1参数
实例dbone没有添加

4、根据服务器磁盘分配情况,创建对应软链接,脚本我配置的路径是/data,根据需要修改,例如:

#mkdir /home/{data,deploy}
#ln -s /home/data/ /data

5、修改(确保局域网内值唯一)
#mysql3306.cnf 的 server_id = 3306
#mysql3307.cnf 的 server_id = 3307

6、注意修改预设的root密码与apps应用连接密码,根据需要修改使用
#dbone_rootpwd="Dbone.3306"
#dbtwo_rootpwd="Dbtwo.3307"

#dbone_appspwd="dbone3306"
#dbtwo_appspwd="dbtwo3307"

7、修改dbone数据库实例 dbone 用户名 dbone 及对应的密码 dbone3306

8、修改dbone导入数据库时的数据库实例名 dbone

9、修改dbtwo数据库实例 dbtwo 用户名 dbtwo 及对应的密码 dbtwo3307

10、修改dbtwo导入数据库时的数据库实例名 dbtwo

#!/bin/bash
################################################################################################
# Install software -- Install Mysql 5.7.28 on CentOS 7
# 
# History: 2020/01/09 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.28      on CentOS7 Linux                           "
echo "################################################################################################"

#set mysql root password
echo "#########################       预设 Mysql root用户密码           ###############################"
dbone_rootpwd="dbone.3306"
dbtwo_rootpwd="dbtwo.3307"

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

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
dboneDefaultfile=/data/mysql/dbone3306/my3306.cnf

dbtwoDatadir=/data/mysql/dbtwo3307/data
dbtwoBinlogdir=/data/mysql/dbtwo3307/binlog
dbtwoLogdir=/data/mysql/dbtwo3307/logs
dbtwoSockfile=/data/mysql/dbtwo3307/dbtwo3307.sock
dbtwopidfile=/data/mysql/dbtwo3307/mysqld3307.pid
dbtwoDefaultfile=/data/mysql/dbtwo3307/my3307.cnf

BaseDir=/usr/local/mysql
MYSQL_DATADIR=/usr/local/mysql/data
UNIX_ADDR=/usr/local/mysql/tmp/mysql.sock
BOOSTDIR=/usr/local/boost
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
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<<EOF
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535
EOF

cat >> /etc/sysctl.conf<<EOF
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_fin_timeout = 10
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_keepalive_time = 120
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 3
vm.swappiness = 0
net.ipv4.ip_forward=1
net.bridge.bridge-nf-call-iptables=1
net.ipv4.neigh.default.gc_thresh1=4096
net.ipv4.neigh.default.gc_thresh2=6144
net.ipv4.neigh.default.gc_thresh3=8192
kernel.shmmax = 25769803774
kernel.shmmni = 4096
kernel.shmall = 16777216
kernel.sem = 1010 129280 1010 128
net.ipv4.ip_local_port_range = 9000 65500
net.core.rmem_default = 4194304
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048576
fs.aio-max-nr = 1048576
fs.file-max = 6815744
EOF
modprobe br_netfilter
sysctl -p
}

fwport() {
firewall-cmd --permanent --zone=public --add-port=3306/tcp
firewall-cmd --permanent --zone=public --add-port=3307/tcp
firewall-cmd --reload
}

clean() {
echo "################################    删除旧Mysql、Maria    #######################################"
    rpm -qa|grep mysql
    MariaDB=`rpm -qa|grep mariadb`
    yum -y remove mysql* --nodeps
    yum -y remove $MariaDB --nodeps

#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 3
fi

chown -R mysql:mysql $Dbdir

cat >> /etc/profile<<EOF
if [ $USER = "mysql" ]; then
if [ $SHELL = "/bin/ksh" ]; then
ulimit -p 16384
ulimit -n 65536
else
ulimit -u 16384 -n 65536
fi
fi
EOF
source /etc/profile
}

#install mysql-5.7.26
dbinstall() {
echo "################################       开始下载安装      #######################################"
yum -y install bzr zlib-devel gcc-c++ ncurses ncurses-devel libev make cmake gcc autoconf automake zlib libxml libgcrypt libtool bison perl perl-devel libaio libaio-devel perl-Time-HiRes perl-DBD-MySQL perl-Digest-MD5 rsync perl-Data-Dumper net-tools wget vim openssl openssl-devel
cd $Softwaredir
wget -c http://www.sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
tar xf boost_1_59_0.tar.gz
mv boost_1_59_0 $BOOSTDIR

wget -c https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.26.tar.gz
tar xf mysql-boost-5.7.26.tar.gz
cd mysql-5.7.26
cmake . -DCMAKE_INSTALL_PREFIX=$BaseDir -DMYSQL_DATADIR=$MYSQL_DATADIR -DMYSQL_UNIX_ADDR=$UNIX_ADDR -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_bin -DEXTRA_CHARSETS=all -DENABLED_LOCAL_INFILE=ON -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 -DWITHOUT_PARTITION_STORAGE_ENGINE=1 -DWITH_FAST_MUTEXES=1 -DWITH_SSL=system -DWITH_ZLIB=bundled -DWITH_EDITLINE=bundled -DWITH_BOOST=$BOOSTDIR -DENABLED_LOCAL_INFILE=1 -DWITH_READLINE=1 -DWITH_EMBEDDED_SERVER=1 -DWITH_DEBUG=0
if [ $? -eq 0 ];then
   make
   if [ $? -eq 0 ];then
      make install
      if [ $? -eq 0 ];then
        echo -e " 33[32m mysql install success 33[0m"
      else
        echo -e "e[31;47;5m make install failed!! please check the result!! e[0m"
        exit 6
      fi
   else
     echo -e "e[31;47;5m make failed!! please check the result!! e[0m"
     exit 5
   fi
else
  echo -e "e[31;47;5m cmake failed!! please check the result!! e[0m"
  exit 4  
fi

cat << EOF >> /etc/profile
export PATH=$PATH:${BaseDir}/bin
EOF
source /etc/profile

chown -R mysql:mysql $BaseDir
}

#edit my.cnf
setmycnf() {
cat >>$dboneDefaultfile<<EOF
[client]
port = 3306
socket = ${dboneSockfile}

[mysql]
prompt="u@jsshapp R:m:s [d]> "
no-auto-rehash

[mysqld]
user                                = mysql
port                                = 3306
symbolic-links                      = 0
basedir                             = ${BaseDir}
datadir                             = ${dboneDatadir}
socket                              = ${dboneSockfile}
pid-file                            = ${dbonepidfile}
server_id                           = 3306
character_set_server                = utf8
max_connections                     = 1000
skip_name_resolve                   = 1
open_files_limit                    = 65536
thread_cache_size                   = 64
table_open_cache                    = 4096
table_definition_cache              = 1024
table_open_cache_instances          = 64
max_prepared_stmt_count             = 1048576
explicit_defaults_for_timestamp     = true
log_timestamps                      = system

binlog_format                       = row
log_bin                           = ${dboneBinlogdir}/mysql-bin
binlog_rows_query_log_events        = on
expire_logs_days                    = 7
binlog_cache_size                   = 4M
max_binlog_cache_size               = 2G
max_binlog_size                     = 1G
sync_binlog                         = 1
log_bin_trust_function_creators     = 1

slow_query_log                      = on
slow_query_log_file                 = ${dboneDatadir}/slow.log
log-error                           = ${dboneLogdir}/error.log
log_queries_not_using_indexes      = on
long_query_time                     = 1.000000

gtid_mode                           = on
enforce_gtid_consistency            = on

default_storage_engine              = innodb                    
default_tmp_storage_engine          = innodb                    
innodb_data_file_path               = ibdata1:1024M:autoextend
innodb_temp_data_file_path          = ibtmp1:12M:autoextend
innodb_buffer_pool_filename         = ib_buffer_pool
innodb_log_files_in_group           = 3
innodb_log_file_size                = 512M
innodb_online_alter_log_max_size    = 1024M
innodb_open_files                   = 4096
innodb_page_size                    = 32k
innodb_thread_concurrency           = 0
innodb_read_io_threads              = 4
innodb_write_io_threads             = 4
innodb_purge_threads                = 4
innodb_page_cleaners                = 4
innodb_print_all_deadlocks          = on
innodb_lock_wait_timeout            = 20
innodb_spin_wait_delay              = 128
innodb_autoinc_lock_mode            = 2
innodb_io_capacity                  = 200
innodb_io_capacity_max              = 2000
#innodb_flush_neighbors             = 
innodb_log_buffer_size              = 8M
innodb_flush_log_at_timeout         = 1
innodb_flush_log_at_trx_commit      = 2
innodb_buffer_pool_size             = 1024M
innodb_buffer_pool_instances        = 4
autocommit                          = 1
innodb_buffer_pool_dump_pct         = 25
innodb_buffer_pool_dump_at_shutdown = ON
innodb_buffer_pool_load_at_startup  = ON

[mysqldump]
quick
max_allowed_packet = 32M
EOF

cat >>$dbtwoDefaultfile<<EOF
[client]
port = 3307
socket = ${dbtwoSockfile}

[mysql]
prompt="u@jsshapp R:m:s [d]> "
no-auto-rehash

[mysqld]
user                                = mysql
port                                = 3307
symbolic-links                      = 0
lower_case_table_names              = 1
basedir                             = ${BaseDir}
datadir                             = ${dbtwoDatadir}
socket                              = ${dbtwoSockfile}
pid-file                            = ${dbtwopidfile}
server_id                           = 3307
character_set_server                = utf8
max_connections                     = 1000
skip_name_resolve                   = 1
open_files_limit                    = 65536
thread_cache_size                   = 64
table_open_cache                    = 4096
table_definition_cache              = 1024
table_open_cache_instances          = 64
max_prepared_stmt_count             = 1048576
explicit_defaults_for_timestamp     = true
log_timestamps                      = system

binlog_format                       = row
log_bin                            = ${dbtwoBinlogdir}/mysql-bin
binlog_rows_query_log_events        = on
expire_logs_days                    = 7
binlog_cache_size                   = 4M
max_binlog_cache_size               = 2G
max_binlog_size                     = 1G
sync_binlog                         = 1

slow_query_log                      = on
slow_query_log_file                 = ${dbtwoDatadir}/slow.log
log-error                           = ${dbtwoLogdir}/error.log
log_queries_not_using_indexes      = on
long_query_time                     = 1.000000

gtid_mode                           = on
enforce_gtid_consistency            = on

default_storage_engine              = innodb
default_tmp_storage_engine          = innodb
innodb_data_file_path               = ibdata1:1024M:autoextend
innodb_temp_data_file_path          = ibtmp1:12M:autoextend
innodb_buffer_pool_filename         = ib_buffer_pool
innodb_log_files_in_group           = 3
innodb_log_file_size                = 512M
innodb_online_alter_log_max_size    = 1024M
innodb_open_files                   = 4096
innodb_page_size                    = 32k
innodb_thread_concurrency           = 0
innodb_read_io_threads              = 4
innodb_write_io_threads             = 4
innodb_purge_threads                = 4
innodb_page_cleaners                = 4
innodb_print_all_deadlocks          = on
innodb_lock_wait_timeout            = 20
innodb_spin_wait_delay              = 128
innodb_autoinc_lock_mode            = 2
innodb_io_capacity                  = 200
innodb_io_capacity_max              = 2000
#innodb_flush_neighbors             = 
innodb_log_buffer_size              = 8M
innodb_flush_log_at_timeout         = 1
innodb_flush_log_at_trx_commit      = 2
innodb_buffer_pool_size             = 1024M
innodb_buffer_pool_instances        = 4
autocommit                          = 1
innodb_buffer_pool_dump_pct         = 25
innodb_buffer_pool_dump_at_shutdown = ON
innodb_buffer_pool_load_at_startup  = ON

[mysqldump]
quick
max_allowed_packet = 32M
EOF
}

#dbone initialize
dboneinitialize() {
echo "################################      dbone 初始化     #######################################"
$BaseDir/bin/mysqld --defaults-file=$dboneDefaultfile --initialize --user=mysql --basedir=$BaseDir --datadir=$dboneDatadir
sleep 120
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 " 33[32m mysql initialize success!! 33[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授权     #######################################"
$BaseDir/bin/mysql_ssl_rsa_setup --user=mysql --basedir=$BaseDir --datadir=$dboneDatadir
if [ $? -eq 0 ];then
   echo -e " 33[32m create ssl_rsa success!! 33[0m"
else
  echo -e "e[31;47;5m create ssl_rsa failed!! e[0m"
  exit 7  
fi

echo "################################      dbone 服务启动     #######################################"
$BaseDir/bin/mysqld_safe --defaults-file=$dboneDefaultfile --user=mysql 2>&1 > /dev/null &
sleep 10
netstat -tnlp|grep 3306
if [ $? -eq 0 ];then
   echo -e " 33[32m dbone start success!! 33[0m"
else
  echo -e "e[31;47;5m dbone start failed!! e[0m"
  exit 8  
fi

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

echo "################################     自动修改 dbone root用户初始密码       ###############################"
$BaseDir/bin/mysqladmin -uroot -p"$dboneTempPass" -S $dboneSockfile password $dbone_rootpwd
if [ $? -eq 0 ];then
   echo -e " 33[32m dbone rootpwd changed success!! 33[0m"
else
  echo -e "e[31;47;5m dbone rootpwd changed failed!! e[0m"
  exit 9  
fi

echo "################################      dbone 服务关闭     #######################################"
$BaseDir/bin/mysqladmin -uroot -p$dbone_rootpwd -S $dboneSockfile shutdown
netstat -tnlp|grep 3306
if [ $? -ne 0 ];then
   echo -e " 33[32m dbone stop success!! 33[0m"
else
  echo -e "e[31;47;5m dbone stop failed!! e[0m"
  exit 10
fi
}

#dbtwo initialize
dbtwoinitialize() {
echo "################################      dbtwo 初始化     #######################################"
$BaseDir/bin/mysqld --defaults-file=$dbtwoDefaultfile --initialize --user=mysql --basedir=$BaseDir --datadir=$dbtwoDatadir
sleep 120
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 " 33[32m mysql initialize success!! 33[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授权     #######################################"
$BaseDir/bin/mysql_ssl_rsa_setup --user=mysql --basedir=$BaseDir --datadir=$dbtwoDatadir
if [ $? -eq 0 ];then
   echo -e " 33[32m create ssl_rsa success!! 33[0m"
else
  echo -e "e[31;47;5m create ssl_rsa failed!! e[0m"
  exit 11
fi

echo "################################      dbtwo 服务启动     #######################################"
$BaseDir/bin/mysqld_safe --defaults-file=$dbtwoDefaultfile --user=mysql 2>&1 > /dev/null &
sleep 10
netstat -tnlp|grep 3307
if [ $? -eq 0 ];then
   echo -e " 33[32m dbtwo start success!! 33[0m"
else
  echo -e "e[31;47;5m dbtwo start failed!! e[0m"
  exit 12  
fi

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

echo "################################     自动修改 dbtwo root用户初始密码       ###############################"
$BaseDir/bin/mysqladmin -uroot -p"$dbtwoTempPass" -S $dbtwoSockfile password $dbtwo_rootpwd
if [ $? -eq 0 ];then
   echo -e " 33[32m dbtwo rootpwd changed success!! 33[0m"
else
  echo -e "e[31;47;5m dbtwo rootpwd changed failed!! e[0m"
  exit 13
fi

echo "################################      dbtwo 服务关闭     #######################################"
$BaseDir/bin/mysqladmin -uroot -p$dbtwo_rootpwd -S $dbtwoSockfile shutdown
netstat -tnlp|grep 3307
if [ $? -ne 0 ];then
   echo -e " 33[32m dbtwo stop success!! 33[0m"
else
  echo -e "e[31;47;5m dbtwo stop failed!! e[0m"
  exit 14  
fi
}

#set dbonestart script
dbonestart() {
SOCK="${dboneSockfile}"
CNF="${dboneDefaultfile}"
MYSQL_USER="root"
MYSQL_PWD="${dbone_rootpwd}"
CmdPath="${BaseDir}/bin"
cat > /etc/init.d/dbone<<EOF
#!/bin/sh
# chkconfig: 2345 80 90 
# Simple dbone mysql init.d script conceived to work on Linux systems

SOCK="${SOCK}"
CNF="${CNF}"
MYSQL_USER="${MYSQL_USER}"
MYSQL_PWD="${MYSQL_PWD}"
CmdPath="${CmdPath}"

#startup function
function_start_mysql()
{
    printf "Starting MySQL...
"
    /bin/sh ${CmdPath}/mysqld_safe --defaults-file=${CNF} --user=mysql  2>&1 > /dev/null &
}

#stop function
function_stop_mysql()
{
    printf "Stoping MySQL...
"
    ${CmdPath}/mysqladmin -u ${MYSQL_USER} -p${MYSQL_PWD} -S ${SOCK} shutdown
}

#restart function
function_restart_mysql()
{
    printf "Restarting MySQL...
"
    function_stop_mysql
    sleep 2
    function_start_mysql
}

case $1 in
start)
    function_start_mysql
;;
stop)
    function_stop_mysql
;;
restart)
    function_restart_mysql
;;
*)
    printf "Usage: dbone {start|stop|restart}
"
esac
EOF

chmod +x /etc/init.d/dbone
chkconfig --add dbone
chkconfig --list dbone
service dbone start
netstat -tnlp|grep 3306
if [ $? -ne 0 ];then
   echo -e " 33[32m dbone start success!! 33[0m"
else
  echo -e "e[31;47;5m dbone start failed!! e[0m"
  exit 15  
fi
}

#set dbtwostart script
dbtwostart() {
SOCK="${dbtwoSockfile}"
CNF="${dbtwoDefaultfile}"
MYSQL_USER="root"
MYSQL_PWD="${dbtwo_rootpwd}"
CmdPath="${BaseDir}/bin"
cat > /etc/init.d/dbtwo<<EOF
#!/bin/sh
# chkconfig: 2345 81 92 
# Simple dbtwo mysql init.d script conceived to work on Linux systems

SOCK="${SOCK}"
CNF="${CNF}"
MYSQL_USER="${MYSQL_USER}"
MYSQL_PWD="${MYSQL_PWD}"
CmdPath="${CmdPath}"

#startup function
function_start_mysql()
{
    printf "Starting MySQL...
"
    /bin/sh ${CmdPath}/mysqld_safe --defaults-file=${CNF} --user=mysql 2>&1 > /dev/null &
}

#stop function
function_stop_mysql()
{
    printf "Stoping MySQL...
"
    ${CmdPath}/mysqladmin -u ${MYSQL_USER} -p${MYSQL_PWD} -S ${SOCK} shutdown
}

#restart function
function_restart_mysql()
{
    printf "Restarting MySQL...
"
    function_stop_mysql
    sleep 2
    function_start_mysql
}

case $1 in
start)
    function_start_mysql
;;
stop)
    function_stop_mysql
;;
restart)
    function_restart_mysql
;;
*)
    printf "Usage: dbtwo {start|stop|restart}
"
esac

EOF

chmod +x /etc/init.d/dbtwo
chkconfig --add dbtwo
chkconfig --list dbtwo
service dbtwo start
netstat -tnlp|grep 3307
if [ $? -ne 0 ];then
   echo -e " 33[32m dbtwo start success!! 33[0m"
else
  echo -e "e[31;47;5m dbtwo start failed!! e[0m"
  exit 16  
fi
}

dboneimp() {
echo "###############################  再次重启 dbone 数据库确认系统服务正常  ##################################"
service dbone stop
service dbone start
sleep 10
netstat -tnlp|grep 3306
if [ $? -ne 0 ];then
   echo -e " 33[32m dbone start success!! 33[0m"
else
  echo -e "e[31;47;5m dbone start failed!! e[0m"
  exit 17  
fi
echo "###############################        创建 dbone 库           ##################################"
MYSQL_CMD="$BaseDir/bin/mysql -uroot -p"${dbone_rootpwd}" -S ${dboneSockfile}"
$MYSQL_CMD -e "create database dbone default character set utf8 collate utf8_bin;grant select,insert,update,delete,create,execute on dbone.* to ‘dbone‘@‘%‘ identified by ‘dbone3306‘;flush privileges;"
$MYSQL_CMD -e "show databases;" | grep dbone
if [ $? -ne 0 ]
 then
   echo -e "e[31;47;5m 创建 dbone 数据库失败!e[0m"
   exit 18
fi
echo "###############################           dbone 导入数据            ##################################"
cd $Softwaredir
$BaseDir/bin/mysql -uroot -p"${dbone_rootpwd}" -S ${dboneSockfile} --comments dbone < testone.sql
if [ $? -ne 0 ]
 then
   echo -e "e[31;47;5m 导入数据失败!e[0m"
   exit 19
fi
}

dbtwoimp() {
echo "###############################   再次重启 dbtwo 数据库确认系统服务正常   ##################################"
service dbtwo stop
service dbtwo start
sleep 10
netstat -tnlp|grep 3307
if [ $? -ne 0 ];then
   echo -e " 33[32m dbtwo start success!! 33[0m"
else
  echo -e "e[31;47;5m dbtwo start failed!! e[0m"
  exit 20  
fi
echo "###############################        创建 dbtwo  库           ##################################"
MYSQL_CMD="$BaseDir/bin/mysql -uroot -p"${dbtwo_rootpwd}" -S ${dbtwoSockfile}"
$MYSQL_CMD -e "create database dbtwo  default character set utf8 collate utf8_bin;grant select,insert,update,delete,create,execute on dbtwo.* to ‘dbtwo‘@‘%‘ identified by ‘dbtwo3307‘;flush privileges;"
$MYSQL_CMD -e "show databases;" | grep dbtwo 
if [ $? -ne 0 ]
 then
   echo -e "e[31;47;5m 创建 dbtwo  数据库失败!e[0m"
   exit 21
fi
echo "###############################           dbtwo  导入数据            ##################################"
cd $Softwaredir
$BaseDir/bin/mysql -uroot -p"${dbtwo_rootpwd}" -S ${dbtwoSockfile} --comments dbtwo  < testtwo.sql
if [ $? -ne 0 ]
 then
   echo -e "e[31;47;5m 导入数据失败!e[0m"
   exit 22
fi
}

main() {

hostsset

ntp

syspro

fwport

clean

addusers

dbinstall

setmycnf

dboneinitialize

dbtwoinitialize

dbonestart

dbtwostart

dboneimp

dbtwoimp
}

main

echo "#######################        安装完成 (请记录dbone数据库dbone信息)          ##############################"
echo "root密码:"
echo $dbone_rootpwd
echo "数据库实例连接密码:"
echo $dbone_appspwd

echo "#######################        安装完成 (请记录dbtwo数据库dbtwo信息)          ##############################"
echo "root密码:"
echo $dbtwo_rootpwd
echo "数据库实例连接密码:"
echo $dbtwo_appspwd

以上是关于CentOS7.5源码shell脚本自动安装MySQL5.7.28多实例的主要内容,如果未能解决你的问题,请参考以下文章

shell自动化安装LAMP

Devops自动化更新Shell脚本实现

一个自动安装lnmp环境的shell脚本

Shell脚本自动编译打包Gradle项目并docker部署

第二十一章 SHELL脚本-CENTOS7.5知识

shell脚本ssh自动登陆服务器