Fabric結合GTID實現Mysql複製高可用

mysql版本:Percona server 5.6.19-67.0 Percona Server (GPL), Release 67.0, Revision 618


Fabric State store安裝參考: http://my.oschina.net/anthonyyau/blog/307165

環境:
4個服務器實例運行Percona server 5.6.19-67.0,fabric node安裝在單獨的服務器,每一個Mysql實例在一臺服務器上;
fabric爲啓用複製的3個mysql實例提供高可用,應用使用fabric-aware的鏈接器路由事務和SQL語句到合適的服務器,透明的進行讀和寫操做;
當前僅僅支持異步primary backup複製(半同步複製須要手工完成)。primary處理全部寫操做,Secondaries使用mysql複製從primary同步,能夠進行讀操做。

服務器信息:


1、mysql實例和fabric state store準備
Mysql安裝好後,每一個fabric管理的實例須要開啓gtid、二進制日誌(不須要啓動複製,由fabric完成,可是不支持配置異步複製),將如下配置放到[mysqld]段下面:
    
    
    
    
    
log-bin=mysql-binbinlog_format=ROWserver-id = 29 #保證每一個mysql實例惟一log-slave-updates=truegtid-mode=onenforce-gtid-consistency=truesync-master-info=1

數據庫用戶帳號準備:
一、超級管理員帳號,本例使用root@'172.17.42.1',密碼爲admin@123;
二、fabric管理mysql實例的帳號,本例使用fabric@'172.17.42.1',密碼爲fabric@456;
三、mysql複製專用帳號,本例使用fabric@'172.17.0.%',密碼爲fabric@456,須要與fabric管理mysql的帳號密碼一致;

查看mysqlfabric命令幫助:
mysqlfabric help:顯示簡短的語法信息和幫忙命令
mysqlfabric help commands: 列出全部可用命令和描述
mysqlfabric help groups: 列出可用命令組
mysqlfabric help [group] [command]: 提供命令的詳細幫助信息

2、使用Fabric建立mysql複製高可用組
一、建立組
    
    
    
    
    
# mysqlfabric group create my_groupPassword for admin: Procedure :{ uuid        = 3f7e82bc-4291-4002-8688-1929fc63ed3e,  finished    = True,  success     = True,  return      = True,  activities  = }

要輸入xml-rpc密碼,能夠將密碼指定到fabric的配置文件,或者設置disable_authentication = yes,須要重啓fabric(先mysqlfabric manage stop,而後修改配置文件,否則將報"Permission denied."錯誤)。

二、添加mysql實例到組
    
    
    
    
    
# mysqlfabric group add my_group 172.17.0.50:3306Procedure :{ uuid        = 6e69a5a7-667b-4e63-92c3-2f9f4269d633,  finished    = True,  success     = True,  return      = True,  activities  = }# mysqlfabric group add my_group 172.17.0.47:3306Procedure :{ uuid        = 70a66b03-a16d-426d-a711-8f13da78fc8d,  finished    = True,  success     = True,  return      = True,  activities  = }# mysqlfabric group add my_group 172.17.0.48:3306Procedure :{ uuid        = 15a2e1ad-b726-4fa8-bdf3-a1701e870166,  finished    = True,  success     = True,  return      = True,  activities  = }

添加實例到組錯誤:
    
    
    
    
    
# mysqlfabric --param=servers.user=fabric --param=servers.password=fabric@456 group add my_group 172.17.0.50:3306Password for admin: Procedure :{ uuid        = d2c0d969-5b8a-40c2-ba7a-1bc963c08824,  finished    = True,  success     = False,  return      = ServerError: Error accessing server (172.17.0.50:3306).,  activities  = }

日誌:
    
    
    
    
    
