主机名 |
内部网络IP |
说明 |
fab_connector |
192.168.56.101 |
安装Fabric和MySQL,建立一个MySQL实例,使用缺省的3306端口,存储MySQL实例的状态和路由信息 |
fab1 |
192.168.56.102 |
安装MySQL,建立三个MySQL数据库实例,端口分别是3326、3327、3328 |
fab2 |
192.168.56.103 |
安装MySQL,建立三个MySQL数据库实例,端口分别是3326、3327、3328 |
fab3 |
192.168.56.104 |
安装MySQL,建立三个MySQL数据库实例,端口分别是3326、3327、3328 |
虚拟机名称 |
网卡 |
连接方式 |
说明 |
fab_connector |
网卡1 |
网络地址转换(NAT) |
用于虚拟机访问宿主机和外网 |
网卡2 |
桥接网卡(192.168.1.119) |
用于宿主机访问虚拟机 |
|
网卡3 |
内部网络 |
用于Fabric组内互联 |
|
fab1 |
网卡1 |
网络地址转换(NAT) |
用于虚拟机访问宿主机和外网 |
网卡2 |
内部网络 |
用于Fabric组内互联 |
|
fab2 |
网卡1 |
网络地址转换(NAT) |
用于虚拟机访问宿主机和外网 |
网卡2 |
内部网络 |
用于Fabric组内互联 |
|
fab3 |
网卡1 |
网络地址转换(NAT) |
用于虚拟机访问宿主机和外网 |
网卡2 |
内部网络 |
用于Fabric组内互联 |
cd /root
tar xvf mysql-5.7.10-linux-glibc2.5-x86_64.tar
tar zxvf mysql-5.7.10-linux-glibc2.5-x86_64.tar.gz
ln -s mysql-5.6.13-linux-glibc2.5-x86_64 mysql
groupadd mysql
useradd -r -g mysql mysql
chown -R mysql .
cd /root
tar zxvf mysql-utilities-1.5.6.tar.gz
cd mysql-utilities-1.5.6
sudo python setup.py install
[root@fab_connector ~]# cat /etc/my_fabric_init.cnf
[mysqld]
basedir=/root/mysql
datadir=/var/lib/mysql
port=3306
socket=/var/lib/mysql/mysql.sock
[root@fab_connector ~]# cat /etc/my_fabric.cnf
[mysqld]
basedir=/root/mysql
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
binlog-format=ROW
log-slave-updates=true
gtid-mode=on
enforce-gtid-consistency=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
port=3306
report-host=fab_connector
report-port=3306
server-id=1
log-bin=fab-bin.log
[root@fab_connector ~]# cat /etc/mysql/fabric.cfg
[DEFAULT]
prefix =
sysconfdir = /etc
logdir = /var/log
[statistics]
prune_time = 3600
[logging]
url = file:///var/log/fabric.log
level = INFO
[storage]
auth_plugin = mysql_native_password
database = fabric
user = fabric
address = localhost:3306
connection_delay = 1
connection_timeout = 6
password = secret
connection_attempts = 6
[failure_tracking]
notification_interval = 60
notification_clients = 50
detection_timeout = 1
detection_interval = 6
notifications = 300
detections = 3
failover_interval = 0
prune_time = 3600
[servers]
restore_user = fabric
unreachable_timeout = 5
backup_password = secret
backup_user = fabric
user = fabric
restore_password = secret
password = secret
[connector]
ttl = 1
[protocol.xmlrpc]
disable_authentication = no
ssl_cert =
realm = MySQL Fabric
ssl_key =
ssl_ca =
threads = 5
user = admin
address = 192.168.1.119:32274
password = secret
[executor]
executors = 5
[sharding]
prune_limit = 10000
mysqldump_program = /root/mysql/bin/mysqldump
mysqlclient_program = /root/mysql/bin/mysql
[protocol.mysql]
disable_authentication = no
ssl_cert =
ssl_key =
ssl_ca =
user = admin
address = 192.168.1.119:32275
password = secret
[root@fab_global_group ~]# cat /etc/my_group_1_init.cnf
[mysqld]
basedir=/root/mysql
datadir=/var/lib/group_1
port=3326
socket=/var/lib/group_1/mysql.sock
plugin_dir=/root/mysql-5.7.10-linux-glibc2.5-x86_64/lib/plugin
[root@fab_global_group ~]# cat /etc/my_group_2_init.cnf
[mysqld]
basedir=/root/mysql
datadir=/var/lib/group_2
port=3327
socket=/var/lib/group_2/mysql.sock
plugin_dir=/root/mysql-5.7.10-linux-glibc2.5-x86_64/lib/plugin
[root@fab_global_group ~]# cat /etc/my_group_3_init.cnf
[mysqld]
basedir=/root/mysql
datadir=/var/lib/group_3
port=3328
socket=/var/lib/group_3/mysql.sock
plugin_dir=/root/mysql-5.7.10-linux-glibc2.5-x86_64/lib/plugin
[root@fab_global_group ~]# cat /etc/my_group_1.cnf
[mysqld]
basedir=/root/mysql
datadir=/var/lib/group_1
port=3326
socket=/var/lib/group_1/mysql.sock
binlog-format=ROW
log-slave-updates=true
gtid-mode=on
enforce-gtid-consistency=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
report-host=fab_global_group
report-port=3326
server-id=11
log-bin=fab1a-bin.log
rpl_semi_sync_slave_enabled=1
rpl_semi_sync_master_enabled = 1
rpl_semi_sync_master_timeout = 1000
[root@fab_global_group ~]# cat /etc/my_group_2.cnf
[mysqld]
basedir=/root/mysql
datadir=/var/lib/group_2
port=3327
socket=/var/lib/group_2/mysql.sock
binlog-format=ROW
log-slave-updates=true
gtid-mode=on
enforce-gtid-consistency=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
report-host=fab_global_group
report-port=3327
server-id=12
log-bin=fab1a-bin.log
rpl_semi_sync_slave_enabled=1
rpl_semi_sync_master_enabled = 1
rpl_semi_sync_master_timeout = 1000
[root@fab_global_group ~]# cat /etc/my_group_3.cnf
[mysqld]
basedir=/root/mysql
datadir=/var/lib/group_3
port=3328
socket=/var/lib/group_3/mysql.sock
binlog-format=ROW
log-slave-updates=true
gtid-mode=on
enforce-gtid-consistency=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
report-host=fab_global_group
report-port=3328
server-id=13
log-bin=fab1a-bin.log
rpl_semi_sync_slave_enabled=1
rpl_semi_sync_master_enabled = 1
rpl_semi_sync_master_timeout = 1000
[root@fab_group1 ~]# cat /etc/my_group_1_init.cnf
[mysqld]
basedir=/root/mysql
datadir=/var/lib/group_1
port=3326
socket=/var/lib/group_1/mysql.sock
plugin_dir=/root/mysql-5.7.10-linux-glibc2.5-x86_64/lib/plugin
[root@fab_group1 ~]# cat /etc/my_group_2_init.cnf
[mysqld]
basedir=/root/mysql
datadir=/var/lib/group_2
port=3327
socket=/var/lib/group_2/mysql.sock
plugin_dir=/root/mysql-5.7.10-linux-glibc2.5-x86_64/lib/plugin
[root@fab_group1 ~]# cat /etc/my_group_3_init.cnf
[mysqld]
basedir=/root/mysql
datadir=/var/lib/group_3
port=3328
socket=/var/lib/group_3/mysql.sock
plugin_dir=/root/mysql-5.7.10-linux-glibc2.5-x86_64/lib/plugin
[root@fab_group1 ~]# cat /etc/my_group_1.cnf
[mysqld]
basedir=/root/mysql
datadir=/var/lib/group_1
port=3326
socket=/var/lib/group_1/mysql.sock
binlog-format=ROW
log-slave-updates=true
gtid-mode=on
enforce-gtid-consistency=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
report-host=fab_group1
report-port=3326
server-id=21
log-bin=fab1a-bin.log
rpl_semi_sync_slave_enabled=1
rpl_semi_sync_master_enabled = 1
rpl_semi_sync_master_timeout = 1000
[root@fab_group1 ~]# cat /etc/my_group_2.cnf
[mysqld]
basedir=/root/mysql
datadir=/var/lib/group_2
port=3327
socket=/var/lib/group_2/mysql.sock
binlog-format=ROW
log-slave-updates=true
gtid-mode=on
enforce-gtid-consistency=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
report-host=fab_group1
report-port=3327
server-id=22
log-bin=fab1a-bin.log
rpl_semi_sync_slave_enabled=1
rpl_semi_sync_master_enabled = 1
rpl_semi_sync_master_timeout = 1000
[root@fab_group1 ~]# cat /etc/my_group_3.cnf
[mysqld]
basedir=/root/mysql
datadir=/var/lib/group_3
port=3328
socket=/var/lib/group_3/mysql.sock
binlog-format=ROW
log-slave-updates=true
gtid-mode=on
enforce-gtid-consistency=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
report-host=fab_group1
report-port=3328
server-id=23
log-bin=fab1a-bin.log
rpl_semi_sync_slave_enabled=1
rpl_semi_sync_master_enabled = 1
rpl_semi_sync_master_timeout = 1000
[root@fab_group2 ~]# cat /etc/my_group_1_init.cnf
[mysqld]
basedir=/root/mysql
datadir=/var/lib/group_1
port=3326
socket=/var/lib/group_1/mysql.sock
plugin_dir=/root/mysql-5.7.10-linux-glibc2.5-x86_64/lib/plugin
[root@fab_group2 ~]# cat /etc/my_group_2_init.cnf
[mysqld]
basedir=/root/mysql
datadir=/var/lib/group_2
port=3327
socket=/var/lib/group_2/mysql.sock
plugin_dir=/root/mysql-5.7.10-linux-glibc2.5-x86_64/lib/plugin
[root@fab_group2 ~]# cat /etc/my_group_3_init.cnf
[mysqld]
basedir=/root/mysql
datadir=/var/lib/group_3
port=3328
socket=/var/lib/group_3/mysql.sock
plugin_dir=/root/mysql-5.7.10-linux-glibc2.5-x86_64/lib/plugin
[root@fab_group2 ~]# cat /etc/my_group_1.cnf
[mysqld]
basedir=/root/mysql
datadir=/var/lib/group_1
port=3326
socket=/var/lib/group_1/mysql.sock
binlog-format=ROW
log-slave-updates=true
gtid-mode=on
enforce-gtid-consistency=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
report-host=fab_group2
report-port=3326
server-id=31
log-bin=fab1a-bin.log
rpl_semi_sync_slave_enabled=1
rpl_semi_sync_master_enabled = 1
rpl_semi_sync_master_timeout = 1000
[root@fab_group2 ~]# cat /etc/my_group_2.cnf
[mysqld]
basedir=/root/mysql
datadir=/var/lib/group_2
port=3327
socket=/var/lib/group_2/mysql.sock
binlog-format=ROW
log-slave-updates=true
gtid-mode=on
enforce-gtid-consistency=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
report-host=fab_group2
report-port=3327
server-id=32
log-bin=fab1a-bin.log
rpl_semi_sync_slave_enabled=1
rpl_semi_sync_master_enabled = 1
rpl_semi_sync_master_timeout = 1000
[root@fab_group2 ~]# cat /etc/my_group_3.cnf
[mysqld]
basedir=/root/mysql
datadir=/var/lib/group_3
port=3328
socket=/var/lib/group_3/mysql.sock
binlog-format=ROW
log-slave-updates=true
gtid-mode=on
enforce-gtid-consistency=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
report-host=fab_group2
report-port=3328
server-id=33
log-bin=fab1a-bin.log
rpl_semi_sync_slave_enabled=1
rpl_semi_sync_master_enabled = 1
rpl_semi_sync_master_timeout = 1000
mysqld --defaults-file=/etc/my_fabric_init.cnf --initialize
# 记下初始化生成的临时密码
chown -R mysql /var/lib/mysql
mysqld --defaults-file=/etc/my_fabric_init.cnf --user=mysql &
mysql -u root -p -h127.0.0.1
# 修改初始密码,添加fabric用户
ALTER USER USER() IDENTIFIED BY 'new_password';
CREATE USER 'fabric'@'localhost' IDENTIFIED BY 'secret'
GRANT ALL ON fabric.* TO 'fabric'@'localhost'";
# 重启MySQL
mysqladmin -u root --protocol=tcp -h127.0.0.1 -p shutdown
mysqld --defaults-file=/etc/my_fabric.cnf --user=mysql &
mkdir /var/lib/group_1
mkdir /var/lib/group_2
mkdir /var/lib/group_3
mysqld --defaults-file=/etc/my_group_1_init.cnf --initialize
mysqld --defaults-file=/etc/my_group_2_init.cnf --initialize
mysqld --defaults-file=/etc/my_group_3_init.cnf --initialize
# 记下初始化生成的临时密码
chown -R mysql /var/lib/group_1
chown -R mysql /var/lib/group_2
chown -R mysql /var/lib/group_3
# 修改初始密码,添加fabric用户
mysqld --defaults-file=/etc/my_group_1_init.cnf --user=mysql &
mysql -u root -p -h127.0.0.1 -P3326
ALTER USER USER() IDENTIFIED BY 'new_password';
CREATE USER 'fabric'@'%' IDENTIFIED BY 'secret';
GRANT ALL ON *.* TO 'fabric'@'%';
INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
mysqld --defaults-file=/etc/my_group_2_init.cnf --user=mysql &
mysql -u root -p -h127.0.0.1 -P3327
ALTER USER USER() IDENTIFIED BY 'new_password';
CREATE USER 'fabric'@'%' IDENTIFIED BY 'secret';
GRANT ALL ON *.* TO 'fabric'@'%';
INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
mysqld --defaults-file=/etc/my_group_3_init.cnf --user=mysql &
mysql -u root -p -h127.0.0.1 -P3328
ALTER USER USER() IDENTIFIED BY 'new_password';
CREATE USER 'fabric'@'%' IDENTIFIED BY 'secret';
GRANT ALL ON *.* TO 'fabric'@'%';
INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
# 重启3个MySQL实例
mysqladmin -u root --protocol=tcp -h127.0.0.1 -P3326 -p shutdown
mysqladmin -u root --protocol=tcp -h127.0.0.1 -P3327 -p shutdown
mysqladmin -u root --protocol=tcp -h127.0.0.1 -P3328 -p shutdown
mysqld --defaults-file=/etc/my_group_1.cnf --user=mysql &
mysqld --defaults-file=/etc/my_group_2.cnf --user=mysql &
mysqld --defaults-file=/etc/my_group_3.cnf --user=mysql &
# 查看3个MySQL实例进程
ps -ef | grep mysql
# 在状态存储(MySQL数据库实例)中建立Fabric库
mysqlfabric manage setup
# 以守护进程方式启动Fabric
mysqlfabric manage start --daemonize
# 检查fabric进程是否运行
mysqlfabric manage ping
# 建立HA组
mysqlfabric group create global-group
mysqlfabric group create my_group1
mysqlfabric group create my_group2
# 在HA组中添加服务器
mysqlfabric group add global-group 192.168.56.102:3326
mysqlfabric group add global-group 192.168.56.103:3326
mysqlfabric group add global-group 192.168.56.104:3326
mysqlfabric group add my_group1 192.168.56.102:3327
mysqlfabric group add my_group1 192.168.56.103:3327
mysqlfabric group add my_group1 192.168.56.104:3327
mysqlfabric group add my_group2 192.168.56.102:3328
mysqlfabric group add my_group2 192.168.56.103:3328
mysqlfabric group add my_group2 192.168.56.104:3328
# 提升主服务器
mysqlfabric group promote global-group --slave_id=192.168.56.102:3326
mysqlfabric group promote my_group1 --slave_id=192.168.56.103:3327
mysqlfabric group promote my_group2 --slave_id=192.168.56.104:3328
# 至此HA特性配置完毕,使用下面的命令检查Fabric配置运行情况
# 查看HA组
mysqlfabric group lookup_groups
# 查看HA组中的服务器
mysqlfabric group lookup_servers global-group
mysqlfabric group lookup_servers my_group1
mysqlfabric group lookup_servers my_group2
# 查看组的健康状况
mysqlfabric group health global-group
mysqlfabric group health my_group1
mysqlfabric group health my_group2
# 定义分片映射方法
mysqlfabric sharding create_definition HASH global-group
# 添加分片表及其字段
mysqlfabric sharding add_table 1 test.chat_message src_userid
# 添加分片
mysqlfabric sharding add_shard 1 "my_group1,my_group2" --state=enabled
# 至此Sharding特性配置完毕,使用下面的S命令检查Sharding配置情况
mysql -u root -p -h127.0.0.1 -e "select * from fabric.shard_maps"
mysql -u root -p -h127.0.0.1 -e "select * from fabric.shard_ranges"
mysql -u root -p -h127.0.0.1 -e "select * from fabric.shard_tables"
mysql -u root -p -h127.0.0.1 -e "select * from fabric.shards"
mysql -u root -p -h127.0.0.1 -P3326
create database test;
use test;
CREATE TABLE chat_message (
src_userid bigint(20) NOT NULL PRIMARY KEY
);
执行完后在fab1、fab2、fab3的9个实例中都已经建好了测试库表。
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.apache.commons.lang3.RandomStringUtils;
import com.mysql.fabric.jdbc.FabricMySQLConnection;
public class Main {
private static String URL = "jdbc:mysql:fabric://192.168.1.119:32274/test?fabricShardTable=chat_message&fabricUsername=admin&fabri
cPassword=secret";
private static String USERNAME = "fabric";
private static String PWD = "secret";
private static int MAX = 20;
private static String SQL = "insert into chat_message(src_userid) values(?)";
public static void main(String[] args) throws ClassNotFoundException, SQLException, UnsupportedEncodingException {
long start = System.currentTimeMillis();
testInsert();
long end = System.currentTimeMillis();
System.out.println((end - start));
System.out.println(MAX / ((end - start) / 1000));
}
private static Connection getConnection() throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.fabric.jdbc.FabricMySQLDriver");
Connection con = DriverManager.getConnection(URL,USERNAME,PWD);
return con;
}
private static void testInsert() throws ClassNotFoundException, SQLException {
int i = 0;
Connection con = getConnection();
con.setAutoCommit(false);
while (i < MAX) {
FabricMySQLConnection fcon=(FabricMySQLConnection) con;
fcon.setShardKey(String.valueOf(i));
PreparedStatement pt = con.prepareStatement(SQL);
pt.setLong(1, i);
pt.executeUpdate();
fcon.commit();
i++;
//fcon.close();
System.out.println(i);
}
con.close();
}
}
13. 在fab2、fab3两个虚拟机上新各建1个MySQL实例,端口为3329,分别用于测试remove_shard、split_shard
(1)准备配置文件
# 在fab2上准备如下2个配置文件
# cat /etc/my_group_4_init.cnf
[mysqld]
basedir=/root/mysql
datadir=/var/lib/group_4
port=3329
socket=/var/lib/group_4/mysql.sock
plugin_dir=/root/mysql-5.7.10-linux-glibc2.5-x86_64/lib/plugin
# cat /etc/my_group_4.cnf
[mysqld]
basedir=/root/mysql
datadir=/var/lib/group_4
port=3329
socket=/var/lib/group_4/mysql.sock
binlog-format=ROW
log-slave-updates=true
gtid-mode=on
enforce-gtid-consistency=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
report-host=fab_group1
report-port=3329
server-id=24
log-bin=fab1a-bin.log
rpl_semi_sync_slave_enabled=1
rpl_semi_sync_master_enabled = 1
rpl_semi_sync_master_timeout = 1000
# 在fab3上准备如下2个配置文件
# cat /etc/my_group_4_init.cnf
[mysqld]
basedir=/root/mysql
datadir=/var/lib/group_4
port=3329
socket=/var/lib/group_4/mysql.sock
plugin_dir=/root/mysql-5.7.10-linux-glibc2.5-x86_64/lib/plugin
# cat /etc/my_group_4.cnf
[mysqld]
basedir=/root/mysql
datadir=/var/lib/group_4
port=3329
socket=/var/lib/group_4/mysql.sock
binlog-format=ROW
log-slave-updates=true
gtid-mode=on
enforce-gtid-consistency=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
report-host=fab_group2
report-port=3329
server-id=34
log-bin=fab1a-bin.log
rpl_semi_sync_slave_enabled=1
rpl_semi_sync_master_enabled = 1
rpl_semi_sync_master_timeout = 1000
(2)启动实例(下面的命令在两个虚拟机上都要执行)
mkdir /var/lib/group_4
mysqld --defaults-file=/etc/my_group_4_init.cnf --initialize
# 记下初始化生成的临时密码
chown -R mysql /var/lib/group_4
# 修改初始密码,添加fabric用户
mysqld --defaults-file=/etc/my_group_4_init.cnf --user=mysql &
mysql -u root -p -h127.0.0.1 -P3329
ALTER USER USER() IDENTIFIED BY 'new_password';
CREATE USER 'fabric'@'%' IDENTIFIED BY 'secret';
GRANT ALL ON *.* TO 'fabric'@'%';
INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
# 重启MySQL实例
mysqladmin -u root --protocol=tcp -h127.0.0.1 -P3329 -p shutdown
mysqld --defaults-file=/etc/my_group_4.cnf --user=mysql &
# 查看MySQL实例进程
ps -ef | grep mysql
14. 在fab_connector上配置HA
# 建立HA组
mysqlfabric group create my_group3
mysqlfabric group create my_group4
# 在HA组中添加服务器
mysqlfabric group add my_group3 192.168.56.103:3329
mysqlfabric group add my_group4 192.168.56.104:3329
# 提升主服务器
mysqlfabric group promote my_group3
mysqlfabric group promote my_group4
15. 测试move_shard
# 把分片1从my_group1移动到my_group3
mysqlfabric sharding move_shard 1 my_group3
执行前后的分片定义如下图所示。
# 把分片2分到my_group4组中
mysqlfabric sharding split_shard 2 my_group4
执行后的分片定义如下图所示。
参考:
MySQL Fabric官方文档