1、事先从官网/国内镜像站点中下载RPM安装包,上传至服务器:

[root@localhost ~]# wget -c https://repo.huaweicloud.com/mysql/Downloads/MySQL-5.7/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
--2022-04-05 16:43:04--  https://repo.huaweicloud.com/mysql/Downloads/MySQL-5.7/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
Resolving repo.huaweicloud.com (repo.huaweicloud.com)... 59.39.0.154, 59.39.0.150, 59.39.0.156, ...
Connecting to repo.huaweicloud.com (repo.huaweicloud.com)|59.39.0.154|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 666603757 (636M) [application/octet-stream]
Saving to: ‘mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz’

100%[=================================================================================================================================>] 666,603,757 5.55MB/s   in 2m 12s 

2022-04-05 16:45:16 (4.80 MB/s) - ‘mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz’ saved [666603757/666603757]

[root@localhost ~]# ll        //查看刚下载的安装包
total 650988
-rw-------. 1 root root      1392 Mar 26 17:20 anaconda-ks.cfg
-rw-r--r--  1 root root 666603757 Nov 30 03:55 mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz

2、创建用户和组

[root@node5 ~]# groupadd -r -g 303 mysql    //创建系统组并设置组号为303
[root@node5 ~]# useradd -r -g 303 -u 303 -s /bin/false mysql    //创建系统用户,设置组号为303,用户id为303,“-s /bin/false”表示此用户无需登录

[root@localhost ~]# id mysql    //查看结果
uid=303(mysql) gid=303(mysql) groups=303(mysql)

3、解压安装包创建软连接

[root@node5 ~]# tar xf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
//-C表示指定解压目录为/usr/local/

[root@localhost ~]# ll /usr/local/        //查看解压结果
total 0
drwxr-xr-x. 2 root root   6 Apr 11  2018 bin
drwxr-xr-x. 2 root root   6 Apr 11  2018 etc
drwxr-xr-x. 2 root root   6 Apr 11  2018 games
drwxr-xr-x. 2 root root   6 Apr 11  2018 include
drwxr-xr-x. 2 root root   6 Apr 11  2018 lib
drwxr-xr-x. 2 root root   6 Apr 11  2018 lib64
drwxr-xr-x. 2 root root   6 Apr 11  2018 libexec
drwxr-xr-x  9 root root 129 Apr  5 16:58 mysql-5.7.37-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root   6 Apr 11  2018 sbin
drwxr-xr-x. 5 root root  49 Mar 26 17:17 share
drwxr-xr-x. 2 root root   6 Apr 11  2018 src

4、创建软连接(方便,名字简短)

[root@localhost ~]# ln -sv /usr/local/mysql-5.7.37-linux-glibc2.12-x86_64/ /usr/local/mysql
‘/usr/local/mysql’ -> ‘/usr/local/mysql-5.7.37-linux-glibc2.12-x86_64/’
[root@localhost ~]# ll /usr/local/        //查看软连接情况
total 0
drwxr-xr-x. 2 root root   6 Apr 11  2018 bin
drwxr-xr-x. 2 root root   6 Apr 11  2018 etc
drwxr-xr-x. 2 root root   6 Apr 11  2018 games
drwxr-xr-x. 2 root root   6 Apr 11  2018 include
drwxr-xr-x. 2 root root   6 Apr 11  2018 lib
drwxr-xr-x. 2 root root   6 Apr 11  2018 lib64
drwxr-xr-x. 2 root root   6 Apr 11  2018 libexec
lrwxrwxrwx  1 root root  47 Apr  5 16:58 mysql -> /usr/local/mysql-5.7.37-linux-glibc2.12-x86_64/
drwxr-xr-x  9 root root 129 Apr  5 16:58 mysql-5.7.37-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root   6 Apr 11  2018 sbin
drwxr-xr-x. 5 root root  49 Mar 26 17:17 share
drwxr-xr-x. 2 root root   6 Apr 11  2018 src

5、准备数据目录

[root@localhost ~]# mkdir -p /data/330{6..8}/data    //由于/目录下本身没有data目录,因此需要加-p参数
[root@localhost ~]# tree /data      //查看创建结果
/data
├── 3306
│   └── data
├── 3307
│   └── data
└── 3308
    └── data

6 directories, 0 files

6、后面操作可分为两种方法

1)方法一

①为每一个MySQL多实例创建主配置文件

[root@localhost ~]# vim /data/3306/my.cnf    //先创建3306的主配置文件
[root@localhost ~]# cat /data/3306/my.cnf     //多实例主配置文件格式如下
[client]
port = 3306
socket = /data/3306/mysql.sock

[mysqld]
user = mysql
port = 3306
socket = /data/3306/mysql.sock
basedir = /usr/local/mysql
datadir = /data/3306/data
server-id = 3306

