数据库服务之 Mysql8.x 服务脚本安装
Mysql介绍
MySQL是一个关系型数据库管理系统,由瑞典 MySQL AB 公司开发,属于 Oracle 旗下产品。MySQL是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQL是最好的RDBMS (Relational Database Management System,关系数据库管理系统)应用软件之一。
MySQL是一种关系型数据库管理系统,关系数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。
MySQL所使用的 SQL 语言是用于访问数据库的最常用标准化语言。MySQL 软件采用了双授权政策,分为社区版和商业版,由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,一般中小型和大型网站的开发都选择 MySQL作为网站数据库。
另外 MySQL8.0的生命周期[Apr 2026],彻底绷不住了。直接安装MySQL8.4 LTS版本。
脚本内容:
#!/bin/bash
#
#********************************************************************
#Author: wwtou
#Date: 2025-04-18
#FileName: Mysql_install_8x.sh
#Description: The test script for OS Version greater than 8.0
#Copyright (C): 2020 All rights reserved
#********************************************************************
# Defined Variable
MYSQL_VER="8.4.5"
MYSQL_FILE="mysql-${MYSQL_VER}-linux-glibc2.28-x86_64.tar.xz"
MYSQL_URL="https://cdn.mysql.com/archives/mysql-8.4/"
SRC_DIR=/tools
MYSQL_BASE_DIR="/app"
MYSQL_INSTALL_DIR="${MYSQL_BASE_DIR}/mysql"
MYSQL_DATA_DIR="/data/3306"
MYSQL_BINLOG_DIR="/data/binlog"
MYSQL_LOG_DIR="/data/log"
MYSQL_CONFILE="/etc/my.cnf"
MYSQL_COMMAND_FILE="/etc/profile.d/mysqld.sh"
USER="mysql"
GROUP="mysql"
MYSQL_PORT=3306
SERVERID=$(hostname -I | awk -F'.' '{print $4}' | cut -d' ' -f1)
. /etc/os-release
color() {
local text="$1"
local status="$2"
local RES_COL=60
local GREEN="\033[1;32m"
local RED="\033[1;31m"
local YELLOW="\033[1;33m"
local NORMAL="\033[0m"
# 左侧文本 + 对齐
printf "%-${RES_COL}s" "$text"
# 移动光标到第 RES_COL 列
echo -en "\033[${RES_COL}G"
echo -n "["
case "$status" in
success|0)
echo -en "${GREEN} OK ${NORMAL}"
;;
failure|1)
echo -en "${RED}FAILED${NORMAL}"
;;
*)
echo -en "${YELLOW}WARNING${NORMAL}"
;;
esac
echo "]"
}
check () {
[ -e ${MYSQL_INSTALL_DIR} ] && { color "MYSQL SERVER 已安装,请卸载后再安装" 1; exit; }
[ ! -d ${MYSQL_BASE_DIR} ] && mkdir -p ${MYSQL_BASE_DIR}
[ ! -d ${MYSQL_DATA_DIR} ] && mkdir -p ${MYSQL_DATA_DIR}
[ ! -d ${MYSQL_BINLOG_DIR} ] && mkdir -p ${MYSQL_BINLOG_DIR}
[ ! -d ${MYSQL_LOG_DIR} ] && mkdir -p ${MYSQL_LOG_DIR}
color "检查操作系统和系统版本" 0
# Detect OS
# $os_version variables aren't always in use, but are kept here for convenience
if grep -qs "ubuntu" /etc/os-release; then
os="ubuntu"
os_version=$(grep 'VERSION_ID' /etc/os-release | cut -d '"' -f 2 | tr -d '.')
elif [[ -e /etc/debian_version ]]; then
os="debian"
os_version=$(grep -oE '[0-9]+' /etc/debian_version | head -1)
elif egrep -i "openeuler|rocky|almalinux|Anolis|centos|kylin" /etc/os-release &> /dev/null; then
os="centos"
os_version=$(grep "VERSION_ID" /etc/os-release | awk -F'"' '{print $2}' | cut -d'.' -f1)
elif [[ -e /etc/fedora-release ]]; then
os="fedora"
os_version=$(grep -oE '[0-9]+' /etc/fedora-release | head -1)
else
color "This installer seems to be running on an unsupported distribution.Supported distros are Ubuntu, Debian, AlmaLinux, Rocky Linux, CentOS and Fedora." 1
exit
fi
if [[ "$os" == "ubuntu" && "$os_version" -lt 2204 ]]; then
color "Ubuntu 22.04 or higher is required to use this installer.This version of Ubuntu is too old and unsupported." 1
exit
fi
if [[ "$os" == "debian" ]]; then
if grep -q '/sid' /etc/debian_version; then
color "Debian Testing and Debian Unstable are unsupported by this installer." 1
exit
fi
if [[ "$os_version" -lt 11 ]]; then
color "Debian 11 or higher is required to use this installer.This version of Debian is too old and unsupported." 1
exit
fi
fi
if [[ "$os" == "centos" && "$os_version" -lt 9 ]]; then
os_name=$(grep "^NAME" /etc/os-release | awk -F'"' '{print $2}')
echo "$os_name 9 or higher is required to use this installer.This version of $os_name is too old and unsupported." 0
exit
fi
color "安装工具包,检查防火墙" 0
if ! hash ufw 2>/dev/null && ! hash firewall-cmd 2>/dev/null && ! hash iptables 2>/dev/null; then
if [[ "$os" == "ubuntu" && "$os" == "debinn" ]]; then
apt -y update && apt install -y ufw
[ $? -ne 0 ] && { color "Install UFW Failed" 1 ; exit; }
ufw disable
color 'ufw is closed' 0
else
dnf install -y firewalld
[ $? -ne 0 ] && { color "Install Firewalld Failed" 1; exit; }
setenforce 0
sed -i '/^SELINUX=/c SELINUX=disabled' /etc/selinux/config
color 'Selinux has been disabled, and Permanent After Restarting' 0
systemctl disable --now firewalld.service &>/dev/null
color 'firewalld is complete closed' 0
fi
fi
if [[ "$os" == "ubuntu" && "$os" == "debinn" ]]; then
apt -y update && apt install -y tar wget vim bash-completion gcc make openssl libssl-dev openssl-dev libaio-dev numactl
[ $? -ne 0 ] && { color "Install Tools Failed" 1 ; exit; }
else
dnf install -y wget tar vim-enhanced bash-completion gcc make openssl-devel libaio-devel numactl
[ $? -ne 0 ] && { color "Install Tools Failed" 1; exit; }
fi
[ ! -d ${SRC_DIR} ] && mkdir ${SRC_DIR}
cd ${SRC_DIR}
if [ -e ${SRC_DIR}/${MYSQL_FILE} ];then
color "相关文件已准备好" 0
else
color '开始下载 MYSQL 源码包' 0
wget ${MYSQL_URL}${MYSQL_FILE}
[ $? -ne 0 ] && { color "下载 ${MYSQL_FILE}文件失败" 1; exit; }
fi
}
install () {
if id $USER &> /dev/null;then
color "$USER 用户已存在" 1
else
groupadd $GROUP
useradd -r -g $GROUP -s /sbin/nologin $USER
color "创建 $USER 用户" 0
fi
chown -R $USER:$GROUP ${MYSQL_DATA_DIR} ${MYSQL_LOG_DIR} ${MYSQL_BINLOG_DIR}
echo
color "开始安装 MYSQL ===>>>"
cd $SRC_DIR
tar -xvf ${MYSQL_FILE} -C ${MYSQL_BASE_DIR}
MYSQL_DIR=$(echo ${MYSQL_FILE} | sed -nr 's/^(.*[0-9]).*/\1/p')
#cd ${MYSQL_BASE_DIR}
#ln -s $(echo ${MYSQL_FILE} | sed -nr 's/^(.*[0-9]).*/\1/p') ${MYSQL_INSTALL_DIR}
ln -s ${MYSQL_BASE_DIR}/${MYSQL_DIR} ${MYSQL_INSTALL_DIR}
# initialized databases
${MYSQL_INSTALL_DIR}/bin/mysqld --initialize-insecure --lower_case_table_names=1 --user=$USER --basedir=${MYSQL_INSTALL_DIR} --datadir=${MYSQL_DATA_DIR}
[ $? -eq 0 ] && color "MYSQL 初始化安装成功" 0 || { color "MYSQL 安装失败,退出!" 1 ;exit; }
# chown BaseDir and DataDir
chown -R $USER:$GROUP ${MYSQL_DATA_DIR} ${MYSQL_LOG_DIR} ${MYSQL_BINLOG_DIR}
chown -R $USER:root ${MYSQL_BASE_DIR}/${MYSQL_DIR}*
# configuration environment variable
echo "export PATH=${MYSQL_INSTALL_DIR}/bin:\$PATH" > ${MYSQL_COMMAND_FILE}
cat > ${MYSQL_CONFILE} <<EOF
[mysql]
socket = /tmp/mysql.sock
default-character-set=utf8mb4
[mysqld]
user = $USER
basedir = ${MYSQL_INSTALL_DIR}
datadir = ${MYSQL_DATA_DIR}
server_id = $SERVERID
port = ${MYSQL_PORT}
socket = /tmp/mysql.sock
# 忽略大小写,加在在5.6/5.7低版本的mysql配置文件中有效. 8版本只能在初始化时指定,然后必须配置服务才能启动
lower_case_table_names = 1
innodb_open_files = 1000
#skip-log-bin = 1
log-bin = ${MYSQL_BINLOG_DIR}/mysql-bin
log-error = ${MYSQL_LOG_DIR}/error.log
binlog_expire_logs_seconds = 604800
max_binlog_size = 100M
character-set-server = utf8mb4
innodb_file_per_table = 1
skip-name-resolve = 1
innodb_strict_mode = 0
#skip-grant-tables = 1
max_allowed_packet = 1024M
max_connections = 1000
EOF
if ! grep "* soft nofile 65535" /etc/security/limits.conf &>/dev/null; then
cat <<-EOF >> /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
EOF
fi
cat > /usr/lib/systemd/system/mysqld.service <<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Service]
User=$USER
Group=$GROUP
ExecStart=${MYSQL_INSTALL_DIR}/bin/mysqld --defaults-file=${MYSQL_CONFILE}
LimitNOFILE = 5000
[Install]
WantedBy=multi-user.target
EOF
# start mysql serivce
systemctl daemon-reload && systemctl enable --now mysqld.service
sleep 10s
systemctl is-active mysqld &> /dev/null && color "MYSQL 服务启动成功!" 0 || { color "MYSQL 服务启动失败,退出!" 1 ; exit; }
color "MYSQL 安装完成" 0
echo
echo "请先运行: source ${MYSQL_COMMAND_FILE}"
echo
}
check
installl
执行脚本
bash mysql_install_v8.sh
通过执行脚本实现一键安装 ~~
转载请注明作者和出处,并添加本页链接。
原文链接:
//www.wwtou.com/vGSSNZyy.html