本文以MySQL5.7为例,介绍在同一台机器下如何安装多个MySQL实例。
环境:RHEL 6.5 MySQL 5.7
1.单实例MySQL安装
先来看下MySQL5.7单实例的安装方法(这里引用官方文档的安装步骤):
shell> groupadd mysql
shell> useradd -r -g mysql -s /bin/false mysql
shell> cd /usr/local
shell> tar zxvf /path/to/mysql-VERSION-OS.tar.gz
shell> ln -s full-path-to-mysql-VERSION-OS mysql
shell> cd mysql
shell> mkdir mysql-files
shell> chown mysql:mysql mysql-files
shell> chmod 750 mysql-files
shell> bin/mysqld --initialize --user=mysql
shell> bin/mysql_ssl_rsa_setup
shell> bin/mysqld_safe --user=mysql &
# Next command is optional
shell> cp support-files/mysql.server /etc/init.d/mysql.server
--修改/etc/profile 增加
export PATH=/usr/local/mysql/bin:$PATH
--需要修改密码才可以正常操作数据库:
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> alter user 'root'@'localhost' identified by '123';
Query OK, 0 rows affected (0.00 sec)
可以看到单实例MySQL的安装非常简单,只需创建了必要的用户组和用户,解压二进制软件包,初始化然后调整下权限基本就OK了。
2.多实例MySQL配置
下面来看下多实例MySQL配置,如果需要安装多实例在同一台机器上,主要4个地方的配置需要区分:端口(Port)、数据目录(datadir)、socket文件、server-id标识。(注:对其他配置可以根据实际需求去自定义各个实例的一些参数)
MySQL的配置文件my.cnf支持标签重定义,比如我这里安装4个实例,分别定义为[mysqld1]、[mysqld2]、[mysqld3]、[mysqld4]。
举例来说,我在原本的标准my.cnf配置文件中增加如下配置内容:
...这里省略原本的my.cnf配置内容...
[mysqld_multi]
mysqld=/usr/local/mysql/bin/mysqld_safe
mysqladmin=/usr/local/mysql/bin/mysqladmin
Log=/usr/local/mysql/multi.log
[mysqld1]
server-id=101
datadir=/data/mysqldata/3307
socket=/tmp/mysql.sock3307
port=3307
[mysqld2]
server-id=102
datadir=/data/mysqldata/3308
socket=/tmp/mysql.sock3308
port=3308
[mysqld3]
server-id=103
datadir=/data/mysqldata/3309
socket=/tmp/mysql.sock3309
port=3309
[mysqld4]
server-id=104
datadir=/data/mysqldata/3310
socket=/tmp/mysql.sock3310
port=3310
3.多实例MySQL初始化
多实例MySQL初始化:即分别初始化不同实例,也就是指定不同的datadir
bin/mysqld --initialize --user=mysql --datadir=/data/mysqldata/3307
bin/mysqld --initialize --user=mysql --datadir=/data/mysqldata/3308
bin/mysqld --initialize --user=mysql --datadir=/data/mysqldata/3309
bin/mysqld --initialize --user=mysql --datadir=/data/mysqldata/3310
--创建多实例各自的datadir
[[email protected] mysqldata]# pwd
/data/mysqldata
[[email protected] mysqldata]# mkdir 3307 3308 3309 3310
--具体参照安装单实例的步骤
[[email protected] local]# tar zxvf /public/ISO/mysql/mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
[[email protected] local]# ln -s mysql-5.7.28-linux-glibc2.12-x86_64 mysql
--多实例MySQL初始化
[[email protected] mysql]# pwd
/usr/local/mysql
[[email protected] mysql]# bin/mysqld --initialize --user=mysql --datadir=/data/mysqldata/3307
[[email protected] mysql]# bin/mysqld --initialize --user=mysql --datadir=/data/mysqldata/3308
[[email protected] mysql]# bin/mysqld --initialize --user=mysql --datadir=/data/mysqldata/3309
[[email protected] mysql]# bin/mysqld --initialize --user=mysql --datadir=/data/mysqldata/3310
记录好每个MySQL实例默认生成的密码:
yD*jUO%pj4ta
aHiLky?Ge28k
psPWYuk/)8=j
<8Pk#rxteaqx
修改密码,我这里测试环境为了方便统一设置密码为123,如果是生产环境切记要设计符合客户要求的密码安全策略:
[[email protected] 3307]# mysql -uroot -S/tmp/mysql.sock3307 -p
yD*jUO%pj4ta
mysql> set password='123';
mysql -uroot -S/tmp/mysql.sock3308 -p
mysql -uroot -S/tmp/mysql.sock3309 -p
mysql -uroot -S/tmp/mysql.sock3310 -p
多实例MySQL开机启动项设置:
[[email protected] mysql]# chkconfig --list|grep mysql
[[email protected] mysql]# cp support-files/mysqld_multi.server /etc/init.d/mysqld_multi
[[email protected] mysql]# chkconfig --add mysqld_multi
[[email protected] mysql]# chkconfig --list|grep mysql
mysqld_multi 0:off 1:off 2:on 3:on 4:on 5:on 6:off
4.多实例MySQL常用管理命令
多实例MySQL常用管理命令:
--显示多实例mysqld的运行状态
mysqld_multi report
--启动[mysqld1]
mysqld_multi start 1
--启动[mysqld2]
mysqld_multi start 2
--启动所有多实例mysqld进程
mysqld_multi start
--通过socket连接到指定的MySQL实例
mysql -uroot -S/tmp/mysql.sock3307 -p
mysql -uroot -S/tmp/mysql.sock3308 -p
mysql -uroot -S/tmp/mysql.sock3309 -p
mysql -uroot -S/tmp/mysql.sock3310 -p
--确认连接实例的端口信息
show variables like '%port%';
最后确认下4个MySQL实例的启动、关闭均正常:
[[email protected] mysql]# mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld1 is running
MySQL server from group: mysqld2 is running
MySQL server from group: mysqld3 is running
MySQL server from group: mysqld4 is running
[[email protected] mysql]# ps -ef|grep mysql
root 10923 1 0 23:48 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --server-id=101 --datadir=/data/mysqldata/3307 --socket=/tmp/mysql.sock3307 --port=3307
root 10930 1 0 23:48 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --server-id=102 --datadir=/data/mysqldata/3308 --socket=/tmp/mysql.sock3308 --port=3308
root 10947 1 0 23:48 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --server-id=103 --datadir=/data/mysqldata/3309 --socket=/tmp/mysql.sock3309 --port=3309
root 10963 1 0 23:48 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --server-id=104 --datadir=/data/mysqldata/3310 --socket=/tmp/mysql.sock3310 --port=3310
mysql 11438 10923 0 23:48 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysqldata/3307 --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --server-id=101 --log-error=jystdrac2.err --pid-file=jystdrac2.pid --socket=/tmp/mysql.sock3307 --port=3307
mysql 11461 10947 0 23:48 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysqldata/3309 --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --server-id=103 --log-error=jystdrac2.err --pid-file=jystdrac2.pid --socket=/tmp/mysql.sock3309 --port=3309
mysql 11463 10930 0 23:48 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysqldata/3308 --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --server-id=102 --log-error=jystdrac2.err --pid-file=jystdrac2.pid --socket=/tmp/mysql.sock3308 --port=3308
mysql 11472 10963 0 23:48 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysqldata/3310 --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --server-id=104 --log-error=jystdrac2.err --pid-file=jystdrac2.pid --socket=/tmp/mysql.sock3310 --port=3310
root 12228 7701 0 23:51 pts/0 00:00:00 grep mysql
[[email protected] mysql]# mysqld_multi stop
[[email protected] mysql]# mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld1 is not running
MySQL server from group: mysqld2 is not running
MySQL server from group: mysqld3 is not running
MySQL server from group: mysqld4 is not running
[[email protected] mysql]# ps -ef|grep mysql
root 12400 7701 0 23:52 pts/0 00:00:00 grep mysql
[[email protected] mysql]# mysqld_multi start
[[email protected] mysql]# mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld1 is running
MySQL server from group: mysqld2 is running
MySQL server from group: mysqld3 is running
MySQL server from group: mysqld4 is running
[[email protected] mysql]#