#!/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_VERSION='5.7'
MYSQL_FILE='mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz'
MYSQL_DOWNLOAD_URL='https://mirrors.huaweicloud.com/mysql/Downloads/MySQL'
MYSQL_ROOT_PASSWORD='magedu'
# 加载函数
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}
rpm -q wget &> /dev/null || dnf -y install wget &> /dev/null
if [ ! -e ${MYSQL_FILE} ] ;then
color "${SCRIPT_NAME}: 开始下载 'MySQL' 二进制安装包." 0
if wget ${MYSQL_DOWNLOAD_URL}-${MYSQL_VERSION}/${MYSQL_FILE} &>/dev/null ;then
color "${SCRIPT_NAME}: ${MYSQL_FILE} 文件下载成功." 0
else
color "${SCRIPT_NAME}: ${MYSQL_FILE} 文件下载失败" 1
exit
fi
else
color "${SCRIPT_NAME}: ${MYSQL_FILE} 文件已存在." 3
return
fi
}
# 数据库存在性检查
check_mysql(){
if [ -e /usr/local/mysql ];then
color "${SCRIPT_NAME}: MySQL 数据库已存在,安装失败." 1
exit
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
fi
# 编写service文件
cat > /lib/systemd/system/mysqld.service <<-EOF
[Unit]
Description=mysql database server
After=network.target
[Service]
Type=notify
PrivateNetwork=false
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
SuccessExitStatus=5 6
ExecStart=/etc/init.d/mysqld start
ExecStop=/etc/init.d//mysqld stop
ExecReload=/etc/init.d/mysqld reload
[Install]
WantedBy=multi-user.target
Alias=mysqld.service
EOF
# 加载mysqld.service配置,使其可以被systemd管理
systemctl daemon-reload # 重新加载systemd管理器配置
systemctl enable --now mysqld &> /dev/null
[ $? -ne 0 ] && { color "${SCRIPT_NAME}: 数据库启动失败,退出." 1 ; exit ; }
# 获取数据库旧密码
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
标签:38,NAME,5.7,color,MYSQL,mysql,SCRIPT,dev,MySQL
From: https://www.cnblogs.com/cherish-mi/p/18263098