美文网首页
一键安装mysql8.0相同版本多实例脚本

一键安装mysql8.0相同版本多实例脚本

作者: 念念OPS | 来源:发表于2021-01-11 06:23 被阅读0次

    写了循环,创建文件夹和配置文件。
    注意这里的初始化是mysqld --defaults-file=/mysql/`$i`/etc/my.cnf --initialize-insecure --user=mysql --datadir=/mysql/${i}/data/ --basedir=/usr/local/mysql
    初始化时候需要加上--defaults-file,否则走默认配置文件路径如下

    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
    

    多实例就很麻烦指定配置文件路径。

    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 #已经验证可以自动化
    #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 "当前用户不是root,安装失败" false
        exit 1
    fi
    
    cd $SRC_DIR
    if [ -e /usr/local/mysql ];then
      action "数据库已存在,安装失败" 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"开始安装MySQL数据库..."$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}数据库..."
        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 启动${i}数据库...\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 "启动成功" || action "失败" false
    done
    }
    
    #执行函数
    check
    install_mysql
    init_mysql
    start_mysql
    

    验证

    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>
    

    相关文章

      网友评论

          本文标题:一键安装mysql8.0相同版本多实例脚本

          本文链接:https://www.haomeiwen.com/subject/wasraktx.html