#!/bin/bash
#
#********************************************************************
#Author: Kevin
#Date: 2024-06-23
#FileName: install_mysql.sh
#Description: The test script
#Copyright (C): 2024 All rights reserved
#********************************************************************
##################### 事先需上传软件包 ###############################
SCRIPT_NAME=$(basename $0)
COLOR="echo -e \e[1;32m"
END="\e[0m"
SRC_DIR='/usr/local/src'
SYSTEM_FILE="/etc/*-release"
MYSQL_FILE='mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz'
MYSQL_ROOT_PASSWORD='bufushu'
# 加载函数
source /etc/profile.d/color.sh
# 检查用户
check_user(){
if [ ! $UID -eq 0 ];then
{ color "${SCRIPT_NAME}: 当前用户不是root,安装失败" 1 ; exit ; }
fi
}
# 检查操作系统
check_os(){
if grep -Ewqi "Rocky|CentOS" /etc/issue || grep -Ewqi "Rocky|CentOS" /etc/*-release ;then
if ! rpm -q redhat-lsb-core &> /dev/null ;then
dnf -y install redhat-lsb-core &> /dev/null
color "${SCRIPT_NAME}: lsb_release 工具安装完成." 0
fi
OS_ID=$(lsb_release -si)
OS_VERSION=$(lsb_release -sr | sed -nr 's/(^.*)\..*$/\1/p')
fi
}
# 二进制文件存在性检查
check_file(){
cd ${SRC_DIR}
if [ ! -e ${MYSQL_FILE} ];then
$COLOR"缺少 ${MYSQL_FILE} 文件."$END
$COLOR"请将相关软件放在 ${SRC_DIR} 目录下."$END
exit
elif [ -e /usr/local/mysql ];then
color "${SCRIPT_NAME}: MySQL 数据库已存在,安装失败." 1
exit
else
return
fi
}
# 开始安装mysql数据库
install_mysql(){
cd ${SRC_DIR}
# 安装依赖包
${COLOR}''开始下载 \'MySQL\' 依赖包,请稍等...''${END}
dnf -y install libaio perl-Data-Dumper ncurses-compat-libs &>/dev/null
# 解包
${COLOR}''开始安装 \'MySQL\' 数据库,请稍等...''${END}
tar xf ${MYSQL_FILE} -C /usr/local
# 创建软链接
MYSQL_DIR=$(echo ${MYSQL_FILE}|sed -rn 's/(^.*[0-9]+).*/\1/p')
ln -s ${MYSQL_DIR} ../mysql
# 创建系统用户和组
if ! id -u mysql &> /dev/null ; then
groupadd -g 3306 mysql
useradd -r -g 3306 -s /sbin/nologin mysql
color "mysql 用户创建成功." 0
fi
chown -R mysql.mysql /usr/local/mysql
# 添加PATH变量
echo "PATH=/usr/local/mysql/bin/:$PATH" > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
# 编写配置文件
cat > /etc/my.cnf <<-EOF
[mysqld]
server-id=1
log-bin
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
EOF
# 创建数据目录,设置目录权限
[ -d /data/mysql ] || mkdir -p /data/mysql &> /dev/null
chown -R mysql.mysql /data/mysql
# 初始化MySQl数据库 生成服务脚本
mysqld --initialize --user=mysql --datadir=/data/mysql
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
# 添加服务列表
if [ ${OS_ID} == "Rocky" -o ${OS_ID} == "CentOS" ];then
chkconfig --add mysqld
chkconfig mysqld on
service mysqld start &>/dev/null
fi
# 获取数据库旧密码
MYSQL_OLDPASSWORD=`grep "password" /data/mysql/mysql.log | sed -nr 's/^.* (.*)$/\1/p'`
# 设置数据库新密码
mysqladmin -uroot -p${MYSQL_OLDPASSWORD} password ${MYSQL_ROOT_PASSWORD} &>/dev/null
color "${SCRIPT_NAME}: MySQL数据库安装完成. " 0
}
main(){
check_user
check_os
check_file
check_mysql
install_mysql
}
main
标签:8.0,mysql,28,dev,etc,MYSQL,MySQL,null,check
From: https://www.cnblogs.com/cherish-mi/p/18263830