1.数据库安装(通过官方yum安装,略过)

2.创建并初始化数据目录

 mkdir -p /data/mysql/{3301,3302,3303,3304,3305}
 chown mysql:mysql /data/mysql/*

3.通过配置文件指定并初始化数据目录

[root@localhost ~]# cp /etc/my.cnf /data/mysql/3301.cnf
[root@localhost ~]# vim /data/mysql/3301.cnf
[mysqld]
port=3301
datadir=/data/mysql/3301/
socket=/tmp/mysql3301.sock
symbolic-links=0

[mysqld_safe]
log-error=/data/mysql/3301.log
pid-file=/usr/local/mysql/data/3301.pid

[client]
port=3301
socket=/tmp/mysql3301.sock

[root@localhost ~]# mysqld --defaults-file=/data/mysql/3301.cnf --initialize-insecure --user=mysql

3301.cnf初始化使用,后续不再需要,重复以上步骤完成3302-3306

4.配置my.cnf文件集中管理多个实例

[root@localhost ~]# cp /etc/my.cnf /etc/my.cnf.bak
[root@localhost ~]# vim /etc/my.cnf
[mysqld_multi]
mysqld=/usr/bin/mysqld_safe
mysqladmin=/usr/bin/mysqladmin

[mysqld3301]
port=3301
socket=/tmp/mysql3301.sock
datadir=/data/mysql/3301/
skip-external-locking
log-bin=/data/mysql/3301/mysql-bin
server-id=3301
user=mysql
character-set-server = utf8
collation-server = utf8_general_ci

......
......

[mysqld3306]
port=3306
socket=/tmp/mysql3306.sock
datadir=/data/mysql/3306/
skip-external-locking
log-bin=/data/mysql/3306/mysql-bin
server-id=3306
user=mysql
character-set-server = utf8
collation-server = utf8_general_ci

[mysql]
no-auto-rehash

mysqld_multi的配置文件和一般MySQL配置不同,没有[mysqld]段,取而代之的是[mysqld1]、[mysqld2]等配置段,每个配置段代表一个MySQL实例。

5.启动多个MySQL实例

[root@localhost ~]# /etc/init.d/mysqld_multi start 3301-3306
[root@localhost ~]# /etc/init.d/mysqld_multi start 3302,3303

6.关闭多个MySQL实例

[root@localhost ~]# /etc/init.d/mysqld_multi stop 3301-3306

但此时发现无法关闭。

7.解决无法通过mysqld_multi关闭mysql实例

给每个实例新建关闭权限的帐号

# 3301为例:
shell> mysql -u root -S /tmp/mysql3301.sock -p
Enter password:
mysql> CREATE USER 'multi_admin'@'localhost' IDENTIFIED BY '123456';
mysql> GRANT SHUTDOWN ON *.* TO 'multi_admin'@'localhost';

其他3302-3306重复该操作

修改my.conf的[mysqld_multi]模块

[mysqld_multi]
mysqld = /usr/bin/mysqld_safe
mysqladmin = /usr/bin/mysqladmin
user = multi_admin
password = 123456

修改mysqld_multi命令

vim /usr/bin/mysqld_multi
# 找到以下行
my $com= join ' ', 'my_print_defaults', @defaults_options, $group;
# 修改my_print_defaults为:my_print_defaults -s
my $com= join ' ', 'my_print_defaults -s', @defaults_options, $group;

再用mysqld_multi就能停库了。