MySQL二进制包安装

74 阅读25分钟

2.3 二进制包安装

2.3.1 MySQL 8.0

2.3.1.1 用户和组

# Rocky、Almalinux、CentOS、openEuler、AnolisOS、OpenCloudOS、 Kylin Server、UOS Server、Ubuntu、Debian
useradd -s /sbin/nologin -r mysql

# openSUSE
groupadd -r mysql
useradd -s /sbin/nologin -r -g mysql mysql

2.3.1.2 准备程序文件

MySQL二进制包下载,去“www.mysql.com/”网站下载,选择“DO…

t74

图74 下载MySQL二进制包

选择“MySQL Community (GPL) Downloads”,如图75所示。

t75

图75 下载MySQL二进制包

然后选择“MySQL Community Server”,如图76所示。

t76

图76 下载MySQL二进制包

然后版本选择“8.0.43”,操作系统选择“Linux-Generic”,下面的有5个版本,分别是“glibc 2.28 (x86, 32-bit、x86, 64-bit和ARM, 64-bit)、glibc 2.17 (x86, 32-bit和x86, 64-bit)”,这个根据系统的glibc版本和主机的硬件架构类型下载,图77所示。

t77

图77 下载MySQL二进制包

# Rocky、Almalinux、CentOS、openEuler、AnolisOS、OpenCloudOS、 Kylin Server、UOS Server
[root@rocky10 ~]# yum info glibc
Last metadata expiration check: 0:08:38 ago on Tue 10 Jun 2025 12:21:32 PM CST.
Installed Packages
Name         : glibc
Version      : 2.39 # 可以看到glibc版本是2.39
Release      : 37.el10
Architecture : x86_64
Size         : 6.2 M
Source       : glibc-2.39-37.el10.src.rpm
Repository   : @System
From repo    : anaconda
Summary      : The GNU libc libraries
URL          : http://www.gnu.org/software/glibc/
License      : LGPL-2.1-or-later AND SunPro AND LGPL-2.1-or-later WITH GCC-exception-2.0 AND BSD-3-Clause AND
             : GPL-2.0-or-later AND LGPL-2.1-or-later WITH GNU-compiler-exception AND GPL-2.0-only AND ISC AND
             : LicenseRef-Fedora-Public-Domain AND HPND AND CMU-Mach AND LGPL-2.1-only AND LGPL-2.0-or-later AND
             : Unicode-DFS-2015 AND GFDL-1.1-or-later AND GPL-1.0-or-later AND FSFUL AND MIT AND Inner-Net-2.0
             : AND X11 AND GPL-2.0-or-later WITH GCC-exception-2.0 AND GFDL-1.3-only AND GFDL-1.1-only
Description  : The glibc package contains standard libraries which are used by
             : multiple programs on the system. In order to save disk space and
             : memory, as well as to make upgrading easier, common system code is
             : kept in one place and shared between programs. This particular package
             : contains the most important sets of shared libraries: the standard C
             : library and the standard math library. Without these two libraries, a
             : Linux system will not function.

# openSUSE
opensuse15:~ # zypper info glibc
Loading repository data...
Reading installed packages...


Information for package glibc:
------------------------------
Repository     : mirror-sle-update
Name           : glibc
Version        : 2.38-150600.14.26.1 # 可以看到glibc版本是2.38
Arch           : x86_64
Vendor         : SUSE LLC <https://www.suse.com/>
Installed Size : 6.5 MiB
Installed      : Yes
Status         : out-of-date (version 2.38-150600.12.1 installed)
Source package : glibc-2.38-150600.14.26.1.src
Upstream URL   : https://www.gnu.org/software/libc/libc.html
Summary        : Standard Shared Libraries (from the GNU C Library)
Description    : 
    The GNU C Library provides the most important standard libraries used
    by nearly all programs: the standard C library, the standard math
    library, and the POSIX thread library. A system is not functional
    without these libraries.

# Ubuntu和Debian
root@ubuntu2404:~# ldd --version
ldd (Ubuntu GLIBC 2.39-0ubuntu8.4) 2.39 # 可以看到glibc版本是2.39
Copyright (C) 2024 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Written by Roland McGrath and Ulrich Drepper.

显示当前主机的硬件架构类型:

[root@rocky10 ~]# arch
x86_64

根据操作系统的glibc版本去下载相应的安装包,这里系统版本选择“Linux - Generic (glibc 2.28) (x86, 64-bit)”,然后选择下面的“mysql-8.0.43-linux-glibc2.28-x86_64.tar.xz”包后面的“Download”进行下载,**注意:**glibc版本大于等于2.28的使用glibc 2.28版的MySQL二进制包,glibc版本小于2.28的使用glibc2.17版的MySQL二进制包,如图78所示。

t78

图78 下载MySQL二进制包

# Rocky、Almalinux、CentOS、AnolisOS、OpenCloudOS、Kylin Server默认没有wget包,需要安装
yum install -y wget

# openEuler 22.03/24.03 LTS、AnolisOS 23、OpenCloudOS 9没有安装tar包,需要安装
yum install -y tar

# Rocky 8/9/10、Almalinux 8/9/10、CentOS Stream 8/9/10、openEuler 22.03/24.03 LTS、AnolisOS 8/23、OpenCloudOS 8/9、Kylin Server v10/v11、UOS Server v20、openSUSE 15、Ubuntu Server 18.04/20.04/22.04/24.04 LTS、Debian 11/12/13:
wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.43-linux-glibc2.28-x86_64.tar.xz
tar xf mysql-8.0.43-linux-glibc2.28-x86_64.tar.xz -C /usr/local
ln -s /usr/local/mysql-8.0.43-linux-glibc2.28-x86_64/ /usr/local/mysql

# CentOS 7
wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.43-linux-glibc2.17-x86_64.tar.xz
tar xf mysql-8.0.43-linux-glibc2.17-x86_64.tar.xz -C /usr/local
ln -s /usr/local/mysql-8.0.43-linux-glibc2.17-x86_64/ /usr/local/mysql

chown -R  mysql:mysql /usr/local/mysql/

2.3.1.3 准备环境变量

echo 'PATH=/usr/local/mysql/bin/:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh

2.3.2.4 准备配置文件

cat > /etc/my.cnf <<-EOF
[mysqld]
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

2.3.2.5 生成数据库文件,并提取root密码

mkdir -p /data/mysql