[mysqldump]
quick
max_allowed_packet = 16M

[mysqld_safe]
log-error=/data/3306/mysql_3306.err
pid-file=/data/3306/mysqld.pid

//通过cp复制3306的主配置文件给3307和3308
[root@localhost ~]# cp /data/3306/my.cnf /data/3307/
[root@localhost ~]# cp /data/3306/my.cnf /data/3308/

//通过sed将3306全局替换为3307或3308
[root@localhost ~]# sed -i 's/3306/3307/g' /data/3307/my.cnf 
[root@localhost ~]# sed -i 's/3306/3308/g' /data/3308/my.cnf 

[root@localhost ~]# cat /data/3307/my.cnf     //查看3307的主配置文件修改结果
[client]
port = 3307
socket = /data/3307/mysql.sock

[mysqld]
user = mysql
port = 3307
socket = /data/3307/mysql.sock
basedir = /usr/local/mysql
datadir = /data/3307/data
server-id = 3307

[mysqldump]
quick
max_allowed_packet = 16M

[mysqld_safe]
log-error=/data/3307/mysql_3307.err
pid-file=/data/3307/mysqld.pid

[root@localhost ~]# cat /data/3308/my.cnf     //查看3308的主配置文件修改结果
[client]
port = 3308
socket = /data/3308/mysql.sock

[mysqld]
user = mysql
port = 3308
socket = /data/3308/mysql.sock
basedir = /usr/local/mysql
datadir = /data/3308/data
server-id = 3308

[mysqldump]
quick
max_allowed_packet = 16M

[mysqld_safe]
log-error=/data/3308/mysql_3308.err
pid-file=/data/3308/mysqld.pid

②修改MySQL多实例的目录权限

[root@localhost ~]# chown -R mysql:mysql /data/330{6..8}    //将所有文件的所属用户和所属组改为mysql

//查看权限修改结果
[root@localhost ~]# ll /data/3306
total 4
drwxr-xr-x 2 mysql mysql   6 Apr  5 16:59 data
-rw-r--r-- 1 mysql mysql 314 Apr  5 17:02 my.cnf
[root@localhost ~]# ll /data/3307
total 4
drwxr-xr-x 2 mysql mysql   6 Apr  5 16:59 data
-rw-r--r-- 1 mysql mysql 314 Apr  5 17:03 my.cnf
[root@localhost ~]# ll /data/3308
total 4
drwxr-xr-x 2 mysql mysql   6 Apr  5 16:59 data
-rw-r--r-- 1 mysql mysql 314 Apr  5 17:03 my.cnf

③配置mysql环境变量

【可以先配,也可以后配,先配可以在初始化时路径写短一点(原本需要完整写出路径mysql/bin/mysqld,现在路径可以直接写mysqld),仅此而已】

[root@localhost ~]# vim /etc/profile.d/mysql.sh
[root@localhost ~]# cat /etc/profile.d/mysql.sh 
export PATH=$PATH:/usr/local/mysql/bin

[root@localhost ~]# source /etc/profile.d/mysql.sh        //执行shell脚本

④初始化多实例

[root@localhost ~]# mysqld --defaults-file=/mysql/3307/my.cnf --initialize --basedir=/mysql --datadir=/mysql/3307/data/ --user=mysql
//--initialize表示初始化 --user表示用户为mysql --basedir表示指定基本目录 --datadir表示指定数据目录
2022-04-05T09:07:26.615796Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-04-05T09:07:26.775785Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-04-05T09:07:26.840052Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-04-05T09:07:26.920107Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: d05dc030-b4bf-11ec-9a73-000c2926cea3.
2022-04-05T09:07:26.921090Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-04-05T09:07:27.444106Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-04-05T09:07:27.444120Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-04-05T09:07:27.444685Z 0 [Warning] CA certificate ca.pem is self signed.
2022-04-05T09:07:27.544611Z 1 [Note] A temporary password is generated for root@localhost: sBy,2mP4CC,J
//注:最下方末尾处为MySQL临时密码,需要记住

[root@localhost ~]# mysqld --initialize --datadir=/data/3307/data --basedir=/usr/local/mysql --user=mysql
//--initialize表示初始化 --user表示用户为mysql --basedir表示指定基本目录 --datadir表示指定数据目录
2022-04-05T09:07:45.055762Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-04-05T09:07:45.238123Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-04-05T09:07:45.264872Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-04-05T09:07:45.328227Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: db569b53-b4bf-11ec-9c11-000c2926cea3.
2022-04-05T09:07:45.329774Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-04-05T09:07:45.733510Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-04-05T09:07:45.733524Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-04-05T09:07:45.734202Z 0 [Warning] CA certificate ca.pem is self signed.
2022-04-05T09:07:45.775601Z 1 [Note] A temporary password is generated for root@localhost: fHs!imS!e7Hm
//注:最下方末尾处为MySQL临时密码,需要记住

