一个机器上安装多个mysql实例,除了将每个实例在不同的目录下编译安装,为每个实例指定不同的端口,socket,配置文件,安装目录等。还可以使用mysqld_multi方式。
mysqld_multi是mysql设计的专门用来管理不同端口,不同socket的mysql实例的工具。它可以同时启动,停止多个mysql实例,也可以查看这些实例的状态。
mysqld_multi会去配置文件my.cnf(或者是—defaults-file参数指定的配置文件)中检查每一个以[mysqldN]为开头的一组配置(N这里是数字)。这个N是mysqld_multi用来区分每一个mysql实例用的。用法如下:
mysqld_multi start|stop|reload|report
N1, N2, N3,… //reload等价于stop和start。
mysqld_multi需要的信息记录在配置文件my.cnf中的[mysqld_multi]组下。
注意,为了能用mysqld_multi统计管理所有的mysql实例,该管理账号必须存在于所有的mysql实例上,而且密码也一致。
以下是我做的测试以及安装步骤:
1、编写配置文件my.cnf,如下:
[root@CentOSMysql1 etc]# cat
my.cnf
[mysqld_multi]
mysqld=mysqld_safe
#表示用mysqld_safe来启动mysql
mysqladmin=/usr/local/mysql/bin/mysqladmin
#指定mysqladmin工具的路径
log=/usr/local/mysql/mysqld_multi.log
#指定mysqld_multi的日志文件
user=root
pass=123456
#指定使用mysqld_multi工具的用户和密码
[mysqld3307]
#指定实例编号为3307的一些配置参数
datadir=/opt/data3307
port=3307
socket=/tmp/mysql.sock3307
[mysqld3308]
#指定实例编号为3308的一些配置参数
datadir=/opt/data3308
port=3308
socket=/tmp/mysql.sock3308
[mysqld56]
#指定实例编号为56的一些配置参数
basedir=/usr/local/mysql56/
datadir=/opt/data56
socket=/tmp/mysql.sock56
port=3310
2、准备安装之前工作
(1)查看是否有mysql用户和mysql组
[root@CentOSMysql1 etc]# cat
group |grep mysql
[root@CentOSMysql1 etc]#
groupadd mysql
[root@CentOSMysql1 mysql]# cat
/etc/passwd|grep mysql
[root@CentOSMysql1 etc]#
useradd -g mysql mysql
(2)下载mysql的安装文件。这里我下载的都是linux generic类型的
(3)解压这些安装包
[root@CentOSMysql1 opt]# tar
-zxf mysql-5.6.34-linux-glibc2.5-x86_64.tar.gz
[root@CentOSMysql1 opt]# tar
-zxf mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz
3、安装编号为3307的mysql实例,这里我计算用mysql5.7的版本。
[root@CentOSMysql1 opt]#mv mysql-5.7.16-linux-glibc2.5-x86_64 /usr/local
[root@CentOSMysql1 opt]#cd /usr/local/
[root@CentOSMysql1 local]#ln –s mysql-5.7.16-linux-glibc2.5-x86_64
mysql
[root@CentOSMysql1 local]#cd
mysql
[root@CentOSMysql1 mysql]# chown -R mysql:mysql .
[root@CentOSMysql1 mysql]# bin/mysqld
--initialize --user=mysql --datadir=/opt/data3307
//这条命令执行成功之后,会在结尾打印出临时密码,如下:
2017-12-21T02:08:32.598340Z 0
[Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use
--explicit_defaults_for_timestamp server option (see documentation for more
details).
2017-12-21T02:08:33.002195Z 0
[Warning] InnoDB: New log files created, LSN=45790
2017-12-21T02:08:33.090315Z 0
[Warning] InnoDB: Creating foreign key constraint system tables.
2017-12-21T02:08:33.179548Z 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:
d898cae9-e5f3-11e7-9e66-000c299323ae.
2017-12-21T02:08:33.181477Z 0
[Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed'
cannot be opened.
2017-12-21T02:08:33.184759Z 1
[Note] A temporary password is generated for root@localhost:L;0/g:XaweYi
[root@CentOSMysql1
mysql]# bin/mysql_ssl_rsa_setup
--datadir=/opt/data3307
到这里,编号为3307的实例初步安装完毕,还剩下一个步骤就是修改临时密码。改密码之前需要把其所在mysql服务启动起来,这里我们用mysqld_multi启动,先查看下状态,如下:
[root@CentOSMysql1
bin]#mysqld_multireport
-bash: mysqld_multi: command
not found
解决方法为将mysqld_multi工具所在的路径添加到环境变量中去:
[root@CentOSMysql1 bin]# vi
/etc/profile
//在文件尾部添加以下语句
PATH=/usr/local/mysql/bin:$PATH
"/etc/profile" 79L,
1828C written
[root@CentOSMysql1 bin]#
source /etc/profile
环境变量生效之后重试
[root@CentOSMysql1 mysql]#
mysqld_multi report
Reporting MySQL servers
MySQL server from group:
mysqld3307 is not running
MySQL server from group:
mysqld3308 is not running
MySQL server from group:
mysqld56 is not running
[root@CentOSMysql1 mysql]#
mysqld_multi start 3307
[root@CentOSMysql1 mysql]#
mysqld_multi report
Reporting MySQL servers
MySQL server from group:
mysqld3307 is running
MySQL server from group: mysqld3308
is not running
MySQL server from group:
mysqld56 is not running
可以看到编号3307的mysql实例已经启动,然后修改root密码。因为my.cnf里面配置了root用户的密码是123456,所以现在我们将root的密码改为“123456”:
[root@CentOSMysql1
mysql]# mysql -uroot -p -S /tmp/mysql.sock3307
Enter password:
//这里输出刚才安装时出现的临时密码。
mysql> set password=password("123456");
到这里,编号为3307的实例安装完毕。
4、安装编号为3308的mysql实例,这里仍用mysql5.7的版本,但是端口号以及datadir要根据my.cnf中的来。
[root@CentOSMysql1 mysql]#
bin/mysqld --initialize --user=mysql --datadir=/opt/data3308
2017-12-21T02:36:31.294011Z 0
[Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use
--explicit_defaults_for_timestamp server option (see documentation for more
details).
2017-12-21T02:36:31.779696Z 0
[Warning] InnoDB: New log files created, LSN=45790
2017-12-21T02:36:31.881428Z 0
[Warning] InnoDB: Creating foreign key constraint system tables.
2017-12-21T02:36:31.948774Z 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: c138a63c-e5f7-11e7-8f7a-000c299323ae.
2017-12-21T02:36:31.950062Z 0
[Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed'
cannot be opened.
2017-12-21T02:36:31.953758Z 1
[Note] A temporary password is generated for root@localhost: whKyXCtxz9;g
//临时密码同样在尾处生成。
[root@CentOSMysql1 mysql]#
bin/mysql_ssl_rsa_setup
--datadir=/opt/data3308
拉起编号3308的mysql实例服务
[root@CentOSMysql1 mysql]#
mysqld_multi start 3308
修改编号3308的mysql实例中root用户的密码为“123456”。
[root@CentOSMysql1 mysql]#
mysql -uroot -p -S /tmp/mysql.sock3308
Enter password:
//这里输出刚才安装时出现的临时密码。
mysql> set
password=password("123456");
到这里,编号为3308的实例安装完毕。
5、安装编号为56的mysql实例,这里使用mysql5.6的版本,由安装目录,端口号以及datadir根据my.cnf中的来。
[root@CentOSMysql1 opt]#mv mysql-5.6.34-linux-glibc2.5-x86_64
/usr/local
[root@CentOSMysql1 opt]#cd
/usr/local/
[root@CentOSMysql1 local]#ln
–s mysql-5.6.34-linux-glibc2.5-x86_64 mysql56
[root@CentOSMysql1 local]# cd
mysql56
[root@CentOSMysql1 mysql56]#
chown -R mysql:mysql .
[root@CentOSMysql1 mysql56]#
scripts/mysql_install_db --user=mysql
--datadir=/opt/data56
这一步和mysql5.7有些不一样,不会生成临时密码。Mysql5.6的临时密码默认是空。
[root@CentOSMysql1 local]#
mysqld_multi start 56
拉起编号56的mysql实例服务
[root@CentOSMysql1 local]#
mysql -uroot -p -S /tmp/mysql.sock56
修改编号56的mysql实例中root用户的密码为“123456”。
mysql> set
password="123456";
ERROR 1372 (HY000): Password
hash should be a 41-digit hexadecimal number
mysql> set
password=password("123456");
到这里,编号为56的实例安装完毕。
6、最后,试试用mysqld_multi统一起停所有的mysql实例
[root@CentOSMysql1 ~]#
mysqld_multi report
Reporting MySQL servers
MySQL server from group:
mysqld3307 is running
MySQL server from group:
mysqld3308 is running
MySQL server from group:
mysqld56 is running
[root@CentOSMysql1 ~]#
mysqld_multi report
Reporting MySQL servers
MySQL server from group:
mysqld3307 is running
MySQL server from group:
mysqld3308 is running
MySQL server from group:
mysqld56 is running
[root@CentOSMysql1 ~]#
mysqld_multi stop
[root@CentOSMysql1 ~]#
mysqld_multi report
Reporting MySQL servers
MySQL server from group:
mysqld3307 is not running
MySQL server from group:
mysqld3308 is not running
MySQL server from group:
mysqld56 is not running
[root@CentOSMysql1 ~]#
mysqld_multi start
[root@CentOSMysql1 ~]#
mysqld_multi report
Reporting MySQL servers
MySQL server from group:
mysqld3307 is running
MySQL server from group:
mysqld3308 is running
MySQL server from group:
mysqld56 is running