MySQL离线安装自动化部署脚本

1、 版本 5.7.27

2、安装包

  • mysql-5.7.27-1.el7.x86_64.rpm-bundle.tar

3、 部署脚本

  • 在mysql安装包同级目录,新建mysql_install.sh,写入以下内容
#!/bin/bash
INSTALL_PACKAGE_PATH="./mysql-5.7.27-1.el7.x86_64.rpm-bundle.tar"
 
function init_environment(){
    rpm -e --nodeps mariadb-libs
    echo "uninstall about mariadb"
}
 
function change_password(){
    findpass=$(grep 'temporary password' /var/log/mysqld.log)
    initpass=${findpass##* }
    echo $initpass
    NEWPASS="Root@123456"
    echo $NEWPASS
    SQL="ALTER USER 'root'@'localhost' IDENTIFIED BY \"${NEWPASS}\";GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY \"${NEWPASS}\" WITH GRANT OPTION;flush privileges;"
    echo $SQL
    mysql --connect-expired-password --password="$initpass" -e "${SQL}"
    if [ $? -eq 0 ]
    then
        echo "change password success"
    else
        echo "change password fail"
        exit
    fi
} 
 
function install(){
    yum -y install libaio numactl libaio-devel autoconf net-tools perl
	init_environment
    echo "Start the installation"
    tar -xvf $INSTALL_PACKAGE_PATH
    RPM_EXE=`ls |grep mysql-community-common*`
    rpm -ivh $RPM_EXE
    RPM_EXE=`ls |grep mysql-community-libs-8*`
    rpm -ivh $RPM_EXE
    RPM_EXE=`ls |grep mysql-community-client*`
    rpm -ivh $RPM_EXE
    RPM_EXE=`ls |grep mysql-community-server*`
    rpm -ivh $RPM_EXE
    groupadd mysql
    useradd -g mysql mysql
    mysqld --initialize --user=mysql
    mysqld --initialize-insecure --user=mysql
    systemctl start mysqld
    change_password
}
 
install
  • 运行安装脚本, sh mysql_install.sh