[root@localhost ~]# mysqld --initialize --datadir=/data/3308/data --basedir=/usr/local/mysql --user=mysql
//--initialize表示初始化 --user表示用户为mysql --basedir表示指定基本目录 --datadir表示指定数据目录
2022-04-05T09:07:53.308148Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-04-05T09:07:53.467427Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-04-05T09:07:53.494582Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-04-05T09:07:53.550891Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: e03d4937-b4bf-11ec-9d3a-000c2926cea3.
2022-04-05T09:07:53.551757Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-04-05T09:07:54.163588Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-04-05T09:07:54.163602Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-04-05T09:07:54.164177Z 0 [Warning] CA certificate ca.pem is self signed.
2022-04-05T09:07:54.526152Z 1 [Note] A temporary password is generated for root@localhost: nDj>WWNmG6gr
//注:最下方末尾处为MySQL临时密码,需要记住

⑤启动MySQL多实例

[root@localhost ~]# touch /data/3306/mysql_3306.err /data/3307/mysql_3307.err /data/3308/mysql_3308.err    //先创建以上多实例对应错误文件,不然启动多实例会报错(可以自己测试一下)
[root@localhost ~]# chown mysql.mysql /data/3306/mysql_3306.err /data/3307/mysql_3307.err /data/3308/mysql_3308.err    //修改多实例对应错误文件的所属用户和所属组为mysql

[root@localhost ~]# mysqld_safe --defaults-file=/mysql/3306/my.cnf &        //启动3306多实例
[1] 1614
[root@localhost ~]# 2022-04-05T09:12:06.906029Z mysqld_safe Logging to '/data/3306/mysql_3306.err'.
2022-04-05T09:12:06.935022Z mysqld_safe Starting mysqld daemon with databases from /data/3306/data

[root@localhost ~]# mysqld_safe --defaults-file=/data/3307/my.cnf &        //启动3307多实例
[2] 1804
[root@localhost ~]# 2022-04-05T09:12:51.169814Z mysqld_safe Logging to '/data/3307/mysql_3307.err'.
2022-04-05T09:12:51.197177Z mysqld_safe Starting mysqld daemon with databases from /data/3307/data

[root@localhost ~]# mysqld_safe --defaults-file=/data/3308/my.cnf &        //启动3308多实例
[3] 1995
[root@localhost ~]# 2022-04-05T09:12:56.002696Z mysqld_safe Logging to '/data/3308/mysql_3308.err'.
2022-04-05T09:12:56.031071Z mysqld_safe Starting mysqld daemon with databases from /data/3308/data

//查看对应端口号是否开启(判断多实例MySQL服务是否均已开启)
[root@localhost ~]# netstat -lnpt | grep :330
tcp6       0      0 :::3306                 :::*                    LISTEN      1776/mysqld         
tcp6       0      0 :::3307                 :::*                    LISTEN      1966/mysqld         
tcp6       0      0 :::3308                 :::*                    LISTEN      2157/mysqld         

⑥登录MySQL

[root@localhost ~]# mysql -uroot -p'sBy,2mP4CC,J' -S /data/3306/mysql.sock
//登录3306(末尾需要通过-S加上套接字文件路径区分不同多实例MySQL服务)
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.37

Copyright (c) 2000, 2022, 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> \q
Bye

[root@localhost ~]# mysql -uroot -p'fHs!imS!e7Hm' -S /data/3307/mysql.sock
//登录3307(末尾需要通过-S加上套接字文件路径区分不同多实例MySQL服务)
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.37

Copyright (c) 2000, 2022, 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> \q
Bye

[root@localhost ~]# mysql -uroot -p'nDj>WWNmG6gr' -S /data/3308/mysql.sock
//登录3308(末尾需要通过-S加上套接字文件路径区分不同多实例MySQL服务)
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.37

Copyright (c) 2000, 2022, 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> \q
Bye

⑦修改密码

[root@localhost ~]# mysqladmin -uroot -p'sBy,2mP4CC,J' password 'MySQL@123' -S /data/3306/mysql.sock
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

[root@localhost ~]# mysqladmin -uroot -p'fHs!imS!e7Hm' password 'MySQL@123' -S /data/3307/mysql.sock
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

[root@localhost ~]# mysqladmin -uroot -p'nDj>WWNmG6gr' password 'MySQL@123' -S /data/3308/mysql.sock
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

⑧关闭多实例

[root@localhost ~]# mysqladmin -uroot -p'MySQL@123' -S /data/3306/mysql.sock shutdown
mysqladmin: [Warning] Using a password on the command line interface can be insecure.

[root@localhost ~]# netstat -lnpt | grep :330    //查看关闭结果
tcp6       0      0 :::3307                 :::*                    LISTEN      1966/mysqld         
tcp6       0      0 :::3308                 :::*                    LISTEN      2157/mysqld