centos7使用shell脚本安装mysql8.0

脚本路径下所有文件

在这里插入图片描述

  • install.cnf:脚本配置文件
  • my.cnf:数据库配置文件
  • mysql-8.0.19-1.el7.x86_64.rpm-bundle.tar:数据库安装包,下载方式:https://downloads.mysql.com/archives/community/(浏览器下载)、 wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.19-1.el7.x86_64.rpm-bundle.tar(本地下载)
  • mysql8.0_install.sh:mysql8.0安装脚本

脚本内容

#!/bin/bash
#导入配置文件
source /home/package/install.cnf

#检查md5值
function check_md5(){

if [ "$1" = "$2" ]
then
echo "-----------------------------------'$3'packag ok ----------------------------------------"
else
echo "-----------------------------------'$3' packag error pleas reinstall ----------------------------------------" >> $package_dir/ad_install.log
exit
fi
}

#mysql rpm安装包检查
function check_mysql_rpm(){
#检查是否存在mariadb的rpm,是则删除
ma=`rpm -qa |grep mariadb`
$mariadb_check=${ma:0:7}
echo $mariadb_check

for i in $ma
do
        echo "exist mariadb.rpm,will remove it"
        rpm -e $i --nodeps
done


#检查是否与mysql8的rpm存在
a=`rpm -qa | grep "$1"`
b=${a:16}
c=${b//./}
check_result=${c//-/}
echo $check_result
#如果模块存在则继续,如果模块不存在则安装,安装后若仍旧判断为不存在,则报错退出
if (($check_result==$2))
then
        echo "$1  exist,continue"
else
        rpm -ivh $3 --nodeps
        a=`rpm -qa | grep "$1"`
        b=${a:16}
        c=${b//./}
        check_result=${c//-/}
        if (($check_result==$2))
        then
                echo "$1  exist,continue"
        else
                echo "something  wrong with $1,exit" >> $package_dir/ad_install.log
        fi
fi
}

#安装mysql8.0
function mysql_install(){
echo "start install mysql8.0"
#判断安装包是否存在,不存在则报错
if [ ! -f $package_dir/mysql-8.0.19-1.el7.x86_64.rpm-bundle.tar ]
then
        echo "mysql-8.0.19-1.el7.x86_64.rpm-bundle.tar not exist,please check" >> $package_dir/ad_install.log
        echo "quit install"
        exit
fi

cd $package_dir
#检查mysql安装包的md5值
mysql_md5_now=`md5sum "$package_dir/mysql-8.0.19-1.el7.x86_64.rpm-bundle.tar" | awk '{print $1}'`
check_md5 $mysql_md5_now $mysql_md5_origin mysql-8.0.19-1.el7.x86_64.rpm-bundle.tar

#解压安装包
echo "unpack mysql package"
tar -xvf $package_dir/mysql-8.0.19-1.el7.x86_64.rpm-bundle.tar
mkdir $package_dir/mysql_rpm
mv mysql-community* $package_dir/mysql_rpm
#
cd $package_dir/mysql_rpm
#
##调用方法检查mysql的各个rpm模块是否存在,不存在则安装
echo "install mysql rpms"

check_mysql_rpm mysql-community-common-8.0.19-1.el7.x86_64 common80191el7x86_64 mysql-community-common-8.0.19-1.el7.x86_64.rpm
check_mysql_rpm mysql-community-libs-8.0.19-1.el7.x86_64 libs80191el7x86_64 mysql-community-libs-8.0.19-1.el7.x86_64.rpm
check_mysql_rpm mysql-community-libs-compat-8.0.19-1.el7.x86_64 libscompat80191el7x86_64 mysql-community-libs-compat-8.0.19-1.el7.x86_64.rpm
check_mysql_rpm mysql-community-client-8.0.19-1.el7.x86_64 client80191el7x86_64 mysql-community-client-8.0.19-1.el7.x86_64.rpm
check_mysql_rpm mysql-community-server-8.0.19-1.el7.x86_64 server80191el7x86_64 mysql-community-server-8.0.19-1.el7.x86_64.rpm


#修改配置文件,主要修改内容有端口、安装路径、错误日志路径等
mv /etc/my.cnf /etc/my.cnf.bak
cp $package_dir/my.cnf /etc/my.cnf
sed -ri 's#(port            =)[^ ]*#\port            ='$mysql_port'#' /etc/my.cnf
sed -ri 's#(log-error       =)[^ ]*#log-error       ='$mysql_errlog'#' /etc/my.cnf
sed -ri 's#(basedir         =)[^ ]*#\basedir         ='$mysql_basedir'#' /etc/my.cnf
sed -ri 's#(datadir         =)[^ ]*#\datadir         ='$mysql_datadir'#' /etc/my.cnf
sed -ri 's#(socket          =)[^ ]*#\socket          ='$mysql_basedir/mysql.sock'#' /etc/my.cnf
#

#创建mysql安装路径
if [ ! -d $mysql_basedir ]
then
        mkdir -p $mysql_basedir
else
        rm -rf $mysql_basedir/*
fi
chown -R mysql:mysql $mysql_basedir

#mysql指定参数初始化
echo "start initialize mysql"
/sbin/mysqld --user=mysql --lower-case-table-names=1 --initialize --basedir=$mysql_basedir --datadir=$mysql_datadir

chown -R mysql:mysql $mysql_basedir

#复制errmsg.sys文件到指定路径,防止报错
mkdir  -p $mysql_basedir/share/mysql-8.0
cp /usr/share/mysql-8.0/english/errmsg.sys $mysql_basedir/share/mysql-8.0/
chown -R mysql:mysql $mysql_basedir/share
chmod 744 $mysql_basedir/share/mysql-8.0/errmsg.sys

#启动mysql并设置开机自启动
echo "start mysql"
systemctl start mysqld
systemctl enable mysqld

#获取mysql初始密码
mysql_passwd_origin=`cat "$mysql_errlog" | grep password | head -1 | rev  | cut -d ' ' -f 1 | rev`
echo $mysql_passwd_origin

#使用初始密码连接mysql并更改密码,root用户授权
mysql --connect-expired-password  -uroot -p"$mysql_passwd_origin"  >tables.txt  <'root'@'localhost' identified  by '$mysql_passwd';
create user 'root'@'%' identified  by '$mysql_passwd';
grant all privileges on *.* to 'root'@'%' with grant option;
create user 'root'@'127.0.0.1' identified  by '$mysql_passwd';
grant all privileges on *.* to 'root'@'127.0.0.1' with grant option;
flush privileges;
quit
EOF
}


配置文件install.cnf内容

#mysql8.0_install.sh配置文件

#安装包所在的目录
package_dir=/home/package
#表示mysql的安装路径
mysql_basedir=/home/mysql8.0
#mysql数据路径
mysql_datadir=/home/mysql8.0/data
#mysql错误日志路径
mysql_errlog=/home/mysql8.0/data/mysqld.log
#表示mysql root用户的密码
mysql_passwd=123456
#表示mariadb10.0.38的md5值
mysql_md5_origin=7c908c1da4609eb45c039bd67d4545ee
#mysql的端口
mysql_port=3308

数据库配置文件my.cnf内容

[client]
socket          =/home/mysql/mysql.sock
port            =3306
[mysqld_safe]
[mysqld]
port            =3306
log-error       =/home/mysql/log/mysqld.log
basedir         =/home/mysql
datadir         =/home/mysql/data
socket          =/home/mysql/mysql.sock
slow_query_log = on
long_query_time = 1
min_examined_row_limit = 1
lower_case_table_names=1
innodb_buffer_pool_size = 6G
innodb_flush_log_at_trx_commit = 2
default_authentication_plugin=mysql_native_password

这是我安装mysql的固定配置模板。数据库的配置都是在这个基础上进行更改

安装方式

1、修改install.cnf中的各项配置
2、下载安装包
3、修改mysql8.0_install.sh中配置文件的路径和名称
4、执行脚本

你可能感兴趣的:(数据库,shell)