寫(xiě)了循環(huán),創(chuàng)建文件夾和配置文件。
注意這里的初始化是mysqld --defaults-file=/mysql/`$i`/etc/my.cnf --initialize-insecure --user=mysql --datadir=/mysql/${i}/data/ --basedir=/usr/local/mysql
初始化時(shí)候需要加上--defaults-file,否則走默認(rèn)配置文件路徑如下
root@17 ~]# mysqld --verbose --help|grep 'my.cnf'
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf
my.cnf, $MYSQL_TCP_PORT, /etc/services, built-in default
多實(shí)例就很麻煩指定配置文件路徑。

image-20210111061330711
#MySQL5.7.29 Download URL: https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz #已經(jīng)驗(yàn)證可以自動(dòng)化
#MySQL5.7.31 Download URL: https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.7/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz" #
#MySQL8.0 Download URL: https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz
. /etc/init.d/functions
SRC_DIR=`pwd`
LINK="https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz"
#MYSQL='mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz'
MYSQL='mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz'
#MYSQL="mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz"
FULL_NAME=${SRC_DIR}/${MYSQL}
COLOR='echo -e \e[01;31m'
END='\e[0m'
MYSQL_ROOT_PASSWORD=123456
action "下載依賴..."
yum -y install autoconf libaio-devel numactl-libs libtinfo* net-tools vim wget gcc gcc-c++ lrzsz &>/dev/null
check (){
if [ $UID -ne 0 ];then
action "當(dāng)前用戶不是root,安裝失敗" false
exit 1
fi
cd $SRC_DIR
if [ -e /usr/local/mysql ];then
action "數(shù)據(jù)庫(kù)已存在,安裝失敗" false
exit 1
elif [ ! -e ${FULL_NAME} ];then
$COLOR"缺少${MYSQL}文件"$END
$COLOR"正在下載${MYSQL}文件..."$END
wget $LINK && action "下載${MYSQL}文件完成" || action "下載${MYSQL}文件失敗" false
else
return
fi
}
install_mysql(){
$COLOR"開(kāi)始安裝MySQL數(shù)據(jù)庫(kù)..."$END
cd $SRC_DIR
tar xf $MYSQL -C /usr/local/
MYSQL_DIR=`echo $MYSQL| sed -nr 's/^(.*[0-9]).*/\1/p'`
ln -s /usr/local/$MYSQL_DIR /usr/local/mysql
id mysql &>/dev/null && { action "mysql already exists";usermod -u306 -md /data/mysql -s /sbin/nologin mysql &>/dev/null; } || { useradd -r -u306 -d /data/mysql -s /sbin/nologin mysql &>/dev/mull; }
chown -R mysql.mysql /usr/local/mysql/
cat > /etc/profile.d/path.sh <<EOF
#mysql
export PATH=/usr/local/mysql/bin:\$PATH
EOF
}
source /etc/profile.d/path.sh
init_mysql() {
unset i
unset j
j=0
for i in {3306..3308};do
action "初始化${i}數(shù)據(jù)庫(kù)..."
let j=++j
mkdir -p /mysql/${i}/{data,etc,log,pid,sock}
cat > /mysql/${i}/etc/my.cnf <<-EOF
[mysql]
socket=/mysql/${i}/sock/mysql.sock
prompt="(\u@\h \p) [\d]>\_"
[mysqld]
user=mysql
skip_name_resolve=1
datadir=/mysql/${i}/data
basedir=/usr/local/mysql
server_id=$j
log-bin=/mysql/${i}/log/mysql-bin
log-error=/mysql/${i}/log/mysql.log
pid-file=/mysql/${i}/pid/mysql.pid
socket=/mysql/${i}/sock/mysql.sock
port=${i}
socket=/mysql/${i}/sock/mysql.sock
EOF
chown mysql.mysql -R /mysql/*
mysqld --defaults-file=/mysql/${i}/etc/my.cnf --initialize-insecure --user=mysql --datadir=/mysql/${i}/data/ --basedir=/usr/local/mysql
done
}
start_mysql () {
for i in {3306..3308};do
echo -e "\E[1;31m 啟動(dòng)${i}數(shù)據(jù)庫(kù)...\E[0m"
cat > /lib/systemd/system/mysqld${i}.service <<-EOF
[Unit]
Description=mysql database server
After=network.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/mysql/${i}/etc/my.cnf
LimitNOFILE=5000
EOF
systemctl enable mysqld${i}
systemctl restart mysqld${i}
ss -lnt|grep -q "$i" && action "啟動(dòng)成功" || action "失敗" false
done
}
#執(zhí)行函數(shù)
check
install_mysql
init_mysql
start_mysql
驗(yàn)證
root@17 ~]# ss -lnt|grep '330*'
LISTEN 0 70 [::]:33060 [::]:*
LISTEN 0 128 [::]:3306 [::]:*
LISTEN 0 128 [::]:3307 [::]:*
LISTEN 0 128 [::]:3308 [::]:*
root@17 ~]# mysql -S /mysql/3306/sock/mysql.sock
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.19 MySQL Community Server - GPL
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>