chown -R mysql:mysql /data/mysql

# /data/mysql  会自动生成,但是/data/必须事先存在
# --initialize会生成一个初始化密码,--initialize-insecure生成空密码,两种方式任选一种
mysqld --initialize --user=mysql --datadir=/data/mysql

grep password /data/mysql/mysql.log
2025-04-22T14:36:39.157585Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: K39lgjhHlu:U
# "K39lgjhHlu:U"就是初始化密码

# 生成空密码的root用户
mysqld --initialize-insecure --user=mysql --datadir=/data/mysql

Rocky 8/9/10、Almalinux 8/9/10、CentOS Stream 8/9/10、CentOS 7、openEuler 22.03/24.03 LTS、AnolisOS 8/23、OpenCloudOS 8/9:

mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory # 提示确少”libaio.so.1“文件

yum install -y libaio

mysqld --initialize-insecure --user=mysql --datadir=/data/mysql

Ubuntu Server 24.04 LTS:

mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

root@ubuntu2404:~# apt search libaio
Sorting... Done
Full Text Search... Done
libaio-dev/noble-updates 0.3.113-6build1.1 amd64
  Linux kernel AIO access library - development files

libaio1t64/noble-updates,now 0.3.113-6build1.1 amd64 [installed,automatic] # 可以看到libaio1t64包已经安装过了
  Linux kernel AIO access library - shared library

# 创建软链接
ln -s /usr/lib/x86_64-linux-gnu/libaio.so.1t64.0.2 /usr/lib/x86_64-linux-gnu/libaio.so.1

mysqld --initialize-insecure --user=mysql --datadir=/data/mysql

Ubuntu Server 18.04 LTS:

mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

root@ubuntu1804:~# apt search libaio
Sorting... Done
Full Text Search... Done
libaio-dev/bionic-updates 0.3.110-5ubuntu0.1 amd64
  Linux kernel AIO access library - development files

libaio1/bionic-updates 0.3.110-5ubuntu0.1 amd64
  Linux kernel AIO access library - shared library

apt install -y libaio1

mysqld --initialize-insecure --user=mysql --datadir=/data/mysq

Debian 11/12:

root@debian12:~# mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

root@debian12:~# apt search libaio
Sorting... Done
Full Text Search... Done
libaio-dev/stable 0.3.113-4 amd64
  Linux kernel AIO access library - development files

libaio1/stable 0.3.113-4 amd64
  Linux kernel AIO access library - shared library

apt install -y libaio1

root@debian12:~# mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory

root@debian12:~# apt search libnuma
Sorting... Done
Full Text Search... Done
libnuma-dev/stable 2.0.16-1 amd64
  Development files for libnuma

libnuma1/stable 2.0.16-1 amd64
  Libraries for controlling NUMA policy

apt install -y libnuma1

mysqld --initialize-insecure --user=mysql --datadir=/data/mysql

Debian 13:

root@debian13:~# mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

root@debian13:~# apt search libaio
libaio-dev/stable 0.3.113-8+b1 amd64
  Linux kernel AIO access library - development files

libaio1t64/stable 0.3.113-8+b1 amd64
  Linux kernel AIO access library - shared library

apt install -y libaio1t64

# 创建软链接
root@debian13:~# ln -s /usr/lib/x86_64-linux-gnu/libaio.so.1t64.0.2 /usr/lib/x86_64-linux-gnu/libaio.so.1

root@debian13:~# mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory

root@debian13:~# apt search libnuma
libnuma-dev/stable 2.0.19-1 amd64
  Development files for libnuma

libnuma1/stable 2.0.19-1 amd64
  Libraries for controlling NUMA policy

apt install -y libnuma1

mysqld --initialize-insecure --user=mysql --datadir=/data/mysql

2.3.2.6 准备服务脚本和启动

Rocky、Almalinux、CentOS、openEuler、AnolisOS、OpenCloudOS、 Kylin Server、UOS Server、Ubuntu、Debian:

# Rocky 9/10、Almalinux 9/10、CentOS Stream 9/10默认没有chkconfig包,需要安装
yum install -y chkconfig

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

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

systemctl daemon-reload && systemctl enable --now mysqld

openSUSE:

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

# openSUSE
cat > /usr/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

# openSUSE
ln -s /sbin/chkconfig  /usr/lib/systemd/systemd-sysv-install
chkconfig --add mysqld

service mysqld start

2.3.2.7 修改口令

# 如果使用的是空密码,就不需要修改口令
awk '/temporary password/{print $NF}' /data/mysql/mysql.log
aupYu;uhJ9&y

mysqladmin -uroot -p'aupYu;uhJ9&y' password 123456

2.3.2.8 测试登录

Rocky 8/9/10、Almalinux 8/9/10、CentOS Stream 8/9/10、CentOS 7、openEuler 22.03/24.03 LTS、AnolisOS 8/23、OpenCloudOS 8/9、Kylin Server v10/v11、UOS Server v20、Ubuntu Server 18.04/20.04/22.04/24.04 LTS、Debian 11、openSUSE:

