OneProxy實現MySQL分庫分表

 

簡介

Part1:寫在最前

    隨着網站的壯大,MySQL數據庫架構一般會經歷一個過程:

wKiom1hc37iQe--XAACBs7m98DM499.png

當我們數據量比較小的時候,一臺單實例數據庫足矣。等我們數據量增大的時候,我們會採用一主多從的數據庫架構來降低我們的讀寫io。當我們某張業務表達到幾百萬上千萬甚至上億時,就應該去進行分表處理。本文演示OneProxy對數據庫實現分表處理,對前端應用是透明的。

 

Part2:環境簡介

HE1:192.168.1.248 Master1

HE3:192.168.1.250 Master2

HE4:192.168.1.251 Oneproxy

 

環境構建

Part1:安裝Oneproxy

Oneproxy的安裝不是本文講述的重點,需要的可移步至

OneProxy實現MySQL讀寫分離與負載均衡

http://suifu.blog.51cto.com/9167728/1884673

 

Part2:proxy.cnf

proxy.cnf文件是oneproxy的主要參數配置文件,新版的oneproxy對整個目錄進行了重新的劃分,配置文件都放在了conf目錄裏

[root@HE4 oneproxy]# cat conf/proxy.conf 
[oneproxy]
keepalive = 1
event-threads = 4
log-file = log/oneproxy.log
pid-file = log/oneproxy.pid
lck-file = log/oneproxy.lck
mysql-version = 5.7.16
proxy-address = :3307
proxy-master-addresses.1 = 192.168.1.248:3306@group1
proxy-master-addresses.2 = 192.168.1.250:3306@group2
proxy-user-list = sys_admin/1C6D087BA5D2607A27DECB2F2AFE247E911E877A@test
proxy-part-tables.1 = /root/oneproxy/conf/part.txt
#proxy-part-tables.2 = /root/oneproxy/conf/part2.txt
proxy-charset = utf8_bin
proxy-group-policy.1 = group1:master-only
proxy-group-policy.2 = group2:master-only
proxy-secure-client = 192.168.1.248
proxy-sequence.1 = default
proxy-httpserver = :8080
proxy-httptitle = OneProxy Monitor

 

Part3:part.txt

part.txt文件是分區策略配置文件,在本博文中,採取hash分區來進行簡單演示

[root@HE4 oneproxy]# cat conf/part.txt 
[
  {
    "table" : "helei",
    "pkey" : "id",
    "type" : "int",
    "method" : "hash",
    "partitions" :     
      [
        { "suffix" : "_0", "group": "group1" },
        { "suffix" : "_1", "group": "group2" },
        { "suffix" : "_2", "group": "group1" },
        { "suffix" : "_3", "group": "group2"}
      ]
  }
]


 

實戰

Part1:啓動OneProxy

[root@HE4 oneproxy]# ./oneproxy.service start
Starting OneProxy ...                                      [  OK  ]

 

Part2:監控頁面

我這裏是兩臺Master

wKioL1hc466zsFDWAAJj-SS3Mng695.jpg

 

 

Part3:創建相關表

登錄oneproxy管理庫創建表

[root@HE1 ~]# mysql -usys_admin -pMANAGER -h192.168.1.251 -P3307 test
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 103
Server version: 5.7.16 OneProxy-Community-5.8.5 (OneXSoft)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create table helei(
    -> id int(10) unsigned NOT NULL AUTO_INCREMENT,
    -> c1 int(10) NOT NULL DEFAULT '0',
    -> c2 int(10) unsigned DEFAULT NULL,
    -> c5 int(10) unsigned NOT NULL DEFAULT '0',
    -> c3 timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    -> c4 varchar(200) NOT NULL DEFAULT '',
    -> PRIMARY KEY(id),
    -> KEY idx_c1(c1),
    -> KEY idx_c2(c2)
    -> )ENGINE=InnoDB ;
Query OK, 0 rows affected (0.27 sec)
mysql> \q

 

Part4:插入數據

[root@HE1 ~]# mysql -usys_admin -pMANAGER -h192.168.1.251 -P3307 test -e"insert into helei(id,c1,c2,c5,c4) values(1,1,1,1,'1')"
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@HE1 ~]# mysql -usys_admin -pMANAGER -h192.168.1.251 -P3307 test -e"insert into helei(id,c1,c2,c5,c4) values(2,2,2,2,'2')"
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@HE1 ~]# mysql -usys_admin -pMANAGER -h192.168.1.251 -P3307 test -e"insert into helei(id,c1,c2,c5,c4) values(3,3,3,3,'3')"
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@HE1 ~]# mysql -usys_admin -pMANAGER -h192.168.1.251 -P3307 test -e"insert into helei(id,c1,c2,c5,c4) values(4,4,4,4,'4')"
mysql: [Warning] Using a password on the command line interface can be insecure.

校驗

Part1:校驗oneproxy表內容

這裏可以看到虛擬表helei中已經具有剛剛插入的內容;

[root@HE1 ~]# mysql -usys_admin -pMANAGER -h192.168.1.251 -P3307 test -e"select * from helei";
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+----+------+----+---------------------+----+
| id | c1 | c2   | c5 | c3                  | c4 |
+----+----+------+----+---------------------+----+
|  4 |  4 |    4 |  4 | 2016-12-23 00:07:21 | 4  |
|  1 |  1 |    1 |  1 | 2016-12-23 16:07:04 | 1  |
|  2 |  2 |    2 |  2 | 2016-12-23 00:07:10 | 2  |
|  3 |  3 |    3 |  3 | 2016-12-23 16:07:16 | 3  |
+----+----+------+----+---------------------+----+


Part2:校驗Master1中的內容

[root@HE1 ~]# mysql -uroot -pMANAGER test
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 158
Server version: 5.7.16-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| checksums      |
| helei_0        |
| helei_2        |
| sbtest         |
+----------------+
4 rows in set (0.00 sec)
mysql> select * from helei_0;
+----+----+------+----+---------------------+----+
| id | c1 | c2   | c5 | c3                  | c4 |
+----+----+------+----+---------------------+----+
|  4 |  4 |    4 |  4 | 2016-12-23 00:07:21 | 4  |
+----+----+------+----+---------------------+----+
1 row in set (0.00 sec)
mysql> select * from helei_2;
+----+----+------+----+---------------------+----+
| id | c1 | c2   | c5 | c3                  | c4 |
+----+----+------+----+---------------------+----+
|  2 |  2 |    2 |  2 | 2016-12-23 00:07:10 | 2  |
+----+----+------+----+---------------------+----+
1 row in set (0.00 sec)


Part3:校驗Master2中的內容

[root@HE3 ~]# mysql -uroot -pMANAGER test
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2997
Server version: 5.7.16-log MySQL Community Server (GPL)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| checksums      |
| helei_1        |
| helei_3        |
+----------------+
3 rows in set (0.00 sec)
mysql> select * from helei_1;
+----+----+------+----+---------------------+----+
| id | c1 | c2   | c5 | c3                  | c4 |
+----+----+------+----+---------------------+----+
|  1 |  1 |    1 |  1 | 2016-12-23 16:07:04 | 1  |
+----+----+------+----+---------------------+----+
1 row in set (0.00 sec)
mysql> select * from helei_3;
+----+----+------+----+---------------------+----+
| id | c1 | c2   | c5 | c3                  | c4 |
+----+----+------+----+---------------------+----+
|  3 |  3 |    3 |  3 | 2016-12-23 16:07:16 | 3  |
+----+----+------+----+---------------------+----+
1 row in set (0.00 sec)

 

 

注意

Warning:警告1

不支持預編譯語句 PreparedStatement,不支持Bind、Execute調用接口。

 

Warning:警告2

不支持使用use命令來切換後端數據庫,use命令可執行,但其含義是切換到不同的MySQL主備集羣,OneProxy在支持分庫分表功能後,就將一個主備集羣視爲一個數據庫了,鏈接Oneproxy時如果指定了數據庫名,則需替換成Server Group的名字

 

Warning:警告3

禁止使用set命令,任何set命令都會直接返回成功,而不做任何處理。

 

Warning:警告4

默認禁止CALL、PREPARE、EXECUTE、DEALLOCATE命令,不支持存儲過程和函數。

 

Warning:警告5

OneProxy支持master進行故障轉移切換,但建議採用流行的高可用方案MHA實現。故障切換後,OneProxy可以自動識別哪臺機器是master。另外,架構必須是一主帶N從,不能是雙主帶N從。

 

 

 

 

——總結——

至此,OneProxy對MySQL的分庫分表測試完成,對於前端應用而言,表名是透明的。無需變更代碼。

 

 

 

參考文獻:

http://hcymysql.blog.51cto.com/5223301/1685146

http://www.onexsoft.com

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章