一鍵安裝mysql8.0相同版本多實(shí)例腳本

寫(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>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容