bash脚本安装MySQL-8.0.20数据库


一、基础环境检测创建文件夹与下载数据库安装包

if [ ! -d "/root/mysql" ];then
    Echo_Blue '[info] 创建文件夹:/root/mysql'
    mkdir /root/mysql
else
    Echo_Yellow "[info] 文件夹/root/mysql已经存在,继续执行..."
fi
cd /root/mysql
if [ ! -f "/root/mysql/mysql-8.0.20-1.el8.x86_64.rpm-bundle.tar" ];then
    Echo_Blue '[info] Mysql包不存在,正在下载'
    wget https://mirrors.huaweicloud.com/mysql/Downloads/MySQL-8.0/mysql-8.0.20-1.el8.x86_64.rpm-bundle.tar
else
    Echo_Blue "[info] Mysql包已存在,继续执行..."
fi

二、解压数据库安装包

tar -xvf mysql-8.0.20-1.el8.x86_64.rpm-bundle.tar

三、判断是否解压完成,并安装

if [ ! -f "mysql-community-common-8.0.20-1.el8.x86_64.rpm" ];then
    Echo_Red '[error] 解压失败,请检查'
    exit
else
    Echo_Green "[info] MySQL安装中,继续执行..."
    rpm -ivh mysql-community-common-8.0.20-1.el8.x86_64.rpm --nodeps --force
    rpm -ivh mysql-community-libs-8.0.20-1.el8.x86_64.rpm --nodeps --force
    rpm -ivh mysql-community-client-8.0.20-1.el8.x86_64.rpm --nodeps --force
    rpm -ivh mysql-community-server-8.0.20-1.el8.x86_64.rpm --nodeps --force
fi

四、修改my.cnf配置文件

if [ `grep -c "default_authentication_plugin=mysql_native_password" /etc/my.cnf` -eq '0' ]; then
    sed -i 's/\[mysqld\]/\[mysqld\]\ndefault_authentication_plugin=mysql_native_password/' /etc/my.cnf
    Echo_Blue "[info] 配置default_authentication_plugin=mysql_native_password"
else
    Echo_Yellow "[info] /etc/my.cnf已存在default_authentication_plugin=mysql_native_password"
fi
if [ `grep -c "sql_mode" /etc/my.cnf` -eq '0' ]; then
    sed -i 's/\[mysqld\]/\[mysqld\]\nsql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES/' /etc/my.cnf
    Echo_Green "[info] 配置sql_mode完成"
else
    Echo_Blue "[info] /etc/my.cnf已存在sql_mode"
fi

五、初始化配置并修改密码

mysqld --initialize
if [ `grep -c "mysql" /etc/group` -eq '0' ]; then
    Echo_Yellow "[info] 不存在mysql用户,正在添加"
    groupadd mysql
    useradd -s /sbin/nologin -M -g mysql mysql
else
    Echo_Yellow "[info] 授予mysql用户权限"
    chown mysql:mysql /var/lib/mysql -R
    if [ ! -f "/var/log/mysqld.log" ];then
        Echo_Red "[error] /var/log/mysqld.log文件不存在,请检查。"
    else
        Echo_Green "[info] 原始密码:"
        cat /var/log/mysqld.log|grep "A temporary password is generated for root@localhost:" |awk -F "host:" '{print $2}'
        Echo_Yellow "[info] 请先修改密码mysql_secure_installation"
    fi
    Echo_Green "[info] 安装完成。"
fi
mysql_secure_installation
systemctl start mysqld.service

六、执行脚本自动安装

wget https://hello.fe80.cn/bash/mysql8020el8.sh && bash mysql8020el8.sh