centos7二进制安装mysql5.7/8.0脚本

#!/bin/bash
#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://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.7/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz"
#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
#创建用户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/

echo 'PATH=/usr/local/mysql/bin/:$PATH' >> /etc/profile.d/path.sh
. /etc/profile.d/path.sh
mkdir -p /mysql/3306/{data,binlog,log,etc,trans_log,pid,sock}
chown -R mysql.mysql /mysql/

cat > /etc/my.cnf < /mysql/3306/etc/my.cnf </dev/null
#mysqld --initialize --user=mysql --datadir=/data/mysql --basedir=/usr/local/mysql

cat > /lib/systemd/system/mysqld.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/3306/etc/my.cnf
    LimitNOFILE=6000
    EOF
systemctl enable mysqld
systemctl start mysqld
ss -lnt|( grep -o 3306 && action "启动成功" || action "启动失败" false )


#MYSQL_OLDPASSWORD=$(awk '/A temporary password/{print $NF}' /data/mysql/mysql.log)
#mysqladmin -uroot -p${MYSQL_OLDPASSWORD} password ${MYSQL_ROOT_PASSWORD} &>/dev/null
action "数据库安装完成"

}

#执行函数
check
install_mysql

你可能感兴趣的:(centos7二进制安装mysql5.7/8.0脚本)