一胃珍、MySQL Fabric高可用集群中一臺(tái)數(shù)據(jù)庫(kù)崩潰了搭盾,不影響數(shù)據(jù)的完整性
1.測(cè)試前準(zhǔn)備
a) 查看group_id-1集群組的服務(wù)器狀態(tài)
mysqlfabric group lookup_servers group_id-1
返回結(jié)果:
Command :
{ success = True
return = [{'status': 'PRIMARY', 'server_uuid': '7a45f71d-7934-11e4-9e8c-782bcb74823a', 'mode': 'READ_WRITE', 'weight': 1.0, 'address': '192.168.1.71:3306'}, {'status': 'SECONDARY', 'server_uuid': '9cf162ca-7934-11e4-9e8d-782bcb1b6b98','mode': 'READ_ONLY', 'weight': 1.0, 'address': '192.168.1.76:3306'}, {'status': 'SECONDARY', 'server_uuid': 'ae94200b-7932-11e4-9e81-a4badb30e16b', 'mode': 'READ_ONLY', 'weight': 1.0, 'address': '192.168.1.70:3306'}]
activities =}
此時(shí)primary機(jī)為機(jī)器:192.168.1.71:3306角骤,group_id-1集群組正常
b) 查看group_id-1集群組中的數(shù)據(jù)
mysql -P 3306 -h 192.168.1.76 -u root -e "select * from test.subscribers"
mysql -P 3306 -h 192.168.1.71 -u root -e "select * from test.subscribers"
mysql -P 3306 -h 192.168.1.70 -u root -e "select * from test.subscribers"
返回結(jié)果如下:
+--------+------------+-----------+
| sub_no | first_name | last_name |
+--------+------------+-----------+
| 500 | Billy | Joel |
| 1500 | Arthur | Askey |
| 5000 | Billy | Fish |
| 17542 | Bobby | Ball |
| 22 | Billy | Bob |
| 8372 | Banana | Man |
| 93846 | Bill | Ben |
| 15050 | John | Smith |
+--------+------------+-----------+
c) 利用python接口查看數(shù)據(jù)庫(kù)test的數(shù)據(jù)表subscribers分片后的全部數(shù)據(jù)
python read_table_ha.py
返回結(jié)果如下:
(u'Billy', u'Bob')
(u'Billy', u'Fish')
(u'Billy', u'Joel')
(u'Arthur', u'Askey')
(u'Banana', u'Man')
(u'Billy', u'Fish')
(u'Bill', u'Ben')
(u'Jimmy', u'White')
(u'John', u'Smith')
(u'Bobby', u'Ball')
2.開(kāi)始測(cè)試
a) 激活故障自動(dòng)切換
即使Fabric選出了master角色荣暮,但當(dāng)這個(gè)master角色宕機(jī)時(shí)颈走,fanric不會(huì)自動(dòng)將secondary角色切換為master角色民轴,所以需要將HA集群組的配置改為自動(dòng)切換角色
學(xué)習(xí)資料分享群:747981058
mysqlfabric group activate group_id-1
b) 停止group_id-1組中的master角色敞峭,即數(shù)據(jù)庫(kù)實(shí)例192.168.1.71:3306踊谋,再查看fabric的狀態(tài)
mysqlfabric group lookup_servers group_id-1
返回結(jié)果:
Command :
{ success = True
return = [{'status': 'FAULTY', 'server_uuid': '7a45f71d-7934-11e4-9e8c-782bcb74823a', 'mode': 'READ_WRITE', 'weight': 1.0, 'address': '192.168.1.71:3306'}, {'status': 'SECONDARY', 'server_uuid': '9cf162ca-7934-11e4-9e8d-782bcb1b6b98', 'mode': 'READ_ONLY', 'weight': 1.0, 'address': '192.168.1.76:3306'}, {'status': 'PRIMARY', 'server_uuid': 'ae94200b-7932-11e4-9e81-a4badb30e16b', 'mode': 'READ_WRITE', 'weight': 1.0, 'address': '192.168.1.70:3306'}]
activities =}
其中,數(shù)據(jù)庫(kù)實(shí)例192.168.1.71:3306的狀態(tài)為FAULTY旋讹,mysql fabric自動(dòng)檢測(cè)到了HA組的故障殖蚕,并自動(dòng)選舉了slave角色為primary角色
c) 查看group_id-1集群組中的數(shù)據(jù)
mysql -P 3306 -h 192.168.1.76 -u root -e "select * from test.subscribers"
mysql -P 3306 -h 192.168.1.70 -u root -e "select * from test.subscribers"
返回結(jié)果:
+--------+------------+-----------+
| sub_no | first_name | last_name |
+--------+------------+-----------+
| 500 | Billy | Joel |
| 1500 | Arthur | Askey |
| 5000 | Billy | Fish |
| 17542 | Bobby | Ball |
| 22 | Billy | Bob |
| 8372 | Banana | Man |
| 93846 | Bill | Ben |
| 15050 | John | Smith |
+--------+------------+-----------+
d) 利用python接口查看數(shù)據(jù)庫(kù)test的數(shù)據(jù)表subscribers分片后的全部數(shù)據(jù)
python read_table_ha.py
返回結(jié)果:
(u'Billy', u'Bob')
(u'Billy', u'Fish')
(u'Billy', u'Joel')
(u'Arthur', u'Askey')
(u'Banana', u'Man')
(u'Billy', u'Fish')
(u'Bill', u'Ben')
(u'Jimmy', u'White')
(u'John', u'Smith')
(u'Bobby', u'Ball')
結(jié)論:由以上測(cè)試返回?cái)?shù)據(jù)表明轿衔,當(dāng)HA組(group_id-1)中的master角色崩潰,mysql? ? ? ? ? ? ? ? ? ? ? ? ? fabric會(huì)自動(dòng)選舉一個(gè)slave角色為master睦疫,并可正常查詢?cè)瓉?lái)的數(shù)據(jù)害驹,因此當(dāng)mysql fabric高可用集群中某一臺(tái)數(shù)據(jù)庫(kù)崩潰掉,并不影響數(shù)據(jù)的完整性
PS:手工恢復(fù)FAULT狀態(tài)的數(shù)據(jù)庫(kù)實(shí)例
mysql fabric高可用集群中的服務(wù)器狀態(tài)有四種:primary蛤育、secondary宛官、faulty、spare
關(guān)閉primary瓦糕,primary不能從組中移除底洗,需要使用mysqlfabric? ? ? ? ? ? ? ? ? ? ? ? ? group demote group_id-1來(lái)關(guān)閉組中的primary,不會(huì)重新選舉一個(gè)新的master咕娄,同時(shí)也不會(huì)關(guān)閉故障檢測(cè)
a) 當(dāng)其中的一個(gè)數(shù)據(jù)庫(kù)實(shí)例崩潰亥揖,可以將這個(gè)數(shù)據(jù)庫(kù)實(shí)例從組中刪除,啟動(dòng)這個(gè)數(shù)據(jù)庫(kù)實(shí)例后再重新添加圣勒,例如:
mysqlfabric group remove group_id-1 7a45f71d-7934-11e4-9e8c-782bcb74823a
mysqlfabric group add group_id-1 192.168.1.71:3306
mysqlfabric group lookup_servers group_id-1
返回結(jié)果:
Command :
{ success = Truereturn = [{'status': 'SECONDARY', 'server_uuid': '7a45f71d-7934-11e4-9e8c-782bcb74823a', 'mode': 'READ_ONLY','weight': 1.0, 'address': '192.168.1.71:3306'}, {'status': 'SECONDARY', 'server_uuid': '9cf162ca-7934-11e4-9e8d-782bcb1b6b98', 'mode': 'READ_ONLY', 'weight': 1.0, 'address': '192.168.1.76:3306'}, {'status': 'PRIMARY', 'server_uuid': 'ae94200b-7932-11e4-9e81-a4badb30e16b','mode': 'READ_WRITE', 'weight': 1.0, 'address': '192.168.1.70:3306'}] activities =}
b) 當(dāng)HA組中全部的服務(wù)器宕機(jī)费变,實(shí)例重啟后,fabric的狀態(tài)不會(huì)自動(dòng)恢復(fù)圣贸,需要先執(zhí)行命令:mysqlfabric? ? ? ? ? ? ? ? ? ? ? ? ? group demote group_id-1挚歧,因?yàn)椴荒苤苯有薷膕erver的狀態(tài)由faulty為secondary,需要將狀態(tài)修改為spare旁趟,在改為secondary昼激,如下:
mysqlfabric group demote group_id-1
mysqlfabric group deactivate group_id-1
mysqlfabric server set_status 7a45f71d-7934-11e4-9e8c-782bcb74823a spare
mysqlfabric server set_status 7a45f71d-7934-11e4-9e8c-782bcb74823a secondary
###當(dāng)所有的數(shù)據(jù)庫(kù)實(shí)例都為secondary狀態(tài)時(shí),可選舉master角色锡搜,命令如下:
mysqlfabric group promote group_id-1
二橙困、MySQL Fabric高可用集群存儲(chǔ)均衡
1.測(cè)試前準(zhǔn)備
a) 測(cè)試腳本
cat test_add_subs_shards.py
import mysql.connector
from mysql.connector import fabric
import math
def add_subscriber(conn, sub_no, first_name, last_name):
conn.set_property(tables=["test.subscribers"], key=sub_no, \
mode=fabric.MODE_READWRITE)
cur = conn.cursor()
cur.execute(
"INSERT INTO subscribers VALUES (%s, %s, %s)",
(sub_no, first_name, last_name)
)
conn = mysql.connector.connect(
fabric={"host" : "localhost", "port" : 32274, "username": "admin", \
"password" : "admin"},
user="root", database="test", password="",
autocommit=True
)
conn.set_property(tables=["test.subscribers"], scope=fabric.SCOPE_LOCAL)
for num in range(10):
add_subscriber(conn, "%s" % num, "k%s" % num, "kw%s" % num)
2.開(kāi)始hash分片測(cè)試
a)插入10條記錄,當(dāng)group_id-1組的三臺(tái)服務(wù)器正常耕餐,group_id-2組只有一臺(tái)服務(wù)器正常運(yùn)作時(shí)凡傅,
group_id-1組的數(shù)據(jù)如下:
b) 測(cè)試插入1萬(wàn)條記錄,group_id-1組和group_id-2組都有三臺(tái)服務(wù)器正常運(yùn)作
分片后肠缔,group_id-1組插入的數(shù)據(jù)行數(shù)如下:
mysql -P 3306 -h 192.168.1.70 -u root -e "select count(*) from test.subscribers"
返回結(jié)果:
+----------+
| count(*) |
+----------+
| 7138 |
+----------+
分片后夏跷,group_id-2組插入的數(shù)據(jù)行數(shù)如下:
mysql -P 3309 -h 192.168.1.76 -u root -e "select count(*) from test.subscribers"
返回結(jié)果:
+----------+
| count(*) |
+----------+
| 2903 |
+----------+
c) 測(cè)試插入10萬(wàn)條記錄,group_id-1組和group_id-2組都有三臺(tái)服務(wù)器正常運(yùn)作
分片后明未,group_id-1組插入的數(shù)據(jù)行數(shù)如下:
mysql -P 3306 -h 192.168.1.70 -u root -e "select count(*) from test.subscribers"
返回結(jié)果:
+----------+
| count(*) |
+----------+
| 78719 |
+----------+
mysql -P 3309 -h 192.168.1.76 -u root -e "select count(*) from test.subscribers"
返回結(jié)果:
+----------+
| count(*) |
+----------+
| 31321 |
+----------+
d) 測(cè)試插入100萬(wàn)條記錄槽华,group_id-1組和group_id-2組都有三臺(tái)服務(wù)器正常運(yùn)作
分片后,group_id-1組插入的數(shù)據(jù)行數(shù)如下:
mysql -P 3306 -h 192.168.1.70 -u root -e "select count(*) from test.subscribers"
返回結(jié)果:
+----------+
| count(*) |
+----------+
| 794287 |
+----------+
分片后趟妥,group_id-2組插入的數(shù)據(jù)行數(shù)如下:
mysql -P 3309 -h 192.168.1.76 -u root -e "select count(*) from test.subscribers"
返回結(jié)果:
+----------+
| count(*) |
+----------+
| 315752 |
+----------+
添加一個(gè)新HA組(group_id-3)猫态,對(duì)HA組(group_id-1)進(jìn)行分片,操作如下:
a) 在機(jī)器:192.168.1.71 192.168.1.76 192.168.1.230上各起一個(gè)數(shù)據(jù)庫(kù)實(shí)例,添加為組group_id-3
mysqlfabric group add group_id-3 192.168.1.71:3313
mysqlfabric group add group_id-3 192.168.1.76:3315
mysqlfabric group add group_id-3 192.168.1.230:3317
選舉primary角色
mysqlfabric group promote group_id-3
b) 重置HA組group_id-3所有的數(shù)據(jù)庫(kù)實(shí)例
mysql -P 3313 -h 192.168.1.71 -u root -e "reset master"
mysql -P 3315 -h 192.168.1.76 -u root -e "reset master"
mysql -P 3317 -h 192.168.1.230 -u root -e "reset master"
c) 查看HA組(group_id-1)的shard_id值亲雪,在儲(chǔ)存機(jī)查看fabric數(shù)據(jù)庫(kù)
mysql -h 127.0.0.1 -P3306 -u root -e 'select * from fabric.shards'
返回結(jié)果:
+----------+------------+---------+
| shard_id | group_id | state |
+----------+------------+---------+
| 4 | group_id-1 | ENABLED |
| 5 | group_id-2 | ENABLED |
+----------+------------+---------+
d) 對(duì)HA組(group_id-1)進(jìn)行分片分離勇凭,將
mysqlfabric sharding split_shard 4 group_id-3
ps:如果這里執(zhí)行分片不成功,錯(cuò)誤如下:
Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'could not find next log;
the first event '' at 4, the last event read from './binlog.000003' at 621,
the last byte read from './binlog.000003' at 621.'
解決方法:
將master角色的數(shù)據(jù)dump出來(lái)义辕,如下:
mysqldump -h 192.168.1.76 -u root -P3315 --all-databases --flush-privileges
--single-transaction --flush-logs --triggers --routines
--events --hex-blob >/data/database/full_backup_master.sql
停掉slave機(jī)
mysql -P 3317 -h 192.168.1.230 -u root -e "stop slave"
mysql -P 3317 -h 192.168.1.230 -u root -e "reset master"
mysql -P 3317 -h 192.168.1.230 -u root
mysql -P 3317 -h 192.168.1.230 -u root -e "start slave"
再次執(zhí)行分片虾标,會(huì)報(bào)以下錯(cuò)誤:
Procedure :
{ uuid = 4da230c2-31c3-4242-bd88-ccafd51bfac1,
finished = True,
success = False,
return = BackupError: ('Error while restoring the backup using the mysql client\n, %s', "ERROR 1840 (HY000) at line 24 in file: 'MySQL_192.168.1.76_3306.sql': @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty.\n"),
activities =}
解決方法:
在HA組(group_id-3)的全部數(shù)據(jù)庫(kù)實(shí)例上執(zhí)行“reset master”,再分片即可
e) 現(xiàn)在HA組有g(shù)roup_id-1灌砖、group_id-2璧函、group_id-3,嘗試插入100條記錄
group_id-1的插入的數(shù)據(jù)行數(shù)有:
mysql -P 3306 -h 192.168.1.70 -u root -e
"select count(*) from test.subscribers where sub_no between 20000 and 20100"
返回結(jié)果:
+----------+
| count(*) |
+----------+
| 70 |
+----------+
group_id-2周崭、group_id-3的插入的數(shù)據(jù)行數(shù)有:
mysql -P 3309 -h 192.168.1.76 -u root -e "select count(*) from test.subscribers where sub_no between 20000 and 20100"
mysql -P 3313 -h 192.168.1.230 -u root -e "select count(*)from test.subscribers where sub_no between 20000 and 20100"
返回結(jié)果:
+----------+
| count(*) |
+----------+
| 30 |
+----------+
用select語(yǔ)句可知柳譬,HA組group_id-2、group_id-3插入的數(shù)據(jù)是一樣的
3.hash分片測(cè)試結(jié)論
在插入1萬(wàn)條記錄時(shí)续镇,HA組(group_id-1)插入了7138條記錄美澳,HA組(group_id-2)插入了2903條記錄
在插入10萬(wàn)條記錄時(shí),HA組(group_id-1)插入了78719條記錄摸航,HA組(group_id-2)插入了31321條記錄
在插入100萬(wàn)條記錄時(shí)制跟,HA組(group_id-1)插入了794287條記錄,HA組(group_id-2)插入了315752條記錄
因此酱虎,對(duì)mysql fabric高集群進(jìn)行hash分片后雨膨,分片比例為7:3,負(fù)載并不是十分均衡
大家推薦一個(gè)學(xué)習(xí)資料分享群:747981058读串,里面大牛已經(jīng)為我們整理好了許多的學(xué)習(xí)資料聊记,有自動(dòng)化,接口恢暖,性能等等的學(xué)習(xí)資料排监!人生是一個(gè)逆水行舟的過(guò)程,不進(jìn)則退杰捂,咱們一起加油吧舆床!