有时候我们需要快速的部署一套集群环境,但是在机器不允许的情况下只能在单机上实现。单机实现就会使用到多个端口了,同时为了保证服务的互不影响就要做文件或数据目录独立分开。本文记录Mysql单机多实例部署方法;
简单、快速在已经安装Mysql的服务器上,添加几个实例:
mysql的安装目录: /usr/local/mysql-5.6.28/
数据存放目录:/data/mysql/
部署多实例前先把数据库停止:
service mysql stop
添加三个实例:分别为mysql-01 、mysql-02、 mysql-03
创建目录:
mkdir -p /data/{mysql-01,mysql-02,mysql-03}
授权:
chwon -R mysql:mysql /data/mysql-0*
复制原来的数据库文件:
cp -rf /data/mysql/mysql/ /data/mysql-01/
cp -rf /data/mysql/mysql/ /data/mysql-02/
cp -rf /data/mysql/mysql/ /data/mysql-03/
原来的my.cnf配置文件备份一下,再修改:
cp /etc/my.cnf /etc/my.cnf.$(date +%F)
修改配置文件:
cat >/etc/my.cnf<<-EOF
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
max-connections=800
[mysqld_multi]
mysqld = /usr/local/mysql-5.6.28/bin/mysqld_safe
mysqladmin = /usr/local/mysql-5.6.28/bin/mysqladmin
log = /usr/local/mysql-5.6.28/mysql_multi.log
[mysqld1]
datadir = /data/mysql-01
socket = /tmp/mysql.sock-01
port = 3306
user = mysql
performance_schema = off
innodb_buffer_pool_size = 32M
bind_address = 0.0.0.0
skip_name_resolve
[mysqld2]
datadir = /data/mysql-02
socket = /tmp/mysql.sock-02
port = 3307
user = mysql
performance_schema = off
innodb_buffer_pool_size = 32M
bind_address = 0.0.0.0
skip_name_resolve
EOF
要注意以上配置中的目录,并保证端口和socket文件不冲突。
[root@lamp-11 ~]# 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
启动实例:可以使用mysqld_multi start 1
一个个启动;
也可以直接:mysqld_multi start
全部一齐启动:
检查效果:
[root@lamp-11 ~]# 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
连接数据库时使用指定的socket文件:
[root@lamp-11 ~]# ll /tmp/mysql.sock*
srwxrwxrwx 1 mysql mysql 0 Dec 26 15:57 /tmp/mysql.sock-01
srwxrwxrwx 1 mysql mysql 0 Dec 26 15:57 /tmp/mysql.sock-02
srwxrwxrwx 1 mysql mysql 0 Dec 26 15:57 /tmp/mysql.sock-03