[root@rocky10 ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.43 MySQL Community Server - GPL

Copyright (c) 2000, 2025, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> status
--------------
mysql  Ver 8.0.43 for Linux on x86_64 (MySQL Community Server - GPL)

Connection id:		8
Current database:	
Current user:		root@localhost
SSL:			Not in use
Current pager:		stdout
Using outfile:		''
Using delimiter:	;
Server version:		8.0.43 MySQL Community Server - GPL
Protocol version:	10
Connection:		Localhost via UNIX socket
Server characterset:	utf8mb4
Db     characterset:	utf8mb4
Client characterset:	utf8mb4
Conn.  characterset:	utf8mb4
UNIX socket:		/data/mysql/mysql.sock
Binary data as:		Hexadecimal
Uptime:			44 sec

Threads: 2  Questions: 5  Slow queries: 0  Opens: 120  Flush tables: 3  Open tables: 36  Queries per second avg: 0.113
--------------

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

mysql> exit
Bye

Debian 12/13:

root@debian13:~# mysql
mysql: error while loading shared libraries: libncurses.so.6: cannot open shared object file: No such file or directory

root@debian13:~# apt search libncurses6
libncurses6/stable 6.5+20250216-2 amd64
  shared libraries for terminal handling

apt install -y libncurses6

root@debian13:~# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.43 MySQL Community Server - GPL

Copyright (c) 2000, 2025, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> status
--------------
mysql  Ver 8.0.43 for Linux on x86_64 (MySQL Community Server - GPL)

Connection id:		8
Current database:	
Current user:		root@localhost
SSL:			Not in use
Current pager:		stdout
Using outfile:		''
Using delimiter:	;
Server version:		8.0.43 MySQL Community Server - GPL
Protocol version:	10
Connection:		Localhost via UNIX socket
Server characterset:	utf8mb4
Db     characterset:	utf8mb4
Client characterset:	utf8mb4
Conn.  characterset:	utf8mb4
UNIX socket:		/data/mysql/mysql.sock
Binary data as:		Hexadecimal
Uptime:			10 sec

Threads: 2  Questions: 5  Slow queries: 0  Opens: 120  Flush tables: 3  Open tables: 36  Queries per second avg: 0.500
--------------

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

mysql> exit
Bye

2.3.2.9 一键安装MySQL 8.0二进制包的脚本

Shell脚本源码地址:

Gitee:gitee.com/raymond9/sh…

Github:github.com/raymond9999…

可以去上面的Gitee或Github代码仓库拉取脚本。

[root@rocky10 ~]# cat install_mysql_8.0_binary_v3.sh
#!/bin/bash
#
#**********************************************************************************
#Author:        Raymond
#QQ:            88563128
#MP:            Raymond运维
#Date:          2025-10-03
#FileName:      install_mysql_8.0_binary_v3.sh
#URL:           https://wx.zsxq.com/group/15555885545422
#Description:   The mysql binary script install supports 
#               “Rocky Linux 8, 9 and 10, AlmaLinux 8, 9 and 10, CentOS 7, 
#               CentOS Stream 8, 9 and 10, openEuler 22.03 and 24.03 LTS, 
#               AnolisOS 8 and 23, OpenCloudOS 8 and 9, Kylin Server v10 and v11, 
#               UOS Server v20, Ubuntu Server 18.04, 20.04, 22.04 and 24.04 LTS,  
#               Debian 11 , 12 and 13, openSUSE Leap 15“ operating systems.
#Copyright (C): 2025 All rights reserved
#**********************************************************************************
COLOR="echo -e \\033[01;31m"
END='\033[0m'

os(){
    . /etc/os-release
    MAIN_NAME=`sed -rn '/^NAME=/s@.*="([[:alpha:]]+).*"$@\1@p' /etc/os-release`
    if [ ${MAIN_NAME} == "Kylin" ];then
        MAIN_VERSION_ID=`sed -rn '/^VERSION_ID=/s@.*="([[:alpha:]]+)(.*)"$@\2@p' /etc/os-release`
    else
        MAIN_VERSION_ID=`sed -rn '/^VERSION_ID=/s@.*="?([0-9]+)\.?.*"?@\1@p' /etc/os-release`
    fi
}

os
DATA_DIR=/data/mysql

# mysql 8.0.43 glibc2.28包下载地址:"https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.43-linux-glibc2.28-x86_64.tar.xz"
# mysql 8.0.43 glibc2.17包下载地址:"https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.43-linux-glibc2.17-x86_64.tar.xz"

MYSQL_VERSION=8.0.43
if [ ${MAIN_NAME} == "CentOS" -a ${MAIN_VERSION_ID} == 7 ];then
    GLIBC_VERSION=2.17
else
    GLIBC_VERSION=2.28
fi
MYSQL_URL=https://cdn.mysql.com//Downloads/MySQL-8.0/
MYSQL_FILE="mysql-${MYSQL_VERSION}-linux-glibc${GLIBC_VERSION}-x86_64.tar.xz"

check_file(){
    if [ ${MAIN_NAME} == "Rocky" -o ${MAIN_NAME} == "AlmaLinux" -o ${MAIN_NAME} == "CentOS" -o ${MAIN_NAME} == "Anolis" -o ${MAIN_NAME} == "OpenCloudOS" -o ${MAIN_NAME} == "Kylin" ];then
        rpm -q wget &> /dev/null || { ${COLOR}"安装wget工具,请稍等......"${END};yum install -y wget &> /dev/null; }
    fi
    if [ ! -e ${MYSQL_FILE} ];then
        ${COLOR}"缺少${MYSQL_FILE}文件"${END}
        ${COLOR}'开始下载MySQL二进制安装包,请稍等......'${END}
        wget ${MYSQL_URL}${MYSQL_FILE} || { ${COLOR}"MySQL二进制安装包下载失败。"${END}; exit; }
    else
        ${COLOR}"${MYSQL_FILE}文件已准备好。"${END}
    fi
}

install_mysql(){
    [ -d /usr/local/mysql ] && { ${COLOR}"MySQL数据库已存在,安装失败!"${END};exit; }
    ${COLOR}"开始安装MySQL数据库,请稍等......"${END}
    if [ ${MAIN_NAME} == "openSUSE" ];then
        id mysql &> /dev/null || { groupadd -r mysql && useradd -s /sbin/nologin -r -g mysql mysql; ${COLOR}"成功创建mysql用户!"${END}; }
    else
        id mysql &> /dev/null || { useradd -s /sbin/nologin -r mysql ; ${COLOR}"成功创建mysql用户!"${END}; }
    fi
    if [ ${MAIN_NAME} == "openEuler" ];then
        if [ ${MAIN_VERSION_ID} == 22 -o ${MAIN_VERSION_ID} == 24 ];then
            yum install -y tar &> /dev/null
        fi
    fi
    if [ ${MAIN_NAME} == "Anolis" ];then
        if [ ${MAIN_VERSION_ID} == 23 ];then
            yum install -y tar &> /dev/null
        fi
    fi
    if [ ${MAIN_NAME} == "OpenCloudOS" ];then
        if [ ${MAIN_VERSION_ID} == 9 ];then
            yum install -y tar &> /dev/null
        fi
    fi
    tar xf ${MYSQL_FILE} -C /usr/local/
    MYSQL_DIR=`echo ${MYSQL_FILE}| sed -nr 's/^(.*[0-9]).*/\1/p'`
    ln -s /usr/local/${MYSQL_DIR} /usr/local/mysql
    chown -R mysql:mysql /usr/local/mysql/
    echo 'PATH=/usr/local/mysql/bin/:$PATH' > /etc/profile.d/mysql.sh
    . /etc/profile.d/mysql.sh
    cat > /etc/my.cnf <<-EOF
[mysqld]
datadir=${DATA_DIR}
socket=${DATA_DIR}/mysql.sock
log-error=${DATA_DIR}/mysql.log
pid-file=${DATA_DIR}/mysql.pid

[client]
socket=${DATA_DIR}/mysql.sock
EOF
    [ -d ${DATA_DIR} ] || mkdir -p ${DATA_DIR} &> /dev/null
    chown -R mysql:mysql ${DATA_DIR}
    if [ ${MAIN_NAME} == "Rocky" -o ${MAIN_NAME} == "AlmaLinux" -o ${MAIN_NAME} == "CentOS" -o ${MAIN_NAME} == "openEuler" -o ${MAIN_NAME} == "Anolis" -o ${MAIN_NAME} == "OpenCloudOS" ];then
        yum install -y libaio &> /dev/null
    fi
    if [ ${MAIN_NAME} == "Ubuntu" ];then
        if [ ${MAIN_VERSION_ID} == 24 ];then
            ln -s /usr/lib/x86_64-linux-gnu/libaio.so.1t64.0.2 /usr/lib/x86_64-linux-gnu/libaio.so.1
        fi
        if [ ${MAIN_VERSION_ID} == 18 ];then
            apt update &> /dev/null;apt install -y libaio1 &> /dev/null
        fi
    fi
    if [ ${MAIN_NAME} == "Debian" ];then
        if [ ${MAIN_VERSION_ID} == 11 -o ${MAIN_VERSION_ID} == 12 ];then
            apt update &> /dev/null;apt install -y libaio1 libnuma1 &> /dev/null
        fi
        if [ ${MAIN_VERSION_ID} == 13 ];then
            apt update &> /dev/null;apt install -y libaio1t64 libnuma1 &> /dev/null
            ln -s /usr/lib/x86_64-linux-gnu/libaio.so.1t64.0.2 /usr/lib/x86_64-linux-gnu/libaio.so.1
        fi
        if [ ${MAIN_VERSION_ID} == 12 -o ${MAIN_VERSION_ID} == 13 ];then
            apt update &> /dev/null;apt install -y libncurses6 &> /dev/null
        fi
    fi
    mysqld --initialize-insecure --user=mysql --datadir=${DATA_DIR}
    if [ ${MAIN_NAME} == "Rocky" ];then
        if [ ${MAIN_VERSION_ID} == 9 -o ${MAIN_VERSION_ID} == 10 ];then
            yum install -y chkconfig &> /dev/null
        fi
    fi
    if [ ${MAIN_NAME} == "AlmaLinux" ];then
        if [ ${MAIN_VERSION_ID} == 9 -o ${MAIN_VERSION_ID} == 10 ];then
            yum install -y chkconfig &> /dev/null
        fi
    fi
    if [ ${MAIN_NAME} == "CentOS" ];then
        if [ ${MAIN_VERSION_ID} == 9 -o ${MAIN_VERSION_ID} == 10 ];then
            yum install -y chkconfig &> /dev/null
        fi
    fi
    cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
    if [ ${MAIN_NAME} == "openSUSE" ];then
       cat > /usr/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
        ln -s /sbin/chkconfig  /usr/lib/systemd/systemd-sysv-install
		chkconfig --add mysqld  &> /dev/null
        service mysqld start
    else
        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
        systemctl daemon-reload && systemctl enable --now mysqld &> /dev/null
    fi
    [ $? -ne 0 ] && { ${COLOR}"数据库启动失败,退出!"${END};exit; }
    ${COLOR}"${PRETTY_NAME}操作系统,MySQL数据库安装完成!"${END}
}

main(){
    check_file
    install_mysql
}

if [ ${MAIN_NAME} == "Rocky" ];then
    if [ ${MAIN_VERSION_ID} == 8 -o ${MAIN_VERSION_ID} == 9 -o ${MAIN_VERSION_ID} == 10 ];then
        main
    fi
elif [ ${MAIN_NAME} == "AlmaLinux" ];then
    if [ ${MAIN_VERSION_ID} == 8 -o ${MAIN_VERSION_ID} == 9 -o ${MAIN_VERSION_ID} == 10 ];then
        main
    fi
elif [ ${MAIN_NAME} == "CentOS" ];then
    if [ ${MAIN_VERSION_ID} == 7 -o ${MAIN_VERSION_ID} == 8 -o ${MAIN_VERSION_ID} == 9 -o ${MAIN_VERSION_ID} == 10 ];then
        main
    fi
elif [ ${MAIN_NAME} == "openEuler" ];then
    if [ ${MAIN_VERSION_ID} == 22 -o ${MAIN_VERSION_ID} == 24 ];then
        main
    fi
elif [ ${MAIN_NAME} == "Anolis" ];then
    if [ ${MAIN_VERSION_ID} == 8 -o ${MAIN_VERSION_ID} == 23 ];then
        main
    fi
elif [ ${MAIN_NAME} == 'OpenCloudOS' ];then
    if [ ${MAIN_VERSION_ID} == 8 -o ${MAIN_VERSION_ID} == 9 ];then
        main
    fi
elif [ ${MAIN_NAME} == "Kylin" ];then
    if [ ${MAIN_VERSION_ID} == 10 -o ${MAIN_VERSION_ID} == 11 ];then
        main
    fi
elif [ ${MAIN_NAME} == "UOS" ];then
    if [ ${MAIN_VERSION_ID} == 20 ];then
        main
    fi
elif [ ${MAIN_NAME} == "openSUSE" ];then
    if [ ${MAIN_VERSION_ID} == 15 ];then
        main
    fi
elif [ ${MAIN_NAME} == "Ubuntu" ];then
    if [ ${MAIN_VERSION_ID} == 18 -o ${MAIN_VERSION_ID} == 20 -o ${MAIN_VERSION_ID} == 22 -o ${MAIN_VERSION_ID} == 24 ];then
        main
    fi
elif [ ${MAIN_NAME} == 'Debian' ];then
    if [ ${MAIN_VERSION_ID} == 11 -o ${MAIN_VERSION_ID} == 12 -o ${MAIN_VERSION_ID} == 13 ];then
        main
    fi
else
    ${COLOR}"此脚本不支持${PRETTY_NAME}操作系统!"${END}
fi

2.3.2 MySQL 8.4

2.3.2.1 用户和组

# Rocky、Almalinux、CentOS、openEuler、AnolisOS、OpenCloudOS、 Kylin Server、UOS Server、Ubuntu、Debian
useradd -s /sbin/nologin -r mysql

# openSUSE
groupadd -r mysql
useradd -s /sbin/nologin -r -g mysql mysql

2.3.2.2 准备程序文件

MySQL二进制包下载,去“www.mysql.com/”网站下载,选择“DO…

t79

图79 下载MySQL二进制包

选择“MySQL Community (GPL) Downloads”,如图80所示。

t80

图80 下载MySQL二进制包

然后选择“MySQL Community Server”,如图81所示。

t81

图81 下载MySQL二进制包

然后版本选择“8.4.6 LTS”,操作系统选择“Linux-Generic”,下面的有3个版本,分别是“glibc 2.28 (x86, 64-bit和ARM, 64-bit)、glibc 2.17 (x86, 64-bit)”,这个根据系统的glibc版本和主机的硬件架构类型下载,如图82所示。

t82

图82 下载MySQL二进制包

# Rocky、Almalinux、CentOS、openEuler、AnolisOS、OpenCloudOS、 Kylin Server、UOS Server
[root@rocky10 ~]# yum info glibc
Last metadata expiration check: 0:08:38 ago on Tue 10 Jun 2025 12:21:32 PM CST.
Installed Packages
Name         : glibc
Version      : 2.39 # 可以看到glibc版本是2.39
Release      : 37.el10
Architecture : x86_64
Size         : 6.2 M
Source       : glibc-2.39-37.el10.src.rpm
Repository   : @System
From repo    : anaconda
Summary      : The GNU libc libraries
URL          : http://www.gnu.org/software/glibc/
License      : LGPL-2.1-or-later AND SunPro AND LGPL-2.1-or-later WITH GCC-exception-2.0 AND BSD-3-Clause AND
             : GPL-2.0-or-later AND LGPL-2.1-or-later WITH GNU-compiler-exception AND GPL-2.0-only AND ISC AND
             : LicenseRef-Fedora-Public-Domain AND HPND AND CMU-Mach AND LGPL-2.1-only AND LGPL-2.0-or-later AND
             : Unicode-DFS-2015 AND GFDL-1.1-or-later AND GPL-1.0-or-later AND FSFUL AND MIT AND Inner-Net-2.0
             : AND X11 AND GPL-2.0-or-later WITH GCC-exception-2.0 AND GFDL-1.3-only AND GFDL-1.1-only
Description  : The glibc package contains standard libraries which are used by
             : multiple programs on the system. In order to save disk space and
             : memory, as well as to make upgrading easier, common system code is
             : kept in one place and shared between programs. This particular package
             : contains the most important sets of shared libraries: the standard C
             : library and the standard math library. Without these two libraries, a
             : Linux system will not function.

# openSUSE
opensuse15:~ # zypper info glibc
Loading repository data...
Reading installed packages...


Information for package glibc:
------------------------------
Repository     : mirror-sle-update
Name           : glibc
Version        : 2.38-150600.14.26.1 # 可以看到glibc版本是2.38
Arch           : x86_64
Vendor         : SUSE LLC <https://www.suse.com/>
Installed Size : 6.5 MiB
Installed      : Yes
Status         : out-of-date (version 2.38-150600.12.1 installed)
Source package : glibc-2.38-150600.14.26.1.src
Upstream URL   : https://www.gnu.org/software/libc/libc.html
Summary        : Standard Shared Libraries (from the GNU C Library)
Description    : 
    The GNU C Library provides the most important standard libraries used
    by nearly all programs: the standard C library, the standard math
    library, and the POSIX thread library. A system is not functional
    without these libraries.

# Ubuntu和Debian
root@ubuntu2404:~# ldd --version
ldd (Ubuntu GLIBC 2.39-0ubuntu8.4) 2.39 # 可以看到glibc版本是2.39
Copyright (C) 2024 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Written by Roland McGrath and Ulrich Drepper.

显示当前主机的硬件架构类型:

[root@rocky10 ~]# arch
x86_64

根据操作系统的glibc版本去下载相应的安装包,这里系统版本选择“Linux - Generic (glibc 2.28) (x86, 64-bit)”,然后选择下面的“mysql-8.4.6-linux-glibc2.28-x86_64.tar.xz”包后面的“Download”进行下载,**注意:**glibc版本大于等于2.28的使用glibc 2.28版的MySQL二进制包,glibc版本小于2.28的使用glibc2.17版的MySQL二进制包,如图83所示。

t83

图83 下载MySQL二进制包

# Rocky、Almalinux、CentOS、AnolisOS、OpenCloudOS、Kylin Server默认没有wget包,需要安装
yum install -y wget

# openEuler 22.03/24.03 LTS、AnolisOS 23、OpenCloudOS 9没有安装tar包,需要安装
yum install -y tar

# Rocky 8/9/10、Almalinux 8/9/10、CentOS Stream 8/9/10、openEuler 22.03/24.03 LTS、AnolisOS 8/23、OpenCloudOS 8/9、Kylin Server v10/v11、UOS Server v20、openSUSE 15、Ubuntu Server 18.04/20.04/22.04/24.04 LTS、Debian 11/12/13:
wget https://cdn.mysql.com//Downloads/MySQL-8.4/mysql-8.4.6-linux-glibc2.28-x86_64.tar.xz
tar xf mysql-8.4.6-linux-glibc2.28-x86_64.tar.xz -C /usr/local
ln -s /usr/local/mysql-8.4.6-linux-glibc2.28-x86_64/ /usr/local/mysql

# CentOS 7
wget https://cdn.mysql.com//Downloads/MySQL-8.4/mysql-8.4.6-linux-glibc2.17-x86_64.tar.xz
tar xf mysql-8.4.6-linux-glibc2.17-x86_64.tar.xz -C /usr/local
ln -s /usr/local/mysql-8.4.6-linux-glibc2.17-x86_64/ /usr/local/mysql

chown -R  mysql:mysql /usr/local/mysql/

2.3.2.3 准备环境变量

echo 'PATH=/usr/local/mysql/bin/:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh

2.3.2.4 准备配置文件

cat > /etc/my.cnf <<-EOF
[mysqld]
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

2.3.2.5 生成数据库文件,并提取root密码

mkdir -p /data/mysql

chown -R mysql:mysql /data/mysql

# /data/mysql  会自动生成,但是/data/必须事先存在
# --initialize会生成一个初始化密码,--initialize-insecure生成空密码,两种方式任选一种
mysqld --initialize --user=mysql --datadir=/data/mysql

grep password /data/mysql/mysql.log
2025-04-22T14:36:39.157585Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: K39lgjhHlu:U
# "K39lgjhHlu:U"就是初始化密码

# 生成空密码的root用户
mysqld --initialize-insecure --user=mysql --datadir=/data/mysql

Rocky 8/9/10、Almalinux 8/9/10、CentOS Stream 8/9/10、CentOS 7、openEuler 22.03/24.03 LTS、AnolisOS 8/23、OpenCloudOS 8/9:

mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory # 提示确少”libaio.so.1“文件

yum install -y libaio

mysqld --initialize-insecure --user=mysql --datadir=/data/mysql

Ubuntu Server 24.04 LTS:

mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

root@ubuntu2404:~# apt search libaio
Sorting... Done
Full Text Search... Done
libaio-dev/noble-updates 0.3.113-6build1.1 amd64
  Linux kernel AIO access library - development files

libaio1t64/noble-updates,now 0.3.113-6build1.1 amd64 [installed,automatic] # 可以看到libaio1t64包已经安装过了
  Linux kernel AIO access library - shared library

# 创建软链接
ln -s /usr/lib/x86_64-linux-gnu/libaio.so.1t64.0.2 /usr/lib/x86_64-linux-gnu/libaio.so.1

mysqld --initialize-insecure --user=mysql --datadir=/data/mysql

Ubuntu Server18.04 LTS:

mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

root@ubuntu1804:~# apt search libaio
Sorting... Done
Full Text Search... Done
libaio-dev/bionic-updates 0.3.110-5ubuntu0.1 amd64
  Linux kernel AIO access library - development files

libaio1/bionic-updates 0.3.110-5ubuntu0.1 amd64
  Linux kernel AIO access library - shared library

apt install -y libaio1

mysqld --initialize-insecure --user=mysql --datadir=/data/mysq

Debian 11/12:

root@debian12:~# mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

root@debian12:~# apt search libaio
Sorting... Done
Full Text Search... Done
libaio-dev/stable 0.3.113-4 amd64
  Linux kernel AIO access library - development files

libaio1/stable 0.3.113-4 amd64
  Linux kernel AIO access library - shared library

apt install -y libaio1

root@debian12:~# mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory

root@debian12:~# apt search libnuma
Sorting... Done
Full Text Search... Done
libnuma-dev/stable 2.0.16-1 amd64
  Development files for libnuma

libnuma1/stable 2.0.16-1 amd64
  Libraries for controlling NUMA policy

apt install -y libnuma1

mysqld --initialize-insecure --user=mysql --datadir=/data/mysql

Debian 13:

root@debian13:~# mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

root@debian13:~# apt search libaio
libaio-dev/stable 0.3.113-8+b1 amd64
  Linux kernel AIO access library - development files

libaio1t64/stable 0.3.113-8+b1 amd64
  Linux kernel AIO access library - shared library

apt install -y libaio1t64

# 创建软链接
root@debian13:~# ln -s /usr/lib/x86_64-linux-gnu/libaio.so.1t64.0.2 /usr/lib/x86_64-linux-gnu/libaio.so.1

root@debian13:~# mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory

root@debian13:~# apt search libnuma
libnuma-dev/stable 2.0.19-1 amd64
  Development files for libnuma

libnuma1/stable 2.0.19-1 amd64
  Libraries for controlling NUMA policy

apt install -y libnuma1

mysqld --initialize-insecure --user=mysql --datadir=/data/mysql

2.3.2.6 准备服务脚本和启动

Rocky、Almalinux、CentOS、openEuler、AnolisOS、OpenCloudOS、 Kylin Server、Uos Server、Ubuntu、Debian:

# Rocky 9/10、Almalinux 9/10、CentOS Stream 9/10默认没有chkconfig包,需要安装
yum install -y chkconfig

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

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

systemctl daemon-reload && systemctl enable --now mysqld

openSUSE:

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

# openSUSE
cat > /usr/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

# openSUSE
ln -s /sbin/chkconfig  /usr/lib/systemd/systemd-sysv-install
chkconfig --add mysqld

service mysqld start

2.3.2.7 修改口令

# 如果使用的是空密码,就不需要修改口令
awk '/temporary password/{print $NF}' /data/mysql/mysql.log
aupYu;uhJ9&y

mysqladmin -uroot -p'aupYu;uhJ9&y' password 123456

2.3.2.8 测试登录

Rocky 8/9、Almalinux 8/9、CentOS Stream 8/9/10、CentOS 7、openEuler 22.03/24.03 LTS、AnolisOS 8/23、OpenCloudOS 8/9、Kylin Server v10/v11、UOS Server v20、Ubuntu Server 18.04/20.04/22.04/24.04 LTS、Debian 11、openSUSE:

[root@rocky10 ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.4.6 MySQL Community Server - GPL

Copyright (c) 2000, 2025, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> status
--------------
mysql  Ver 8.4.6 for Linux on x86_64 (MySQL Community Server - GPL)

Connection id:		8
Current database:	
Current user:		root@localhost
SSL:			Not in use
Current pager:		stdout
Using outfile:		''
Using delimiter:	;
Server version:		8.4.6 MySQL Community Server - GPL
Protocol version:	10
Connection:		Localhost via UNIX socket
Server characterset:	utf8mb4
Db     characterset:	utf8mb4
Client characterset:	utf8mb4
Conn.  characterset:	utf8mb4
UNIX socket:		/data/mysql/mysql.sock
Binary data as:		Hexadecimal
Uptime:			36 sec

Threads: 2  Questions: 6  Slow queries: 0  Opens: 120  Flush tables: 3  Open tables: 36  Queries per second avg: 0.166
--------------

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> exit
Bye

Debian 12/13:

root@debian12:~# mysql
mysql: error while loading shared libraries: libncurses.so.6: cannot open shared object file: No such file or directory

root@debian12:~# apt search libncurses6
Sorting... Done
Full Text Search... Done
libncurses6/stable 6.4-4 amd64
  shared libraries for terminal handling

apt install -y libncurses6

root@debian13:~# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.4.6 MySQL Community Server - GPL

Copyright (c) 2000, 2025, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> status
--------------
mysql  Ver 8.4.6 for Linux on x86_64 (MySQL Community Server - GPL)

Connection id:		8
Current database:	
Current user:		root@localhost
SSL:			Not in use
Current pager:		stdout
Using outfile:		''
Using delimiter:	;
Server version:		8.4.6 MySQL Community Server - GPL
Protocol version:	10
Connection:		Localhost via UNIX socket
Server characterset:	utf8mb4
Db     characterset:	utf8mb4
Client characterset:	utf8mb4
Conn.  characterset:	utf8mb4
UNIX socket:		/data/mysql/mysql.sock
Binary data as:		Hexadecimal
Uptime:			19 sec

Threads: 2  Questions: 6  Slow queries: 0  Opens: 120  Flush tables: 3  Open tables: 36  Queries per second avg: 0.315
--------------

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> exit
Bye

2.3.2.9 一键安装MySQL 8.4二进制包的脚本

Shell脚本源码地址:

Gitee:gitee.com/raymond9/sh…

Github:github.com/raymond9999…

可以去上面的Gitee或Github代码仓库拉取脚本。

[root@rocky10 ~]# cat install_mysql_8.4_binary_v3.sh
#!/bin/bash
#
#**********************************************************************************
#Author:        Raymond
#QQ:            88563128
#MP:            Raymond运维
#Date:          2025-10-03
#FileName:      install_mysql_8.4_binary_v3.sh
#URL:           https://wx.zsxq.com/group/15555885545422
#Description:   The mysql binary script install supports 
#               “Rocky Linux 8, 9 and 10, AlmaLinux 8, 9 and 10, CentOS 7, 
#               CentOS Stream 8, 9 and 10, openEuler 22.03 and 24.03 LTS, 
#               AnolisOS 8 and 23, OpenCloudOS 8 and 9, Kylin Server v10 and v11, 
#               UOS Server v20, Ubuntu Server 18.04, 20.04, 22.04 and 24.04 LTS,  
#               Debian 11 , 12 and 13, openSUSE Leap 15“ operating systems.
#Copyright (C): 2025 All rights reserved
#**********************************************************************************
COLOR="echo -e \\033[01;31m"
END='\033[0m'

os(){
    . /etc/os-release
    MAIN_NAME=`sed -rn '/^NAME=/s@.*="([[:alpha:]]+).*"$@\1@p' /etc/os-release`
    if [ ${MAIN_NAME} == "Kylin" ];then
        MAIN_VERSION_ID=`sed -rn '/^VERSION_ID=/s@.*="([[:alpha:]]+)(.*)"$@\2@p' /etc/os-release`
    else
        MAIN_VERSION_ID=`sed -rn '/^VERSION_ID=/s@.*="?([0-9]+)\.?.*"?@\1@p' /etc/os-release`
    fi
}

os
DATA_DIR=/data/mysql

# mysql 8.4.6 glibc2.28包下载地址:"https://cdn.mysql.com//Downloads/MySQL-8.4/mysql-8.4.6-linux-glibc2.28-x86_64.tar.xz"
# mysql 8.4.6 glibc2.17包下载地址:"https://cdn.mysql.com//Downloads/MySQL-8.4/mysql-8.4.6-linux-glibc2.17-x86_64.tar.xz"

MYSQL_VERSION=8.4.6
if [ ${MAIN_NAME} == "CentOS" -a ${MAIN_VERSION_ID} == 7 ];then
    GLIBC_VERSION=2.17
else
    GLIBC_VERSION=2.28
fi
MYSQL_URL=https://cdn.mysql.com//Downloads/MySQL-8.4/
MYSQL_FILE="mysql-${MYSQL_VERSION}-linux-glibc${GLIBC_VERSION}-x86_64.tar.xz"

check_file(){
    if [ ${MAIN_NAME} == "Rocky" -o ${MAIN_NAME} == "AlmaLinux" -o ${MAIN_NAME} == "CentOS" -o ${MAIN_NAME} == "Anolis" -o ${MAIN_NAME} == "OpenCloudOS" -o ${MAIN_NAME} == "Kylin" ];then
        rpm -q wget &> /dev/null || { ${COLOR}"安装wget工具,请稍等......"${END};yum install -y wget &> /dev/null; }
    fi
    if [ ! -e ${MYSQL_FILE} ];then
        ${COLOR}"缺少${MYSQL_FILE}文件"${END}
        ${COLOR}'开始下载MySQL二进制安装包,请稍等......'${END}
        wget ${MYSQL_URL}${MYSQL_FILE} || { ${COLOR}"MySQL二进制安装包下载失败。"${END}; exit; }
    else
        ${COLOR}"${MYSQL_FILE}文件已准备好。"${END}
    fi
}

install_mysql(){
    [ -d /usr/local/mysql ] && { ${COLOR}"MySQL数据库已存在,安装失败!"${END};exit; }
    ${COLOR}"开始安装MySQL数据库,请稍等......"${END}
    if [ ${MAIN_NAME} == "openSUSE" ];then
        id mysql &> /dev/null || { groupadd -r mysql && useradd -s /sbin/nologin -r -g mysql mysql; ${COLOR}"成功创建mysql用户!"${END}; }
    else
        id mysql &> /dev/null || { useradd -s /sbin/nologin -r mysql ; ${COLOR}"成功创建mysql用户!"${END}; }
    fi
    if [ ${MAIN_NAME} == "openEuler" ];then
        if [ ${MAIN_VERSION_ID} == 22 -o ${MAIN_VERSION_ID} == 24 ];then
            yum install -y tar &> /dev/null
        fi
    fi
    if [ ${MAIN_NAME} == "Anolis" ];then
        if [ ${MAIN_VERSION_ID} == 23 ];then
            yum install -y tar &> /dev/null
        fi
    fi
    if [ ${MAIN_NAME} == "OpenCloudOS" ];then
        if [ ${MAIN_VERSION_ID} == 9 ];then
            yum install -y tar &> /dev/null
        fi
    fi
    tar xf ${MYSQL_FILE} -C /usr/local/
    MYSQL_DIR=`echo ${MYSQL_FILE}| sed -nr 's/^(.*[0-9]).*/\1/p'`
    ln -s /usr/local/${MYSQL_DIR} /usr/local/mysql
    chown -R mysql:mysql /usr/local/mysql/
    echo 'PATH=/usr/local/mysql/bin/:$PATH' > /etc/profile.d/mysql.sh
    . /etc/profile.d/mysql.sh
    cat > /etc/my.cnf <<-EOF
[mysqld]
datadir=${DATA_DIR}
socket=${DATA_DIR}/mysql.sock
log-error=${DATA_DIR}/mysql.log
pid-file=${DATA_DIR}/mysql.pid

[client]
socket=${DATA_DIR}/mysql.sock
EOF
    [ -d ${DATA_DIR} ] || mkdir -p ${DATA_DIR} &> /dev/null
    chown -R mysql:mysql ${DATA_DIR}
    if [ ${MAIN_NAME} == "Rocky" -o ${MAIN_NAME} == "AlmaLinux" -o ${MAIN_NAME} == "CentOS" -o ${MAIN_NAME} == "openEuler" -o ${MAIN_NAME} == "Anolis" -o ${MAIN_NAME} == "OpenCloudOS" ];then
        yum install -y libaio &> /dev/null
    fi
    if [ ${MAIN_NAME} == "Ubuntu" ];then
        if [ ${MAIN_VERSION_ID} == 24 ];then
            ln -s /usr/lib/x86_64-linux-gnu/libaio.so.1t64.0.2 /usr/lib/x86_64-linux-gnu/libaio.so.1
        fi
        if [ ${MAIN_VERSION_ID} == 18 ];then
            apt update &> /dev/null;apt install -y libaio1 &> /dev/null
        fi
    fi
    if [ ${MAIN_NAME} == "Debian" ];then
        if [ ${MAIN_VERSION_ID} == 11 -o ${MAIN_VERSION_ID} == 12 ];then
            apt update &> /dev/null;apt install -y libaio1 libnuma1 &> /dev/null
        fi
        if [ ${MAIN_VERSION_ID} == 13 ];then
            apt update &> /dev/null;apt install -y libaio1t64 libnuma1 &> /dev/null
            ln -s /usr/lib/x86_64-linux-gnu/libaio.so.1t64.0.2 /usr/lib/x86_64-linux-gnu/libaio.so.1
        fi
        if [ ${MAIN_VERSION_ID} == 12 -o ${MAIN_VERSION_ID} == 13 ];then
            apt update &> /dev/null;apt install -y libncurses6 &> /dev/null
        fi
    fi
    mysqld --initialize-insecure --user=mysql --datadir=${DATA_DIR}
    if [ ${MAIN_NAME} == "Rocky" ];then
        if [ ${MAIN_VERSION_ID} == 9 -o ${MAIN_VERSION_ID} == 10 ];then
            yum install -y chkconfig &> /dev/null
        fi
    fi
    if [ ${MAIN_NAME} == "AlmaLinux" ];then
        if [ ${MAIN_VERSION_ID} == 9 -o ${MAIN_VERSION_ID} == 10 ];then
            yum install -y chkconfig &> /dev/null
        fi
    fi
    if [ ${MAIN_NAME} == "CentOS" ];then
        if [ ${MAIN_VERSION_ID} == 9 -o ${MAIN_VERSION_ID} == 10 ];then
            yum install -y chkconfig &> /dev/null
        fi
    fi
    cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
    if [ ${MAIN_NAME} == "openSUSE" ];then
       cat > /usr/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
        ln -s /sbin/chkconfig  /usr/lib/systemd/systemd-sysv-install
		chkconfig --add mysqld  &> /dev/null
        service mysqld start
    else
        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
        systemctl daemon-reload && systemctl enable --now mysqld &> /dev/null
    fi
    [ $? -ne 0 ] && { ${COLOR}"数据库启动失败,退出!"${END};exit; }
    ${COLOR}"${PRETTY_NAME}操作系统,MySQL数据库安装完成!"${END}
}

main(){
    check_file
    install_mysql
}

if [ ${MAIN_NAME} == "Rocky" ];then
    if [ ${MAIN_VERSION_ID} == 8 -o ${MAIN_VERSION_ID} == 9 -o ${MAIN_VERSION_ID} == 10 ];then
        main
    fi
elif [ ${MAIN_NAME} == "AlmaLinux" ];then
    if [ ${MAIN_VERSION_ID} == 8 -o ${MAIN_VERSION_ID} == 9 -o ${MAIN_VERSION_ID} == 10 ];then
        main
    fi
elif [ ${MAIN_NAME} == "CentOS" ];then
    if [ ${MAIN_VERSION_ID} == 7 -o ${MAIN_VERSION_ID} == 8 -o ${MAIN_VERSION_ID} == 9 -o ${MAIN_VERSION_ID} == 10 ];then
        main
    fi
elif [ ${MAIN_NAME} == "openEuler" ];then
    if [ ${MAIN_VERSION_ID} == 22 -o ${MAIN_VERSION_ID} == 24 ];then
        main
    fi
elif [ ${MAIN_NAME} == "Anolis" ];then
    if [ ${MAIN_VERSION_ID} == 8 -o ${MAIN_VERSION_ID} == 23 ];then
        main
    fi
elif [ ${MAIN_NAME} == 'OpenCloudOS' ];then
    if [ ${MAIN_VERSION_ID} == 8 -o ${MAIN_VERSION_ID} == 9 ];then
        main
    fi
elif [ ${MAIN_NAME} == "Kylin" ];then
    if [ ${MAIN_VERSION_ID} == 10 -o ${MAIN_VERSION_ID} == 11 ];then
        main
    fi
elif [ ${MAIN_NAME} == "UOS" ];then
    if [ ${MAIN_VERSION_ID} == 20 ];then
        main
    fi
elif [ ${MAIN_NAME} == "openSUSE" ];then
    if [ ${MAIN_VERSION_ID} == 15 ];then
        main
    fi
elif [ ${MAIN_NAME} == "Ubuntu" ];then
    if [ ${MAIN_VERSION_ID} == 18 -o ${MAIN_VERSION_ID} == 20 -o ${MAIN_VERSION_ID} == 22 -o ${MAIN_VERSION_ID} == 24 ];then
        main
    fi
elif [ ${MAIN_NAME} == 'Debian' ];then
    if [ ${MAIN_VERSION_ID} == 11 -o ${MAIN_VERSION_ID} == 12 -o ${MAIN_VERSION_ID} == 13 ];then
        main
    fi
else
    ${COLOR}"此脚本不支持${PRETTY_NAME}操作系统!"${END}
fi