1.创建mysql用户以及组

groupadd mysql
useradd -r -g mysql -s /bin/false mysql

2.创建Mysql数据目录

mkdir -p /data/mysql/3306
mkdir -p /data/mysql/3307
mkdir -p /data/mysql/3308
chown -R mysql.mysql /data/

3.创建mysql logs目录

mkdir -p /logs/mysql/3306
mkdir -p /logs/mysql/3307
mkdir -p /logs/mysql/3308
mkdir -p /var/run/mysqld
chown -R mysql.mysql /logs/
chown -R mysql.mysql /var/run/mysqld

4.解压Mysql安装包

tar xvf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz -C /
cd /
mv mysql-5.7.26-linux-glibc2.12-x86_64/ mysql
chown -R mysql.mysql /mysql

5.将Mysql加入环境变量

[root@mysqlmulti /]# . ~/.bash_profile 
[root@mysqlmulti /]# cat ~/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin:/mysql/bin
export PATH

6.初始化数据

cd /mysql/bin
./mysqld --initialize --user=mysql --basedir=/mysql --datadir=/data/mysql/3306 --explicit_defaults_for_timestamp
./mysqld --initialize --user=mysql --basedir=/mysql --datadir=/data/mysql/3307 --explicit_defaults_for_timestamp
./mysqld --initialize --user=mysql --basedir=/mysql --datadir=/data/mysql/3308 --explicit_defaults_for_timestamp

7.修改配置文件

[root@mysqlmulti run]# cat /etc/my.cnf
[mysqld_multi]
mysqld = /mysql/bin/mysqld_safe
mysqladmin = /mysql/bin/mysqladmin

[mysqld3306]
port = 3306
server_id = 3306
basedir =/mysql
datadir =/data/mysql/3306
log-bin=/data/mysql/3306/mysql-bin
socket =/tmp/mysql3306.sock
log-error =/logs/mysql/3306/mysqld3306.log
pid-file =/var/run/mysqld/mysqld3306.pid

[mysqld3307]
port = 3307
server_id = 3307
basedir =/mysql
datadir =/data/mysql/3307
log-bin=/data/mysql/3307/mysql-bin
socket =/tmp/mysql3307.sock
log-error =/logs/mysql/3307/mysqld3307.log
pid-file =/var/run/mysqld/mysqld3307.pid

[mysqld3308]
port = 3308
server_id = 3308
basedir =/mysql
datadir =/data/mysql/3308
log-bin=/data/mysql/3308/mysql-bin
socket =/tmp/mysql3308.sock
log-error =/logs/mysql/3308/mysqld3308.log
pid-file =/var/run/mysqld/mysqld3308.pid

8.创建日志文件

touch /logs/mysql/3306/mysqld3306.log 
touch /logs/mysql/3307/mysqld3307.log
touch /logs/mysql/3308/mysqld3308.log
chown -R mysql.mysql /logs/

9.启动3306,3307,3308实例

mysqld_multi start 3306
mysqld_multi start 3307
mysqld_multi start 3308

10.访问3306、3307、3308实例,密码是第六步初始化数据生成的密码

mysql -uroot -p -S /tmp/mysql3306.sock
mysql -uroot -p -S /tmp/mysql3307.sock
mysql -uroot -p -S /tmp/mysql3308.sock

11.更改密码

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'wwwwww'; 
mysql> FLUSH PRIVILEGES;

12.查看多实例状态

[root@mysqlmulti bin]# mysqld_multi report
WARNING: Log file disabled. Maybe directory or file isn't writable?
mysqld_multi log file version 2.16; run: Sat Jul 13 23:38:27 2019
Reporting MySQL servers
MySQL server from group: mysqld3306 is running
MySQL server from group: mysqld3307 is running
MySQL server from group: mysqld3308 is running