MySQL Fabric是Oracle官方推出的原生mysql高可用性系统,它可以简化管理MySQL数据库群,它提供两大特性,一是通过故障检测和故障转移功能提供了高可用性,一是通过自动数据分片功能实现可扩展性。本文档只说明自动故障转移-高可用性的实现。
一、测试环境
功能 | IP | 端口 |
Master | 192.168.247.132 | 3306 |
Slave | 192.168.247.133 | 3306 |
Fabric state store | 192.168.247.4 | 3306 |
Fabric 管理进程 | 192.168.247.4 | 32274 |
系统环境要求如下:
MySQL Servers (version 5.6.10 or later)
·Backing store database server
·Application database servers
Python 2.6 or 2.7
·No support for 3.x yet
python连接器1.2.1以上
二、下载Fabric
Fabric目前是被打包到了MySQL Utilities中,所以大家下载MySQL Utilities就可以了,下载地址是:http://dev.mysql.com/downloads/utilities/,目前版本是:1.5.3,在本例中我们下载的是 mysql-utilities-1.5.3-1.el6.noarch.rpm。
Mysql-python连接器下载地址:http://dev.mysql.com/downloads/connector/python/,本例中我们下载mysql-connector-python-2.0.2-1.el6.noarch.rpm
三、安装连接器和MySQL Utilities
rpm包的安装还是挺简单的,具体如下:
rpm -ivh mysql-connector-python-2.0.2-1.el6.noarch.rpm
rpm -ivh mysql-utilities-1.5.3-1.el6.noarch.rpm
装完后执行
mysqlfabric
如果有回显说明安装完毕。
四、建立Backing Store帐号
Backing Store用于存储整个HA集群的服务器等相关配置,它需要一个MySQL实例来存储这些信息,这个实例的版本需要跟其它在HA中的MySQL实例版本保持一致,而且必须是5.6.10及更高的版本,我们在本例中选择192.168.247.4:3306实例来使用。Fabric管理进程和Backing Store实例可以是在两台不同的机器,测试中我们共用192.168.247.4。
创建一个帐号来连接Backing Store的MySQL实例,这个帐号需要有对fabric数据库的管理员级权限,具体如下:
GRANT ALL ON fabric.* TO fabric@'%' IDENTIFIED BY 'fabric@123';
FLUSH PRIVILEGES;
五、Fabric配置文件
Fabric配置文件默认位置是:/etc/mysql/fabric.cfg
修改其中的[storage]部分,具体如下:
[storage]
auth_plugin = mysql_native_password
database = fabric
user = fabric #连接state store实例fabric库的用户名
address = 192.168.247.4:3306 #指定state store的mysql实例地址和端口
connection_delay = 1 #连续尝试连接间隔,单位秒
connection_timeout = 6 #连接超时时间,单位秒
password = fabric@123 #用户密码
connection_attempts = 6 #连接尝试连接的次数
修改其中的[servers]部分,具体如下:
[servers]
password = fabric@123 #连接HA组中实例的用户密码
user = fabric #连接HA组中各实例的用户名
修改其中的[protocol.xmlrpc]部分,具体如下:
[servers]
user = admin #管理账号
address = localhost:32274 #fabric进程的服务器地址及端口号
password = abc123 #管理账号密码
六、填充Backing Store信息
我们通过执行以下命令来生成fabric数据库,具体如下:
mysqlfabric manage setup
[INFO] 1421226952.410291 - MainThread - Initializing persister: user(fabric), server (192.168.247.4:3306), database (fabric).
Finishing initial setup
=======================
Password for admin user is not yet set.
Password for admin/xmlrpc:
Repeat Password:
Password set.
Password set.
No result returned
期间会提示Fabric的管理员帐户admin没有设置密码,按提示设置密码。
连接state store实例可以看到新创建了一个fabfic数据库及相关存储配置信息的表。:
mysql> use fabric;
mysql> show tables;
+-------------------+
| Tables_in_fabric |
+-------------------+
| checkpoints |
| error_log |
| group_replication |
| group_view |
| groups |
| log |
| machines |
| permissions |
| proc_view |
| providers |
| role_permissions |
| roles |
| servers |
| shard_maps |
| shard_ranges |
| shard_tables |
| shards |
| user_roles |
| users |
+-------------------+
七、配置HA中主从MySQL节点帐号
本例中132和133实例是需要做成HA的,它们需要建个管理员权限的帐号,该账号将用于fabric连接HA中的各实例,以及用于replication。注意帐号名也要跟fabric state store实例保持一致,本例中需要是fabric,具体如下:
GRANT ALL ON *.* TO fabric@'192.168.247.%' IDENTIFIED BY 'fabric@123';
FLUSH PRIVILEGES;
同时,由于fabric是基于GTID主从复制,所以这些实例中必须要启用GTID,它们的配置文件要有这些参数:
log-bin
gtid-mode=ON
enforce-gtid-consistency
log_slave_updates
八、启动fabric
我们用下面的命令来启动fabric:
mysqlfabric manage start
[root@localhost Desktop]# mysqlfabric manage start &
[1] 8131
[root@localhost Desktop]# [INFO] 1421227750.407190 - MainThread - Initializing persister: user (fabric), server (192.168.247.4:3306), database (fabric).
[WARNING] 1421227750.428230 - MainThread - Provider error: No module named novaclient.
[INFO] 1421227750.428522 - MainThread - Loading Services.
[INFO] 1421227750.447799 - MainThread - MySQL-RPC protocol server started, listening on localhost:32275
[INFO] 1421227750.467301 - MainThread - Fabric node starting.
[INFO] 1421227750.470832 - MainThread - Starting Executor.
[INFO] 1421227750.470945 - MainThread - Setting 5 executor(s).
[INFO] 1421227750.471722 - Executor-0 - Started.
[INFO] 1421227750.474404 - Executor-1 - Started.
[INFO] 1421227750.477835 - Executor-2 - Started.
[INFO] 1421227750.480265 - Executor-3 - Started.
[INFO] 1421227750.482358 - MainThread - Executor started.
[INFO] 1421227750.485552 - Executor-4 - Started.
[INFO] 1421227750.527671 - MainThread - Starting failure detector.
[INFO] 1421227750.541374 - XML-RPC-Server - XML-RPC protocol server ('127.0.0.1', 32274) started.
[INFO] 1421227750.541887 - XML-RPC-Server - Setting 1 XML-RPC session(s).
[INFO] 1421227750.542572 - XML-RPC-Session-0 - Started XML-RPC-Session.
九、建立HA服务器组
这个HA服务器组,用于把参与HA的所有MySQL实例都填加进来:
mysqlfabric group create mygroup
Password for admin:
Fabric UUID: 5ca1ab1e-a007-feed-f00d-cab3fe13249e
Time-To-Live: 1
uuid finished success result
------------------------------------ -------- ------- ------
612e53c8-87b7-4e97-b714-ffbe38a977e0 1 1 1
这样我们就创建了一个组名为mygroup的HA服务器组。
十、填加HA组的成员
我们首先填加192.168.247.132:3306,具体如下:
mysqlfabric group add mygroup 192.168.247.132:3306
Password for admin:
Fabric UUID: 5ca1ab1e-a007-feed-f00d-cab3fe13249e
Time-To-Live: 1
uuid finished success result
------------------------------------ -------- ------- ------
a918fe34-2acd-4868-9689-1080be3e9b69 1 1 1
接下来填192.168.247.133:3306,具体如下:
mysqlfabric group add mygroup 192.168.247.133:3306
Password for admin:
Fabric UUID: 5ca1ab1e-a007-feed-f00d-cab3fe13249e
Time-To-Live: 1
uuid finished success result
------------------------------------ -------- ------- ------
97c06a94-10ad-4897-b40e-5a3e9e56bfa2 1 1 1
如果屏幕回显示无error,那么说明成功填加了成员。我们也可以查看一下mygroup里面的成员信息,具体如下:
mysqlfabric group lookup_servers mygroup
Password for admin:
Fabric UUID: 5ca1ab1e-a007-feed-f00d-cab3fe13249e
Time-To-Live: 1
server_uuid address status mode weight
------------------------------------ -------------------- --------- --------- ------
032bb536-73af-11e4-ba8b-000c2978e187 192.168.247.132:3306 SECONDARY READ_ONLY 1.0
c505ce10-9bc0-11e4-bfd4-000c290e7abe 192.168.247.133:3306 SECONDARY READ_ONLY 1.0
可以看到有2个实例,但这2个实例都不是PRIMARY,因为刚添加组成员,是不会自动选举出PRIMARY的。
十一、选举一个主库
选举的方法也非常简单,具体如下:
mysqlfabric group promote mygroup
Password for admin:
Fabric UUID: 5ca1ab1e-a007-feed-f00d-cab3fe13249e
Time-To-Live: 1
uuid finished success result
------------------------------------ -------- ------- ------
84f51361-83c5-466d-97ae-b6e9e497a4bf 1 1 1
执行成功了,没有返回error。
我们再次查看一下HA集群中各服务器情况:
mysqlfabric group lookup_servers mygroup
Password for admin:
Fabric UUID: 5ca1ab1e-a007-feed-f00d-cab3fe13249e
Time-To-Live: 1
server_uuid address status mode weight
------------------------------------ -------------------- --------- ---------- ------
032bb536-73af-11e4-ba8b-000c2978e187 192.168.247.132:3306 PRIMARY READ_WRITE 1.0
c505ce10-9bc0-11e4-bfd4-000c290e7abe 192.168.247.133:3306 SECONDARY READ_ONLY 1.0
可见192.168.247.132:3306已经被随机选举成为Master角色。
十二、激活故障自动切换
即使Fabric选出了Master角色,但当这个Master宕机时,Fabric并不会自动将Secondary角色切换成Master角色,所以我们需要将HA配置成可以自动切换角色的样子,具体如下:
mysqlfabric group activate mygroup
Password for admin:
Fabric UUID: 5ca1ab1e-a007-feed-f00d-cab3fe13249e
Time-To-Live: 1
uuid finished success result
------------------------------------ -------- ------- ------
9422543d-6847-4242-ba8b-b737544c440 1 1 1
当然,我们也可以使用deactivate命令需求取消Fabric故障自动切换。
十三、测试HA
在这个实验中,我们将192.168.247.132:3306实例停止,再看看Fabric的状态:
mysqlfabric group lookup_servers mygroup
Password for admin:
Fabric UUID: 5ca1ab1e-a007-feed-f00d-cab3fe13249e
Time-To-Live: 1
server_uuid address status mode weight
------------------------------------ -------------------- ------- ---------- ------
032bb536-73af-11e4-ba8b-000c2978e187 192.168.247.132:3306 FAULTY READ_WRITE 1.0
c505ce10-9bc0-11e4-bfd4-000c290e7abe 192.168.247.133:3306 PRIMARY READ_WRITE 1.0
其中192.168.247.132:3306实例的状态已经变成了“FAULTY”,可以看出Fabric自动检测到了这个故障,并且选举了slave重新当了primary角色。
重新启动192.168.247.132:3306再查看HA组状态:
mysqlfabric group lookup_servers mygroup
Fabric UUID: 5ca1ab1e-a007-feed-f00d-cab3fe13249e
Time-To-Live: 1
server_uuid address status mode weight
------------------------------------ -------------------- ------- ---------- ------
032bb536-73af-11e4-ba8b-000c2978e187 192.168.247.132:3306 FAULTY READ_WRITE 1.0
c505ce10-9bc0-11e4-bfd4-000c290e7abe 192.168.247.133:3306 PRIMARY READ_WRITE 1.0
可以看到即使132实例已恢复正常,但它在HA组中的状态不会自动恢复,需要手动改变服务器状态,支持的状态值为primary,secondary,spare,faulty。
mysqlfabric server set_status c505ce10-9bc0-11e4-bfd4-000c290e7abe secondary
Fabric UUID: 5ca1ab1e-a007-feed-f00d-cab3fe13249e
Time-To-Live: 1
ServerError: Cannot change server's (032bb536-73af-11e4-ba8b-000c2978e187) status from (FAULTY) to (SECONDARY).
我们要把132实例变更成secondary状态,由上可见,从FAULTY状态不允许直接变更变SECONDARY状态,必须先变更成spare状态(避免写和读访问,同时保证当故障切换时不会被选取),然后再变更成secondary。
mysqlfabric server set_status 032bb536-73af-11e4-ba8b-000c2978e187 spare
Fabric UUID: 5ca1ab1e-a007-feed-f00d-cab3fe13249e
Time-To-Live: 1
uuid finished success result
------------------------------------ -------- ------- ------
c66afff7-cb9a-4064-a402-10e0a05b3be2 1 1 1
查看状态,132实例已成功更改为spare状态
mysqlfabric group lookup_servers mygroupFabric UUID: 5ca1ab1e-a007-feed-f00d-cab3fe13249e
Time-To-Live: 1
server_uuid address status mode weight
------------------------------------ -------------------- ------- ---------- ------
032bb536-73af-11e4-ba8b-000c2978e187 192.168.247.132:3306 SPARE OFFLINE 1.0
c505ce10-9bc0-11e4-bfd4-000c290e7abe 192.168.247.133:3306 PRIMARY READ_WRITE 1.0
再变更为secondary状态:
mysqlfabric server set_status 032bb536-73af-11e4-ba8b-000c2978e187 secondary
Fabric UUID: 5ca1ab1e-a007-feed-f00d-cab3fe13249e
Time-To-Live: 1
uuid finished success result
------------------------------------ -------- ------- ------
530ad1ce-ba74-4c3d-96a9-9edeb497d5d2 1 1 1
查看状态,132实例已成功变为secondary状态:
mysqlfabric group lookup_servers mygroupFabric UUID: 5ca1ab1e-a007-feed-f00d-cab3fe13249e
Time-To-Live: 1
server_uuid address status mode weight
------------------------------------ -------------------- --------- ---------- ------
032bb536-73af-11e4-ba8b-000c2978e187 192.168.247.132:3306 SECONDARY READ_ONLY 1.0
c505ce10-9bc0-11e4-bfd4-000c290e7abe 192.168.247.133:3306 PRIMARY READ_WRITE 1.0
十四、常用相关命令
mysqlfabricgroup create mygroup #创建HA组
mysqlfabricgroup destroy mygroup #删除HA组
mysqlfabricgroup add mygroup 192.168.247.132:3306 #添加组成员
mysqlfabricgroup remove mygroup c505ce10-9bc0-11e4-bfd4-000c290e7abe #移出组成员
mysqlfabricgroup lookup_servers mygroup #查看组成员
mysqlfabricgroup promote mygroup #选举master
mysqlfabric groupactivate mygroup #激活自动故障转移
mysqlfabric group deactivatemygroup #禁用自动故障转移
mysqlfabric serverset_status server_uuid status #变更服务器状态
mysqlfabrichelp manage #manage命令帮助
mysqlfabrichelp group #group命令帮助
mysqlfabrichelp server #server命令帮助
总结:
1. 程序必须通过Fabric-aware connectors来实现路由,目前只支持PHP,Python 和Java;
2. mysql实例进行恢复后,fabric不会自动改变实例的状态,需要手工修改;
3. fabric state store和fabric note当前是单点,mysqlfabric进程本身不提供容错,出现故障时需要重启,当mysql fabric不可用时,fabric-aware连接器能够通过本地的cache继续路由操作(配置ttl值);
4. 基于GTID的复制本身存在一些限制。
附两个测试脚本:
1.创建表测试脚本:
#test.py
import sys
import mysql.connector
from mysql.connector import fabric
def main():
try:
conn = mysql.connector.connect(
fabric ={"host":"localhost","port":32274,"username":"admin",
"password":"abc123"},
user = 'root',database = 'test',password = 'abc123',autocommit =True
)
except Exception,e:
print e
sys.exit(1)
else:
conn.set_property(mode=fabric.MODE_READWRITE,group="mygroup")
cur = conn.cursor()
cur.execute(
"CREATE TABLE IF NOT EXISTS t1 ("
" id INT,"
" first_name varchar(40),"
" last_name varchar(40)"
")"
)
finally:
conn.close()
if __name__ == '__main__':
main()
2.插入数据测试脚本:
#test2.py
import sys
import mysql.connector
from mysql.connector import fabric
defadd_subscriber(conn,id,first_name,last_name):
conn.set_property(group="mygroup",mode=fabric.MODE_READWRITE)
cur = conn.cursor()
cur.execute(
"INSERT INTO t1 VALUES (%s,%s,%s)",
(id,first_name,last_name)
)
def main():
try:
conn = mysql.connector.connect(
fabric ={"host":"localhost","port":32274,"username":"admin",
"password":"abc123"},
user="root",database="test",password="abc123",autocommit=True
)
except Exception,e:
print e
sys.exit(1)
else:
#conn.set_property(group="mygroup",mode=fabric.MODE_READWRITE)
add_subscriber(conn, 1, "Tom", "Jerry")
add_subscriber(conn, 2, "Jack", "Tom")
add_subscriber(conn, 3, "Lily", "Lee")
finally:
conn.close()
if __name__ == '__main__':
main()