[DEBUG] 1409151193.274349 - Executor-4 - Statement (BEGIN, Params(()).[DEBUG] 1409151193.274764 - Executor-4 - Executing _add_server[DEBUG] 1409151193.274849 - Executor-4 - Statement (SELECT group_id, description, master_uuid, master_defined, status FROM groups WHERE group_id = %s, Params(('my_group',)).[DEBUG] 1409151193.275494 - Executor-4 - Start executing function: discover_uuid((), {'connection_timeout': 5, 'address': '172.17.0.50:3306'}).[DEBUG] 1409151193.276136 - Executor-4 - Error executing function: discover_uuid.[DEBUG] 1409151193.276203 - Executor-4 - _add_server failed, executing compensation[DEBUG] 1409151193.276255 - Executor-4 - Error accessing server (172.17.0.50:3306).[DEBUG] 1409151193.276308 - Executor-4 - Statement (ROLLBACK, Params(()).[DEBUG] 1409151193.276664 - Executor-4 - Complete job (60b0fa1f-3a39-43f3-aa71-46ea732aae84, 946e0199-53ca-4fdc-9b49-f134d44db476, mysql.fabric.services.server._add_server, Error).

將密碼寫到fabric配置文件是能夠,使用命令行參數不能覆蓋配置文件參數,暫時沒有找到是什麼緣由。

三、查看組中信息
能夠看到全部實例的狀態都是SECONDARY
    
    
    
    
    
# mysqlfabric group lookup_servers my_groupCommand :{ success     = True  return      = [{'status': 'SECONDARY', 'server_uuid': '19a37552-2d44-11e4-af5c-763d1493518d', 'mode': 'READ_ONLY', 'weight': 1.0, 'address': '172.17.0.50:3306'}, {'status': 'SECONDARY', 'server_uuid': '7bd52611-2d44-11e4-af5f-3ecad7c2f82a', 'mode': 'READ_ONLY', 'weight': 1.0, 'address': '172.17.0.47:3306'}, {'status': 'SECONDARY', 'server_uuid': 'ade3ee53-2d44-11e4-af60-de532998e8a6', 'mode': 'READ_ONLY', 'weight': 1.0, 'address': '172.17.0.48:3306'}]  activities  = }

四、查看組健康詳細信息
    
    
    
    
    
# mysqlfabric group health my_groupCommand :{ success     = True  return      = {'19a37552-2d44-11e4-af5c-763d1493518d': {'status': 'SECONDARY', 'is_alive': True, 'threads': {'is_configured': False}}, '7bd52611-2d44-11e4-af5f-3ecad7c2f82a': {'status': 'SECONDARY', 'is_alive': True, 'threads': {'is_configured': False}}, 'ade3ee53-2d44-11e4-af60-de532998e8a6': {'status': 'SECONDARY', 'is_alive': True, 'threads': {'is_configured': False}}}  activities  = }

五、提高和降級master服務
建立高可用組後,fabric沒有意識到任何複製拓撲。須要提高一個爲primary,降級剩餘的服務器自動爲secondaries(slaves)。
查看命令幫助:
# mysqlfabric help group promote
group promote group_id  [--slave_id=NONE] [--update_only] [--synchronous]

若是隻是想更新state store,跳過複製配置,使用--update_only參數。
若是slave沒有提供,將選擇一個最好的候選者,候選者必須開啓二進制日誌,同時跟master屬於同一個組,跟master延時小。進行故障切換操做,選擇這個候選者,同時將其餘slave指向到新的master,同時更新state store。

提高一個mysql實例爲master:
    
    
    
    
    
# mysqlfabric group promote my_groupProcedure :{ uuid        = 21eb4d58-d7ec-41eb-a0e5-560eb5976272,  finished    = True,  success     = True,  return      = True,  activities  = }

執行一樣的命名將設置不一樣的服務器爲primary,同時降級當前primary並選取一個新的。若是當前primary錯誤,執行一樣的命令能手動觸發選取一個新的primary。

一個標記位"faulty"狀態的服務器不能提高爲secondary或primary,須要先轉換成"spare"狀態。使用命令mysqlfabric server set_status <server-address> spare
若是直接從"faulty"轉換成"secondary"將報錯:
    
    
    
    
    
# mysqlfabric server set_status 3ecc746f-2e05-11e4-b448-560d7281695e secondaryProcedure :{ uuid        = 06d0acb1-ad01-483b-a1d4-e13c4b775fcd,  finished    = True,  success     = False,  return      = ServerError: Cannot change server's (3ecc746f-2e05-11e4-b448-560d7281695e) status from (FAULTY) to (SECONDARY).,  activities  = }

查看組狀態和驗證是否複製正常:
    
    
    
    
    
# mysqlfabric group lookup_servers my_groupCommand :{ success     = True  return      = [{'status': 'PRIMARY', 'server_uuid': '19a37552-2d44-11e4-af5c-763d1493518d', 'mode': 'READ_WRITE', 'weight': 1.0, 'address': '172.17.0.50:3306'}, {'status': 'SECONDARY', 'server_uuid': '7bd52611-2d44-11e4-af5f-3ecad7c2f82a', 'mode': 'READ_ONLY', 'weight': 1.0, 'address': '172.17.0.47:3306'}, {'status': 'SECONDARY', 'server_uuid': 'ade3ee53-2d44-11e4-af60-de532998e8a6', 'mode': 'READ_ONLY', 'weight': 1.0, 'address': '172.17.0.48:3306'}]  activities  = }

使用show slave status查看slave狀態:
    
    
    
    
    
# mysql -ufabric -pfabric@456 -h172.17.0.47 -e "show slave status\G;"*************************** 1. row ***************************               Slave_IO_State: Connecting to master                  Master_Host: 172.17.0.50                  Master_User: fabric                  Master_Port: 3306                Connect_Retry: 60              Master_Log_File:           Read_Master_Log_Pos: 4               Relay_Log_File: d4f404f647b0-relay-bin.000001                Relay_Log_Pos: 4        Relay_Master_Log_File:              Slave_IO_Running: Connecting            Slave_SQL_Running: Yes              Replicate_Do_DB:           Replicate_Ignore_DB:            Replicate_Do_Table:        Replicate_Ignore_Table:       Replicate_Wild_Do_Table:   Replicate_Wild_Ignore_Table:                    Last_Errno: 0                   Last_Error:                  Skip_Counter: 0          Exec_Master_Log_Pos: 0              Relay_Log_Space: 151              Until_Condition: None               Until_Log_File:                 Until_Log_Pos: 0           Master_SSL_Allowed: No           Master_SSL_CA_File:            Master_SSL_CA_Path:               Master_SSL_Cert:             Master_SSL_Cipher:                Master_SSL_Key:         Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: No                Last_IO_Errno: 1045                Last_IO_Error: error connecting to master 'fabric@172.17.0.50:3306' - retry-time: 60  retries: 31               Last_SQL_Errno: 0               Last_SQL_Error:   Replicate_Ignore_Server_Ids:              Master_Server_Id: 0                  Master_UUID:              Master_Info_File: /usr/local/Percona-Server-5.6.19-rel67.0-618.Linux.x86_64/data/master.info                    SQL_Delay: 0          SQL_Remaining_Delay: NULL      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it           Master_Retry_Count: 86400                  Master_Bind:       Last_IO_Error_Timestamp: 140827 16:42:50     Last_SQL_Error_Timestamp:                Master_SSL_Crl:            Master_SSL_Crlpath:            Retrieved_Gtid_Set:             Executed_Gtid_Set: 7bd52611-2d44-11e4-af5f-3ecad7c2f82a:1-6                Auto_Position: 1

看到鏈接錯誤:
因fabric使用其鏈接管理mysql實例的用戶配置slave,全部須要從新受權slave訪問master:
    
    
    
    
    
# mysql -uroot -padmin@123 -h172.17.0.50 -e "grant replication slave on *.* to fabric@'172.17.0.%' identified by 'fabric@456'" # mysql -uroot -padmin@123 -h172.17.0.47 -e "grant replication slave on *.* to fabric@'172.17.0.%' identified by 'fabric@456'" # mysql -uroot -padmin@123 -h172.17.0.48 -e "grant replication slave on *.* to fabric@'172.17.0.%' identified by 'fabric@456'" 

受權後從新查看slave狀態:看到鏈接正常,複製正常工做
    
    
    
    
    
# mysql -ufabric -pfabric@456 -h172.17.0.48 -e 'show slave status\G'*************************** 1. row ***************************               Slave_IO_State: Waiting for master to send event                  Master_Host: 172.17.0.50                  Master_User: fabric                  Master_Port: 3306                Connect_Retry: 60              Master_Log_File: mysql-bin.000004          Read_Master_Log_Pos: 3451               Relay_Log_File: e294ab366580-relay-bin.000003                Relay_Log_Pos: 448        Relay_Master_Log_File: mysql-bin.000004             Slave_IO_Running: Yes            Slave_SQL_Running: Yes              Replicate_Do_DB:           Replicate_Ignore_DB:            Replicate_Do_Table:        Replicate_Ignore_Table:       Replicate_Wild_Do_Table:   Replicate_Wild_Ignore_Table:                    Last_Errno: 0                   Last_Error:                  Skip_Counter: 0          Exec_Master_Log_Pos: 3451              Relay_Log_Space: 4169              Until_Condition: None               Until_Log_File:                 Until_Log_Pos: 0           Master_SSL_Allowed: No           Master_SSL_CA_File:            Master_SSL_CA_Path:               Master_SSL_Cert:             Master_SSL_Cipher:                Master_SSL_Key:         Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: No                Last_IO_Errno: 0                Last_IO_Error:                Last_SQL_Errno: 0               Last_SQL_Error:   Replicate_Ignore_Server_Ids:              Master_Server_Id: 27                  Master_UUID: 19a37552-2d44-11e4-af5c-763d1493518d             Master_Info_File: /usr/local/Percona-Server-5.6.19-rel67.0-618.Linux.x86_64/data/master.info                    SQL_Delay: 0          SQL_Remaining_Delay: NULL      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it           Master_Retry_Count: 86400                  Master_Bind:       Last_IO_Error_Timestamp:      Last_SQL_Error_Timestamp:                Master_SSL_Crl:            Master_SSL_Crlpath:            Retrieved_Gtid_Set: 19a37552-2d44-11e4-af5c-763d1493518d:1-10            Executed_Gtid_Set: 19a37552-2d44-11e4-af5c-763d1493518d:1-10                Auto_Position: 1# mysql -ufabric -pfabric@456 -h172.17.0.47 -e 'show slave status\G'*************************** 1. row ***************************               Slave_IO_State: Waiting for master to send event                  Master_Host: 172.17.0.50                  Master_User: fabric                  Master_Port: 3306                Connect_Retry: 60              Master_Log_File: mysql-bin.000004          Read_Master_Log_Pos: 3451               Relay_Log_File: d4f404f647b0-relay-bin.000004                Relay_Log_Pos: 448        Relay_Master_Log_File: mysql-bin.000004             Slave_IO_Running: Yes            Slave_SQL_Running: Yes              Replicate_Do_DB:           Replicate_Ignore_DB:            Replicate_Do_Table:        Replicate_Ignore_Table:       Replicate_Wild_Do_Table:   Replicate_Wild_Ignore_Table:                    Last_Errno: 0                   Last_Error:                  Skip_Counter: 0          Exec_Master_Log_Pos: 3451              Relay_Log_Space: 1205              Until_Condition: None               Until_Log_File:                 Until_Log_Pos: 0           Master_SSL_Allowed: No           Master_SSL_CA_File:            Master_SSL_CA_Path:               Master_SSL_Cert:             Master_SSL_Cipher:                Master_SSL_Key:         Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: No                Last_IO_Errno: 0                Last_IO_Error:                Last_SQL_Errno: 0               Last_SQL_Error:   Replicate_Ignore_Server_Ids:              Master_Server_Id: 27                  Master_UUID: 19a37552-2d44-11e4-af5c-763d1493518d             Master_Info_File: /usr/local/Percona-Server-5.6.19-rel67.0-618.Linux.x86_64/data/master.info                    SQL_Delay: 0          SQL_Remaining_Delay: NULL      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it           Master_Retry_Count: 86400                  Master_Bind:       Last_IO_Error_Timestamp:      Last_SQL_Error_Timestamp:                Master_SSL_Crl:            Master_SSL_Crlpath:            Retrieved_Gtid_Set: 19a37552-2d44-11e4-af5c-763d1493518d:1-10            Executed_Gtid_Set: 19a37552-2d44-11e4-af5c-763d1493518d:1-10                Auto_Position: 1

六、啓動或禁用故障檢測
若是primary故障,可能想自動提高一個次級到primary,重定向剩餘的secondaries到新的primary:
    
    
    
    
    
# mysqlfabric group activate my_groupProcedure :{ uuid        = 1d78324a-d39d-4098-8774-b0c2359a57ed,  finished    = True,  success     = True,  return      = True,  activities  = }

狀態檢測語句:
[DEBUG] 1409157170.648943 - FailureDetector(my_group) - Statement (SELECT server_uuid, server_address, mode, status, weight FROM servers WHERE group_id = %s, Params(('my_group',)).
[DEBUG] 1409157172.655084 - FailureDetector(my_group) - Statement (SELECT group_id, description, master_uuid, master_defined, status FROM groups WHERE group_id = %s, Params(('my_group',)).

測試關閉當前master,看是否選取新的master,fabric能自動進程故障切換:
查看當前組的狀態:當前master爲" 172.17.0.51 "
    
    
    
    
    
# mysqlfabric group lookup_servers my_groupCommand :{ success     = True  return      = [{'status': 'SECONDARY', 'server_uuid': '19a37552-2d44-11e4-af5c-763d1493518d', 'mode': 'READ_ONLY', 'weight': 1.0, 'address': '172.17.0.50:3306'}, {'status': 'PRIMARY', 'server_uuid': '3ecc746f-2e05-11e4-b448-560d7281695e', 'mode': 'READ_WRITE', 'weight': 1.0, 'address': '172.17.0.51:3306'}, {'status': 'SECONDARY', 'server_uuid': 'ade3ee53-2d44-11e4-af60-de532998e8a6', 'mode': 'READ_ONLY', 'weight': 1.0, 'address': '172.17.0.48:3306'}]  activities  = }

關閉當前master實例:
    
    
    
    
    
# mysqladmin -h172.17.0.51 -uroot -padmin@123 shutdown

關閉172.17.0.51實例後,fabric已經標記它的狀態爲"FAULTY";
    
    
    
    
    
# mysqlfabric group lookup_servers my_groupCommand :{ success     = True  return      = [{'status': 'SECONDARY', 'server_uuid': '19a37552-2d44-11e4-af5c-763d1493518d', 'mode': 'READ_ONLY', 'weight': 1.0, 'address': '172.17.0.50:3306'}, {'status': 'FAULTY', 'server_uuid': '3ecc746f-2e05-11e4-b448-560d7281695e', 'mode': 'READ_ONLY', 'weight': 1.0, 'address': '172.17.0.51:3306'}, {'status': 'PRIMARY', 'server_uuid': 'ade3ee53-2d44-11e4-af60-de532998e8a6', 'mode': 'READ_WRITE', 'weight': 1.0, 'address': '172.17.0.48:3306'}]  activities  = }

從新啓動172.17.0.51:3306實例,可是狀態不會恢復:
    
    
    
    
    
# mysql -h172.17.0.51 -uroot -padmin@123 -e "show master status"+------------------+----------+--------------+------------------+-------------------------------------------+| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                         |+------------------+----------+--------------+------------------+-------------------------------------------+| mysql-bin.000003 |      191 |              |                  | 19a37552-2d44-11e4-af5c-763d1493518d:1-10 |+------------------+----------+--------------+------------------+-------------------------------------------+# mysqlfabric group lookup_servers my_groupCommand :{ success     = True  return      = [{'status': 'SECONDARY', 'server_uuid': '19a37552-2d44-11e4-af5c-763d1493518d', 'mode': 'READ_ONLY', 'weight': 1.0, 'address': '172.17.0.50:3306'}, {'status': 'FAULTY', 'server_uuid': '3ecc746f-2e05-11e4-b448-560d7281695e', 'mode': 'READ_ONLY', 'weight': 1.0, 'address': '172.17.0.51:3306'}, {'status': 'PRIMARY', 'server_uuid': 'ade3ee53-2d44-11e4-af60-de532998e8a6', 'mode': 'READ_WRITE', 'weight': 1.0, 'address': '172.17.0.48:3306'}]  activities  = }

須要手動設置服務器的狀態:
查看幫助命令:
# mysqlfabric help server set_status
server set_status server_id status  [--update_only] [--synchronous]  

Set a server's status.

支持的狀態值爲primary,secondary,spare,faulty。
能手動改變服務器的狀態,改變到spare,避免寫和讀訪問,同時保證當故障切換時不會被選取。

改變錯誤狀態的實例(172.17.0.51)的狀態爲secondary,注意須要先轉換成spare:
    
    
    
    
    
# mysqlfabric server set_status 3ecc746f-2e05-11e4-b448-560d7281695e spareProcedure :{ uuid        = e96802d7-5fc1-4a70-94d4-4d8d31d13960,  finished    = True,  success     = True,  return      = True,  activities  = }# mysqlfabric server set_status 3ecc746f-2e05-11e4-b448-560d7281695e secondaryProcedure :{ uuid        = c74edd89-ac4a-45ce-8d18-22423ed73561,  finished    = True,  success     = True,  return      = True,  activities  = }

查看狀態:
    
    
    
    
    
# mysqlfabric group lookup_servers my_groupCommand :{ success     = True  return      = [{'status': 'SECONDARY', 'server_uuid': '19a37552-2d44-11e4-af5c-763d1493518d', 'mode': 'READ_ONLY', 'weight': 1.0, 'address': '172.17.0.50:3306'}, {'status': 'SECONDARY', 'server_uuid': '3ecc746f-2e05-11e4-b448-560d7281695e', 'mode': 'READ_ONLY', 'weight': 1.0, 'address': '172.17.0.51:3306'}, {'status': 'PRIMARY', 'server_uuid': 'ade3ee53-2d44-11e4-af60-de532998e8a6', 'mode': 'READ_WRITE', 'weight': 1.0, 'address': '172.17.0.48:3306'}]  activities  = }

禁用故障檢測自動切換:
    
    
    
    
    
# mysqlfabric group deactivate my_groupProcedure :{ uuid        = a88d2bb7-f0c7-46d8-925a-c982287d6447,  finished    = True,  success     = True,  return      = True,  activities  = }

七、執行更新和查詢
應用程序經過fabric-aware鏈接器鏈接到fabric,獲取服務器、組信息。

測試腳本:
    
    
    
    
    
# cat fabric_test1.py#!/usr/bin/env python#coding:utf-8# Author:  AnthonyYau --<q_yxian@163.com># Purpose: fabric test scripts1.# Created: 2014/8/26import sysimport mysql.connectorfrom mysql.connector import fabricdef  main():    try:        conn = mysql.connector.connect(            fabric = {"host":"localhost","port":32274,"username":"admin",                      "password":"admin"}, #指定使用xml-rpm鏈接fabric的用戶名和密碼            user = 'root',database = 'test',password = 'admin@123',autocommit = True #指定mysql實例的用戶名和密碼        )    except Exception,e:        print e        sys.exit(1)    else:        conn.set_property(mode=fabric.MODE_READWRITE,group="my_group") #指定訪問模式,以及須要訪問的HA組        cur = conn.cursor()        cur.execute(            "CREATE TABLE IF NOT EXISTS subscribers (" #建立表        " sub_no INT,"        " first_name CHAR(40),"        " last_name CHAR(4)"        ")"        ) finally: conn.close()    if  __name__ == '__main__':    main()

    
    
    
    
    
# cat fabric_test2.py   #!/usr/bin/env python#coding:utf-8# Author:  AnthonyYau --<q_yxian@163.com># Purpose: fabric test scripts2.# Created: 2014/8/26import sysimport mysql.connectorfrom mysql.connector import fabricdef add_subscriber(conn,sub_no,first_name,last_name):    conn.set_property(group="my_group",mode=fabric.MODE_READWRITE) #指定訪問模式,以及訪問的組    cur = conn.cursor()    cur.execute(        "INSERT INTO subscribers VALUES (%s,%s,%s)",        (sub_no,first_name,last_name)    )def  main():    try:        conn = mysql.connector.connect(            fabric = {"host":"localhost","port":32274,"username":"admin",                      "password":"admin"}, #指定使用xml-rpc鏈接fabric的用戶名和密碼            user="root",database="test",password="admin@123",autocommit=True #指定訪問mysql實例的用戶名和密碼        )    except Exception,e:        print e        sys.exit(1)    else:        #conn.set_property(group="my_group",mode=fabric.MODE_READWRITE)        add_subscriber(conn, 72, "Billy", "Fish") #調用函數,插入數據        add_subscriber(conn, 500, "Billy", "Joel")        add_subscriber(conn, 1500, "Arthur", "Askey")        add_subscriber(conn, 5000, "Billy", "Fish")        add_subscriber(conn, 15000, "Jimmy", "White")        add_subscriber(conn, 17542, "Bobby", "Ball")     finally: conn.close()    if  __name__ == '__main__':

執行測試程序:
    
    
    
    
    
python fabric_test1.pypython fabric_test2.py

檢查結果(查看兩臺從服務器的數據):當前slave爲 172.17.0.50和 172.17.0.51
    
    
    
    
    
# mysqlfabric group lookup_servers my_groupCommand :{ success     = True  return      = [{'status': 'SECONDARY', 'server_uuid': '19a37552-2d44-11e4-af5c-763d1493518d', 'mode': 'READ_ONLY', 'weight': 1.0, 'address': '172.17.0.50:3306'}, {'status': 'SECONDARY', 'server_uuid': '3ecc746f-2e05-11e4-b448-560d7281695e', 'mode': 'READ_ONLY', 'weight': 1.0, 'address': '172.17.0.51:3306'}, {'status': 'PRIMARY', 'server_uuid': 'ade3ee53-2d44-11e4-af60-de532998e8a6', 'mode': 'READ_WRITE', 'weight': 1.0, 'address': '172.17.0.48:3306'}]  activities  = }

使用腳本檢查結果:
    
    
    
    
    
# python fabric_test3.py 72 Billy Fish500 Billy Joel1500 Arthur Aske5000 Billy Fish15000 Jimmy Whit17542 Bobby Ball

查詢腳本以下:
     
     
     
     
     
#!/usr/bin/env python#coding:utf-8# Author:  AnthonyYau --<q_yxian@163.com># Purpose: fabric test scripts3.# Created: 2014/8/27import sysimport mysql.connectorfrom mysql.connector import fabricdef  main():    try:        conn = mysql.connector.connect(            fabric = {"host":"localhost","port":32274,"username":"admin",                                  "password":"admin"},                        user="root",database="test",password="admin@123",autocommit=True                )    except Exception,e:        print e        sys.exit(1)    else:        conn.set_property(group="my_group",mode=fabric.MODE_READONLY) #使用readonly訪問模式(從slave獲取數據),也可使用readwrite        cur = conn.cursor()        cur.execute('select * from test.subscribers')        results = cur.fetchall()        if results:            for id,f_name,l_name in results:                print "%s\t%s\t%s" %(id,f_name,l_name) finally: conn.close()    if  __name__ == '__main__':    main()

使用mysql客戶端進行檢查:
    
    
    
    
    
# mysql -uroot -padmin@123 -h172.17.0.51 -e "select * from test.subscribers"+--------+------------+-----------+| sub_no | first_name | last_name |+--------+------------+-----------+|     72 | Billy      | Fish      ||    500 | Billy      | Joel      ||   1500 | Arthur     | Aske      ||   5000 | Billy      | Fish      ||  15000 | Jimmy      | Whit      ||  17542 | Bobby      | Ball      |+--------+------------+-----------+[root@yau620 ~]# mysql -uroot -padmin@123 -h172.17.0.50 -e "select * from test.subscribers"+--------+------------+-----------+| sub_no | first_name | last_name |+--------+------------+-----------+|     72 | Billy      | Fish      ||    500 | Billy      | Joel      ||   1500 | Arthur     | Aske      ||   5000 | Billy      | Fish      ||  15000 | Jimmy      | Whit      ||  17542 | Bobby      | Ball      |+--------+------------+-----------+

3、組維護
一、找出哪些服務器在組中,查看組狀態
    
    
    
    
    
# mysqlfabric group lookup_servers my_groupCommand :{ success     = True  return      = [{'status': 'PRIMARY', 'server_uuid': '19a37552-2d44-11e4-af5c-763d1493518d', 'mode': 'READ_WRITE', 'weight': 1.0, 'address': '172.17.0.50:3306'}, {'status': 'SECONDARY', 'server_uuid': '7bd52611-2d44-11e4-af5f-3ecad7c2f82a', 'mode': 'READ_ONLY', 'weight': 1.0, 'address': '172.17.0.47:3306'}, {'status': 'SECONDARY', 'server_uuid': 'ade3ee53-2d44-11e4-af60-de532998e8a6', 'mode': 'READ_ONLY', 'weight': 1.0, 'address': '172.17.0.48:3306'}]  activities  = }

二、從組中移除mysql實例
有時須要將secondaries離線進行維護,在中止服務以前,須要先從組中移除它,避免fabric錯誤檢查觸發任何動做:
# mysqlfabric help group remove
group remove group_id server_id  [--synchronous]  

Remove a server from a group.

如從my_group組中移除server_uuid爲7bd52611-2d44-11e4-af5f-3ecad7c2f82a的實例:
    
    
    
    
    
# mysqlfabric group remove my_group 7bd52611-2d44-11e4-af5f-3ecad7c2f82aProcedure :{ uuid        = 07317ba2-497c-43f0-b8a9-e43ef9ded834,  finished    = True,  success     = True,  return      = True,  activities  = }# mysqlfabric group lookup_servers my_groupCommand :{ success     = True  return      = [{'status': 'PRIMARY', 'server_uuid': '19a37552-2d44-11e4-af5c-763d1493518d', 'mode': 'READ_WRITE', 'weight': 1.0, 'address': '172.17.0.50:3306'}, {'status': 'SECONDARY', 'server_uuid': 'ade3ee53-2d44-11e4-af60-de532998e8a6', 'mode': 'READ_ONLY', 'weight': 1.0, 'address': '172.17.0.48:3306'}]  activities  = }

而後能夠從新添加到組,將自動開啓複製:
    
    
    
    
    
# mysqlfabric group add my_group 172.17.0.47:3306Procedure :{ uuid        = 39344790-3af8-4049-92a6-669bd2642a1c,  finished    = True,  success     = True,  return      = True,  activities  = }

查看複製狀態:
    
    
    
    
    
# mysql -ufabric -pfabric@456 -h172.17.0.47 -e 'show slave status\G'*************************** 1. row ***************************               Slave_IO_State: Waiting for master to send event                  Master_Host: 172.17.0.50                  Master_User: fabric                  Master_Port: 3306                Connect_Retry: 60              Master_Log_File: mysql-bin.000004          Read_Master_Log_Pos: 3451               Relay_Log_File: d4f404f647b0-relay-bin.000002                Relay_Log_Pos: 408        Relay_Master_Log_File: mysql-bin.000004             Slave_IO_Running: Yes            Slave_SQL_Running: Yes              Replicate_Do_DB:           Replicate_Ignore_DB:            Replicate_Do_Table:        Replicate_Ignore_Table:       Replicate_Wild_Do_Table:   Replicate_Wild_Ignore_Table:                    Last_Errno: 0                   Last_Error:                  Skip_Counter: 0          Exec_Master_Log_Pos: 3451              Relay_Log_Space: 619              Until_Condition: None               Until_Log_File:                 Until_Log_Pos: 0           Master_SSL_Allowed: No           Master_SSL_CA_File:            Master_SSL_CA_Path:               Master_SSL_Cert:             Master_SSL_Cipher:                Master_SSL_Key:         Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: No                Last_IO_Errno: 0                Last_IO_Error:                Last_SQL_Errno: 0               Last_SQL_Error:   Replicate_Ignore_Server_Ids:              Master_Server_Id: 27                  Master_UUID: 19a37552-2d44-11e4-af5c-763d1493518d             Master_Info_File: /usr/local/Percona-Server-5.6.19-rel67.0-618.Linux.x86_64/data/master.info                    SQL_Delay: 0          SQL_Remaining_Delay: NULL      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it           Master_Retry_Count: 86400                  Master_Bind:       Last_IO_Error_Timestamp:      Last_SQL_Error_Timestamp:                Master_SSL_Crl:            Master_SSL_Crlpath:            Retrieved_Gtid_Set:             Executed_Gtid_Set: 19a37552-2d44-11e4-af5c-763d1493518d:1-10                Auto_Position: 1
三、添加一個全新的Mysql實例(實例IP爲172.17.0.51)到HA group組
建立數據庫複製帳號:
    
    
    
    
    
# mysql -uroot -padmin@123 -h172.17.0.51 -e "grant replication slave on *.* to fabric@'172.17.0.%' identified by 'fabric@456'"# mysql -uroot -padmin@123 -h172.17.0.51 -e "show slave status"

添加到my_group組:
    
    
    
    
    
# mysqlfabric group add my_group 172.17.0.51:3306 Procedure :{ uuid        = 45c32c66-6e60-44d8-8406-308de01b1ef9,  finished    = True,  success     = True,  return      = True,  activities  = }

查看複製狀態,fabric已經開啓複製:
    
    
    
    
    
# mysql -uroot -padmin@123 -h172.17.0.51 -e "show slave status\G"*************************** 1. row ***************************               Slave_IO_State: Waiting for master to send event                  Master_Host: 172.17.0.50                  Master_User: fabric                  Master_Port: 3306                Connect_Retry: 60              Master_Log_File: mysql-bin.000004          Read_Master_Log_Pos: 3451               Relay_Log_File: 9355b5f279ad-relay-bin.000003                Relay_Log_Pos: 448        Relay_Master_Log_File: mysql-bin.000004             Slave_IO_Running: Yes            Slave_SQL_Running: Yes              Replicate_Do_DB:           Replicate_Ignore_DB:            Replicate_Do_Table:        Replicate_Ignore_Table:       Replicate_Wild_Do_Table:   Replicate_Wild_Ignore_Table:                    Last_Errno: 0                   Last_Error:                  Skip_Counter: 0          Exec_Master_Log_Pos: 3451              Relay_Log_Space: 4169              Until_Condition: None               Until_Log_File:                 Until_Log_Pos: 0           Master_SSL_Allowed: No           Master_SSL_CA_File:            Master_SSL_CA_Path:               Master_SSL_Cert:             Master_SSL_Cipher:                Master_SSL_Key:         Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: No                Last_IO_Errno: 0                Last_IO_Error:                Last_SQL_Errno: 0               Last_SQL_Error:   Replicate_Ignore_Server_Ids:              Master_Server_Id: 27                  Master_UUID: 19a37552-2d44-11e4-af5c-763d1493518d             Master_Info_File: /usr/local/Percona-Server-5.6.19-rel67.0-618.Linux.x86_64/data/master.info                    SQL_Delay: 0          SQL_Remaining_Delay: NULL      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it           Master_Retry_Count: 86400                  Master_Bind:       Last_IO_Error_Timestamp:      Last_SQL_Error_Timestamp:                Master_SSL_Crl:            Master_SSL_Crlpath:            Retrieved_Gtid_Set: 19a37552-2d44-11e4-af5c-763d1493518d:1-10            Executed_Gtid_Set: 19a37552-2d44-11e4-af5c-763d1493518d:1-10                Auto_Position: 1

四、關閉primary
primary不能從組中移除,使用group demote關閉在組中的任何primary,不會從新選取一個新master,同時也不會關閉故障檢測。
    
    
    
    
    
# mysqlfabric group demote my_groupProcedure :{ uuid        = 448df7ff-f0b5-4868-be15-8b505ec38c19,  finished    = True,  success     = True,  return      = True,  activities  = }

查看組狀態:全部mysql實例的狀態都已經變成SECONDARY
    
    
    
    
    
# mysqlfabric group lookup_servers my_groupCommand :{ success     = True  return      = [{'status': 'SECONDARY', 'server_uuid': '19a37552-2d44-11e4-af5c-763d1493518d', 'mode': 'READ_ONLY', 'weight': 1.0, 'address': '172.17.0.50:3306'}, {'status': 'SECONDARY', 'server_uuid': '3ecc746f-2e05-11e4-b448-560d7281695e', 'mode': 'READ_ONLY', 'weight': 1.0, 'address': '172.17.0.51:3306'}, {'status': 'SECONDARY', 'server_uuid': '7bd52611-2d44-11e4-af5f-3ecad7c2f82a', 'mode': 'READ_ONLY', 'weight': 1.0, 'address': '172.17.0.47:3306'}, {'status': 'SECONDARY', 'server_uuid': 'ade3ee53-2d44-11e4-af60-de532998e8a6', 'mode': 'READ_ONLY', 'weight': 1.0, 'address': '172.17.0.48:3306'}]  activities  = }

五、手動提高一個服務組,選取一個master
當前master是172.17.0.48:
    
    
    
    
    
# mysqlfabric group promote my_groupProcedure :{ uuid        = d3072441-a023-42bd-8fe8-6746a620189d,  finished    = True,  success     = True,  return      = True,  activities  = }# mysqlfabric group lookup_servers my_groupCommand :{ success     = True  return      = [{'status': 'SECONDARY', 'server_uuid': '19a37552-2d44-11e4-af5c-763d1493518d', 'mode': 'READ_ONLY', 'weight': 1.0, 'address': '172.17.0.50:3306'}, {'status': 'SECONDARY', 'server_uuid': '3ecc746f-2e05-11e4-b448-560d7281695e', 'mode': 'READ_ONLY', 'weight': 1.0, 'address': '172.17.0.51:3306'}, {'status': 'SECONDARY', 'server_uuid': '7bd52611-2d44-11e4-af5f-3ecad7c2f82a', 'mode': 'READ_ONLY', 'weight': 1.0, 'address': '172.17.0.47:3306'}, {'status': 'PRIMARY', 'server_uuid': 'ade3ee53-2d44-11e4-af60-de532998e8a6', 'mode': 'READ_WRITE', 'weight': 1.0, 'address': '172.17.0.48:3306'}]  activities  = }

從新選取一個新master:fabric已經自動選取了172.17.0.47做爲master
    
    
    
    
    
# mysqlfabric group promote my_groupProcedure :{ uuid        = c779bfa6-5a57-4d60-9899-d64b85e9bee3,  finished    = True,  success     = True,  return      = True,  activities  = }# mysqlfabric group lookup_servers my_groupCommand :{ success     = True  return      = [{'status': 'SECONDARY', 'server_uuid': '19a37552-2d44-11e4-af5c-763d1493518d', 'mode': 'READ_ONLY', 'weight': 1.0, 'address': '172.17.0.50:3306'}, {'status': 'SECONDARY', 'server_uuid': '3ecc746f-2e05-11e4-b448-560d7281695e', 'mode': 'READ_ONLY', 'weight': 1.0, 'address': '172.17.0.51:3306'}, {'status': 'PRIMARY', 'server_uuid': '7bd52611-2d44-11e4-af5f-3ecad7c2f82a', 'mode': 'READ_WRITE', 'weight': 1.0, 'address': '172.17.0.47:3306'}, {'status': 'SECONDARY', 'server_uuid': 'ade3ee53-2d44-11e4-af60-de532998e8a6', 'mode': 'READ_ONLY', 'weight': 1.0, 'address': '172.17.0.48:3306'}]  activities  = }

六、移除組(使用group destroy)
若是組沒有服務器,是空的,能移除組
    
    
    
    
    
# mysqlfabric group create my_testProcedure :{ uuid        = d53ba76d-1ed0-4e9d-96b3-01102b46f0bf,  finished    = True,  success     = True,  return      = True,  activities  = }# mysqlfabric group destroy my_testProcedure :{ uuid        = 7046fea7-6e87-4dc0-afd4-e21ea399f9c1,  finished    = True,  success     = True,  return      = True,  activities  = }

也能強制的移除非空的組(使用--force):
    
    
    
    
    
# mysqlfabric group create my_testProcedure :{ uuid        = be15e7bd-e597-4e67-aa4d-4ff34e0d3af8,  finished    = True,  success     = True,  return      = True,  activities  = }# mysqlfabric group add my_test 172.17.0.52:3306Procedure :{ uuid        = da981fe4-eeaf-4a3f-a770-d82d60a2ad64,  finished    = True,  success     = True,  return      = True,  activities  = }# mysqlfabric group lookup_servers my_testCommand :{ success     = True  return      = [{'status': 'SECONDARY', 'server_uuid': '7bd52611-2d44-11e4-af5f-3ecad7c2f82a', 'mode': 'READ_ONLY', 'weight': 1.0, 'address': '172.17.0.52:3306'}]  activities  = }# mysqlfabric group destroy my_testProcedure :{ uuid        = 63b0ec92-ffa2-4f54-9c21-530b1532d21c,  finished    = True,  success     = False,  return      = GroupError: Group (my_test) is not empty.,  activities  = }# mysqlfabric group destroy my_test --forceProcedure :{ uuid        = 37c5b49b-c451-41ad-9193-c9e1f06edaa9,  finished    = True,  success     = True,  return      = True,  activities  = }

4、總結
一、使用fabric實現mysql複製高可用,以組爲單位對mysql實例進行管理,能夠管理多個HA組;
二、當前fabric只能實現自動配置異步複製,不支持半同步複製;
三、mysql實例進行恢復後,fabric不會自動改變實例的狀態,須要手工修改;
四、能在線添加(自動配置好複製,當即同步數據)和刪除mysql實例;
五、fabric state store和fabric note當前是單點,雖然 fabric-aware鏈接器能夠緩存路由信息(能夠配置ttl值);
六、使用fabric不須要對slave配置vip,不像MHA和MMM等須要對vip進行管理;
七、mysql支持須要支持gtid,也是就要升級到mysql 5.6.10以上版本;

參考:



相關文章
相關標籤/搜索