MySQL RouterとMySQL Fabric(sharding)環境との連携
-
Upload
shinya-sugiyama -
Category
Software
-
view
659 -
download
0
Transcript of MySQL RouterとMySQL Fabric(sharding)環境との連携
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MySQL RouterとMySQL Fabric MySQL FabricによるSharding環境においてのMySQL Router連携 Created: 2016/01/23
MySQL Global Business Unit
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
以下の事項は、弊社の一般的な製品の方向性に関する概要を説明するものです。 また、情報提供を唯一の目的とするものであり、いかなる契約にも組み込むことはできません。 以下の事項は、マテリアルやコード、機能を提供することをコミットメントするものではない為、購買決定を行う際の判断材料になさらないで下さい。 オラクル製品に関して記載されている機能の開発、リリースおよび時期については、 弊社の裁量により決定されます。
SAFE HARBOR STATEMENT
3
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MySQL Fabricとは?
• MySQLサーバー群を管理する 統合型のフレームワーク
•高可用性とデータ・シャーディング による拡張性を実現する事が可能
• OpenStack Novaとの連携
• MySQL Utilitiesの一部として提供 (2014-05-27 ver.1.4.3~) – GTIDモードによるレプリケーション 機能を活用している (MySQL 5.6.5以降で使用可能)
MySQL Fabric
Connector
Application
Read-slaves
SQL
Master group
Read-slaves
Master group 参照:MySQL Fabric の特徴と利点 http://www-jp.mysql.com/products/enterprise/fabric/features.html
Master
Slave
コネクター
管理
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Connectorからデータベースへのアクセス 常に最新の状況をメンバー間で共有
MySQL Fabric
Connector
Application
Read-slaves
Master group
アプリケーションからデータベースへの接続
① Fabric対応コネクターがfabric.cfgの設定をImportして、Fabricノードにアクセスし、レプリケーションの構成やシャーディング情報等を取得。
② ①で取得した情報をベースにマスターサーバー(Read_Write), スレーブサーバー(Read_Only)へアクセス
①
② MySQL Backing Store
管理ノード Master/Slave
コネクター
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MySQL
Backing
Store
MySQL Fabricと管理データベース 管理サーバーでは、XML-RPCとMySQL Protocol にてノードと通信しFabricの状態を管理
管理ノード
管理用データベースには、 Fabricで構成されたデータベースの最新状況を保管
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Sharding with Fabric 書き込みスケーラビリティ より多くの書き込みを処理することが可能
大規模なデータセット 大き過ぎるデータベース/単一サーバーに収まらないデータ
性能改善 小さなインデックスサイズ/ワーキングセットに分割
UID 1-299 UID 300-599
キー範囲分割 UID 600-899 UID 900-1199 UID 1200-1499 ………………
Single Master in each HA Group.
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
例) MySQL Fabric 書き込みSharding
1) Fabricグループ構成の確認 2) FabricにSharding Groupを追加 mysqlfabric group create shard# mysqlfabric group add shard# サーバー mysqlfabric sharding add_table 1 test.employees emp_no mysqlfabric sharding add_shard 1 shard1/1,shard2/300 3) Fabricグループ構成の確認 # shard1 (Server:63304,63305,63306) # shard2 (Server:63307,63308,63309) 300以上 mysqlfabric dump sharding_information mysqlfabric group lookup_servers グループ名
1~299
300~
※Shardingを削除する場合は、定義をDisableしてからRemove
Fabric対応コネクターが自動的にDMLを適切なグループに割り振ります
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
例) Fabric対応コネクター (python)
import mysql.connector from mysql.connector import fabric import time def add_employee(conn, emp_no, first_name, last_name): try: conn.set_property(tables=["test.employees"], key=str(emp_no), mode=fabric.MODE_READWRITE, scope=fabric.SCOPE_LOCAL) cur = conn.cursor() cur.execute("INSERT INTO employees VALUES (%s, %s, %s)", (emp_no, first_name, last_name)) except mysql.connector.Error: print "add_employee: Database connection error, trying to reconnect ..." conn=connect() def find_employee(conn, emp_no): try: conn.set_property(tables=["test.employees"], key=str(emp_no), mode=fabric.MODE_READONLY, scope=fabric.SCOPE_LOCAL) cur = conn.cursor() cur.execute( “SELECT concat(‘Hostname:’,@@hostname),concat(‘: ’,@@port,‘ ; ’), first_name, last_name FROM employees WHERE emp_no = %s", (emp_no,) ) except mysql.connector.Error: print "find_employee: Database connection error, trying to reconnect ..." conn=connect() for row in cur: print row[0],row[1],row[2],row[3] def connect(): try: conn=mysql.connector.connect( fabric={"host" : "localhost", "port" : 32274, "username": "admin", "password": "admin"}, user="app", database="test", password="app", autocommit=True
コネクターがSharding Ruleをベースとして、データを自動的にそれぞれのグループへ 振り分けると同時に読み込みに関しても自動的にデータを持つグループへ分散
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MySQL RouterからShardingされた Fabric Groupにアクセスする場合
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
説明用) Sharding定義 -bash-4.2$ mysqlfabric dump sharding_information Fabric UUID: 5ca1ab1e-a007-feed-f00d-cab3fe13249e Time-To-Live: 1 schema_name table_name column_name lower_bound shard_id type_name group_id global_group ----------- ---------- ----------- ----------- -------- --------- -------- ------------ test employees emp_no 1 1 RANGE shard1 global test employees emp_no 500 2 RANGE shard2 global
global server_uuid address status mode weight ------------------------------------ --------------- --------- ---------- ------ 6d950152-c1c5-11e5-8f6d-080027d65c57 127.0.0.1:63301 PRIMARY READ_WRITE 1.0 70d6ea75-c1c5-11e5-8fe0-080027d65c57 127.0.0.1:63302 SECONDARY READ_ONLY 1.0
shard1 server_uuid address status mode weight ------------------------------------ --------------- --------- ---------- ------ 74f7973b-c1c5-11e5-930f-080027d65c57 127.0.0.1:63303 PRIMARY READ_WRITE 1.0 79516b62-c1c5-11e5-9412-080027d65c57 127.0.0.1:63304 SECONDARY READ_ONLY 1.0 7c69673a-c1c5-11e5-94da-080027d65c57 127.0.0.1:63305 SECONDARY READ_ONLY 1.0
shard2 server_uuid address status mode weight ------------------------------------ --------------- --------- ---------- ------ 802efea1-c1c5-11e5-95fe-080027d65c57 127.0.0.1:63306 PRIMARY READ_WRITE 1.0 84e9c0e9-c1c5-11e5-9789-080027d65c57 127.0.0.1:63307 SECONDARY READ_ONLY 1.0 88fe54f6-c1c5-11e5-9a10-080027d65c57 127.0.0.1:63308 SECONDARY READ_ONLY 1.0
親グループのGlobalに対して、DDL, DMLを実行するとShard1, Shard2にも反映されます。
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MySQL Router設定ファイルをSharding環境用に設定
[fabric_cache:ha1] address = localhost:32275 user = admin [routing:Read_Write_Global] bind_port = 7001 destinations = fabric+cache://ha1/group/global mode = read-write [routing:Read_Only_Global] bind_port = 7002 destinations = fabric+cache://ha1/group/global mode = read-only
[routing:Read_Write_shard1] bind_port = 7003 destinations = fabric+cache://ha1/group/shard1 mode = read-write [routing:Read_Only_shard1] bind_port = 7004 destinations = fabric+cache://ha1/group/shard1 mode = read-only [routing:Read_Write_shard2] bind_port = 7005 destinations = fabric+cache://ha1/group/shard2 mode = read-write [routing:Read_Only_shard2] bind_port = 7006 destinations = fabric+cache://ha1/group/shard2 mode = read-only [keepalive] interval = 60
それぞれのレプリケーショングループ (Sharding Group)毎にrouter経由の接続を作成
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
例) test.employeeにデータをINSERT (Fabric対応Connector経由)
-bash-4.2$ mysql -h 127.0.0.1 -P 7003 -u root --password=root -e "select count(*),@@port FROM test.employees" mysql: [Warning] Using a password on the command line interface can be insecure. +----------+--------+ | count(*) | @@port | +----------+--------+ | 22 | 63303 | +----------+--------+ -bash-4.2$ mysql -h 127.0.0.1 -P 7004 -u root --password=root -e "select count(*),@@port FROM test.employees" mysql: [Warning] Using a password on the command line interface can be insecure. +----------+--------+ | count(*) | @@port | +----------+--------+ | 22 | 63304 | +----------+--------+ -bash-4.2$ mysql -h 127.0.0.1 -P 7004 -u root --password=root -e "select count(*),@@port FROM test.employees" mysql: [Warning] Using a password on the command line interface can be insecure. +----------+--------+ | count(*) | @@port | +----------+--------+ | 22 | 63305 | +----------+--------+
schema_name table_name column_name lower_bound shard_id type_name group_id global_group ----------- ---------- ----------- ----------- -------- --------- -------- ------------ test employees emp_no 1 1 RANGE shard1 global test employees emp_no 500 2 RANGE shard2 global
Shard1のみに22件データがINSERTされている事が確認出来る
参照はRouter経由で行っている為、Round-Robinしている
メモ: Fabric対応コネクター経由の処理は常に、 アプリケーションからの接続先がFabric管理サーバー スライド P.8参照
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
例) test.employeeにデータをINSERT (MySQL Router経由)
-bash-4.2$ mysql -h 127.0.0.1 -P 7004 -u root --password=root -e "select count(*),@@port FROM test.employees" +----------+--------+ | count(*) | @@port | +----------+--------+ | 22 | 63305 | +----------+--------+ -bash-4.2$ mysql -h 127.0.0.1 -P 7003 -u root --password=root -e "insert into test.employees(emp_no,first_name,last_name) values(23,'Shard1','Insert from P7003')" -bash-4.2$ mysql -h 127.0.0.1 -P 7004 -u root --password=root -e "select count(*),@@port FROM test.employees" +----------+--------+ | count(*) | @@port | +----------+--------+ | 23 | 63304 | +----------+--------+ -bash-4.2$ mysql -h 127.0.0.1 -P 7004 -u root --password=root -e "select count(*),@@port FROM test.employees" +----------+--------+ | count(*) | @@port | +----------+--------+ | 23 | 63305 | +----------+--------+
schema_name table_name column_name lower_bound shard_id type_name group_id global_group ----------- ---------- ----------- ----------- -------- --------- -------- ------------ test employees emp_no 1 1 RANGE shard1 global test employees emp_no 500 2 RANGE shard2 global
Router経由でshard1に データを1件INSERT
参照はRouter経由で行っている為、Round-Robinしている
メモ: MySQL Router2.0.2経由の処理は、アプリケーション内部で それぞれのShardingグループ毎の接続に分ける必要あり。
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
参考)
MySQL Fabric
https://www.mysql.com/products/enterprise/fabric.html
MySQL Router
https://www.mysql.com/products/enterprise/router.html
https://dev.mysql.com/doc/mysql-router/en/
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |