運行環境:
- 主機1:Ubuntu14.04 Desktop + MySQL5.5 + JDK 1.7(HP Z400) 內網IP地址:192.168.137.8
- NODE1:Ubuntu 13.04 server + MySQL5.5 內網IP地址:192.168.137.31
- NODE2:Ubuntu 13.04 server + MySQL5.5 內網IP地址:192.168.137.32
注:(NODE1和NODE2運行於XEN虛擬化平臺,硬件環境HP Z800) python
Cobar簡介:
Cobar是關係型數據庫的分佈式處理系統,它能夠在分佈式的環境下看上去像傳統數據庫同樣爲您提供海量數據服務。
- 產品在阿里巴巴B2B公司已經穩定運行了3年以上。
- 目前已經接管了3000+個MySQL數據庫的schema,爲應用提供數據服務。
- 據最近統計cobar集羣目前平均天天處理近50億次的SQL執行請求。
Cobar最主要解決的問題是:分佈式和HA。
分佈式:主要是經過將同一個表的數據拆分紅多個,放入不一樣的數據庫實例,查詢的時候也會按照一樣的操做方式,來更新具體數據庫實例中的對應的數據。 mysql
HA:高可用性,在設置了MYSQL心跳的狀況下,若是主數據庫發生了異常,Cobar會自動鏈接從數據庫,若是主數據庫恢復正常,只能手動恢復到主數據庫。Cobar只負責切換數據庫實例,不負責主從數據庫的同步,因此須要提早將主從數據庫設置雙向同步。 git
存在的不足:
- (1).不支持跨庫狀況下的join、分頁、排序、子查詢操做。
- (2).SET語句執行會被忽略,事務和字符集設置除外。
- (3).分庫狀況下,insert語句必須包含拆分字段列名。
- (4).分庫狀況下,update語句不能更新拆分字段的值。
- (5).不支持SAVEPOINT操做。
- (6).暫時只支持MySQL數據節點。
- (7).使用JDBC時,不支持rewriteBatchedStatements=true參數設置(默認爲false)。
- (8).使用JDBC時,不支持useServerPrepStmts=true參數設置(默認爲false)。
- (9).使用JDBC時,BLOB, BINARY, VARBINARY字段不能使用setBlob()或setBinaryStream()方法設置參數。
固然,若是想努力實現這些功能,能夠fork官方的源碼:https://github.com/alibaba/cobar github
環境搭建
- Cobar服務器:192.168.137.8:8066 用戶名/密碼:root/sa 實例名:dbtest
- 主機1:192.168.137.8:3306 用戶名/密碼:cobar/sa 實例名:dbtest1
- Node1:192.168.137.31:3306 用戶名/密碼:cobar/sa 實例名:dbtest2
- Node2:192.168.137.32:3306 用戶名/密碼:cobar/sa 實例名:dbtest3
Cobar-Server-1.2.7版本下載:http://pan.baidu.com/s/1pJudQh9 算法
實驗拓撲圖以下: sql
首先分別在三個主機建立數據庫實例:
02 |
dropdatabaseif exists dbtest1; |
03 |
createdatabasedbtest1; |
10 |
dropdatabaseif exists dbtest2; |
11 |
createdatabasedbtest2; |
18 |
dropdatabaseif exists dbtest3; |
19 |
createdatabasedbtest3; |
Cobar配置:
schema.xml配置以下 數據庫
01 |
<!DOCTYPE cobar:schema SYSTEM "schema.dtd"> |
04 |
<schemaname="dbtest"dataNode="dnTest1"> |
05 |
<tablename="tb2"dataNode="dnTest2,dnTest3"rule="rule1"/> |
07 |
<!-- 數據節點定義,數據節點由數據源和其餘一些參數組織而成。--> |
08 |
<dataNodename="dnTest1"> |
09 |
<propertyname="dataSource"> |
10 |
<dataSourceRef>dsTest[0]</dataSourceRef> |
13 |
<dataNodename="dnTest2"> |
14 |
<propertyname="dataSource"> |
15 |
<dataSourceRef>dsTest[1]</dataSourceRef> |
18 |
<dataNodename="dnTest3"> |
19 |
<propertyname="dataSource"> |
20 |
<dataSourceRef>dsTest[2]</dataSourceRef> |
23 |
<!-- 數據源定義,數據源是一個具體的後端數據鏈接的表示。--> |
24 |
<dataSourcename="dsTest"type="mysql"> |
25 |
<propertyname="location"> |
26 |
<location>192.168.137.8:3306/dbtest1</location> |
27 |
<location>192.168.137.31:3306/dbtest2</location> |
28 |
<location>192.168.137.32:3306/dbtest3</location> |
30 |
<propertyname="user">cobar</property> |
31 |
<propertyname="password">sa</property> |
32 |
<propertyname="sqlMode">STRICT_TRANS_TABLES</property> |
server.xml簡單配置 後端
1 |
<!DOCTYPE cobar:server SYSTEM "server.dtd"> |
3 |
<!-- 用戶訪問定義,用戶名、密碼、schema等信息。 --> |
5 |
<propertyname="password">sa</property> |
6 |
<propertyname="schemas">dbtest</property> |
rule.xml配置 數組
01 |
<!DOCTYPE cobar:rule SYSTEM "rule.dtd"> |
03 |
<!-- 路由規則定義,定義什麼表,什麼字段,採用什麼路由算法 --> |
04 |
<tableRulename="rule1"> |
06 |
<columns>val</columns> |
07 |
<algorithm><![CDATA[ func2(${val}) ]]></algorithm> |
11 |
<functionname="func1"class="com.alibaba.cobar.route.function.PartitionByLong"> |
12 |
<propertyname="partitionCount">2</property> |
13 |
<propertyname="partitionLength">512</property> |
16 |
<functionname="func2"class="com.alibaba.cobar.route.function.PartitionByString"> |
17 |
<propertyname="partitionCount">2</property> |
18 |
<propertyname="partitionLength">512</property> |
19 |
<propertyname="hashSlice">-5:</property> |
這裏須要說明,INSERT語句中必須包含路由規則定義的字段,不然Cobar不會對數據進行拆分,同時存儲到多個數據庫實例中,好比上面的tb2表中,id字段若是設置了auto_increment,插入語句中不用指明id字段,這樣就沒法達到數據包拆分的目的。 服務器
準備完成以後直接運行bin目錄下的./startup.sh便可。
而後查看輸入的日誌:
01 |
yan@yan-Z400:~/cobar-server-1.2.7/logs$ tail -f stdout.log |
02 |
09:57:00,155 INFO Cobar is ready to startup ... |
03 |
09:57:00,155 INFO Startup processors ... |
04 |
09:57:00,198 INFO Startup connector ... |
05 |
09:57:00,202 INFO Initialize dataNodes ... |
06 |
09:57:00,811 INFO dnTest1:0 init success |
07 |
09:57:00,816 INFO dnTest3:0 init success |
08 |
09:57:00,821 INFO dnTest2:0 init success |
09 |
09:57:00,835 INFO CobarManager is started and listening on 9066 |
10 |
09:57:00,837 INFO CobarServer is started and listening on 8066 |
11 |
09:57:00,837 INFO =============================================== |
這樣cobar服務端就已經啓動。
直接使用jdbc或mysql終端鏈接cobar:
01 |
yan@yan-Z400:~$ mysql -uroot -psa -P8066 -h192.168.137.8 |
02 |
Welcome to the MySQL monitor. Commands end with ; or \g. |
03 |
Your MySQL connection id is 1 |
04 |
Server version: 5.1.48-cobar-1.2.7 Cobar Server (ALIBABA) |
06 |
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. |
08 |
Oracle is a registered trademark of Oracle Corporation and/or its |
09 |
affiliates. Other names may be trademarks of their respective |
12 |
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. |
下面使用幾句Pyhton進行數據庫的插入操做:
06 |
cxn=MySQLdb.Connect(host='192.168.137.8',port=8066, user='root', passwd='sa') |
10 |
cur.execute("USE dbtest") |
13 |
cur.execute("INSERT INTO tb2 (val) values ('this is a test record %d')"%i) |
14 |
print'insert the %d record into cobar'%i |
插入後查看數據庫dbtest2和數據dbtest3的狀況:
能夠看到有100條數據插入了dbtest2,99條數據插入了dbtest3。
後面會對Cobar進行更深刻的瞭解。個人Fork分支
(完)