Cobar的分佈式主要是經過將表放入不一樣的庫來實現:
1.Cobar支持將一張表水平拆分紅多份分別放入不一樣的庫來實現表的水平拆分
2.Cobar也支持將不一樣的表放入不一樣的庫
3.多數狀況下,用戶會將以上兩種方式混合使用
4.Cobar不支持將一張表,例如test表拆分紅test_1, test_2, test_3.....放在同一個庫中,必須將拆分後的表分別放入不一樣的庫來實現分佈式.
缺點:
1.不支持跨庫的關聯操做:join、分頁、排序、子查詢
2.不支持SAVEPOINT操做
3.不支持SET語句的執行,事務和字符集設置語句除外
4.只支持MySQL數據節點
5.對於拆分表,插入操做須給出列名,必須包含拆分字段
環境規劃:
IP 數據庫 表
192.168.1.247 test01 t1
192.168.1.247 test02 t1
192.168.1.247 test03 t1
說明:在本服務器建立三個數據庫,數據庫中建立相同的表和表類型,將t1表中的數據拆分到teat01,02,03數據庫中.java
1.建立數據庫和表
[root@tong1 ~]# /usr/local/mysql-5.6.23/bin/mysql -u root -p
mysql> create database test01; --建立數據庫test01,02,03
Query OK, 1 row affected (0.03 sec)
mysql> create database test02;
Query OK, 1 row affected (0.03 sec)
mysql> create database test03;
Query OK, 1 row affected (0.03 sec)
mysql> \u test01
Database changed
mysql> create table t1(a int,b char(5)); --在三個數據庫建立相同的表
Query OK, 0 rows affected (0.34 sec)
mysql> \u test02
Database changed
mysql> create table t1(a int,b char(5));
Query OK, 0 rows affected (0.31 sec)
mysql> \u test03
Database changed
mysql> create table t1(a int,b char(5));
Query OK, 0 rows affected (0.30 sec)
mysql> show tables;
+------------------+
| Tables_in_test03 |
+------------------+
| t1 |
+------------------+
1 row in set (0.00 sec)
mysql> grant all privileges on *.* to tong@'localhost' identified by 'system';
mysql> flush privileges;
mysql> exit
Bye
[root@tong1 bin]#mysql
2.安裝java開發軟件包
[root@tong1 ~]# tar xvf jdk-7u71-linux-x64.tar.gz -C /usr/local/
[root@tong1 ~]# cd /usr/local/
[root@tong1 local]# chown -R root:root jdk1.7.0_71/
[root@tong1 local]# vim /etc/profile/java.sh --添加環境變量
export PATH=$PATH:/usr/local/protobuf-2.5.0/bin:/usr/local/jdk1.7.0_71/bin
export JAVA_HOME=/usr/local/jdk1.7.0_71/
export CLASS_HOME=/usr/local/jdk1.7.0_71/lib
[root@tong1 local]# source /etc/profile/java.sh
[root@tong1 local]# java -version --查看java是否安裝成功
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)linux
3.下載安裝cobar
下載地址:http://pan.baidu.com/s/1o6igLwY
[root@tong1 ~]# tar xvf cobar-server-1.2.7.tar.gz -C /usr/local/
[root@tong1 ~]# cd /usr/local/cobar-server-1.2.7/
[root@tong1 cobar-server-1.2.7]# ll
drwxr-xr-x. 2 root root 4096 Dec 29 2012 bin
drwxr-xr-x. 2 root root 4096 Dec 29 2012 conf
-rwsrwsrwt. 1 root root 575 Dec 29 2012 COPYRIGHT
drwxr-xr-x. 3 root root 4096 May 14 10:13 lib
-rwsrwsrwt. 1 root root 11549 Dec 29 2012 LICENSE
drwxr-xr-x. 2 root root 4096 Dec 29 2012 logs
-rwsrwsrwt. 1 root root 428 Dec 29 2012 README
[root@tong1 cobar-server-1.2.7]# cd conf/
[root@tong1 conf]# ll
-rw-r--r--. 1 root root 2604 Dec 29 2012 log4j.xml
-rw-r--r--. 1 root root 1262 Dec 29 2012 rule.xml
-rw-r--r--. 1 root root 1966 Dec 29 2012 schema.xml --mysql數據庫的IP,端口
-rw-r--r--. 1 root root 2292 Dec 29 2012 server.xml
[root@tong1 conf]# vim schema.xml
<!-- schema定義 -->
<schema name="test" dataNode="test1"> --test架構名,用於用戶登錄,test1第一個數據庫
<table name="t1" dataNode="test2,test3" rule="rule1" /> --t1是表名,拆分的表,test2,test3是兩個數據庫名
</schema>
<!-- 數據節點定義,數據節點由數據源和其餘一些參數組織而成。-->
<dataNode name="test1"> --test1是第一個數據庫
<property name="dataSource">
<dataSourceRef>test[0]</dataSourceRef> --第一個數據庫源
</property>
</dataNode>
<dataNode name="test2"> --第二個數據庫
<property name="dataSource">
<dataSourceRef>test[1]</dataSourceRef>
</property>
</dataNode>
<dataNode name="test3"> --第三個數據庫
<property name="dataSource">
<dataSourceRef>test[2]</dataSourceRef>
</property>
</dataNode>
<!-- 數據源定義,數據源是一個具體的後端數據鏈接的表示。-->
<dataSource name="test" type="mysql"> --test源數據,用於用戶登錄
<property name="location">
<location>192.168.1.247:3306/test1</location> --三個數據庫服務器和數據庫名,數據庫也可在不一樣的服務器上
<location>192.168.1.247:3306/test2</location>
<location>192.168.1.247:3306/test3</location>
</property>
<property name="user">tong</property> --用戶登錄
<property name="password">system</property> --密碼
<property name="sqlMode">STRICT_TRANS_TABLES</property>
</dataSource>
[root@tong1 conf]# vim rule.xml
<!-- 路由規則定義,定義什麼表,什麼字段,採用什麼路由算法 -->
<tableRule name="rule1">
<rule>
<columns>id</columns>
<algorithm><![CDATA[ func1(${id}) ]]></algorithm>
</rule>
</tableRule>
<!-- 路由函數定義 -->
<function name="func1" class="com.alibaba.cobar.route.function.PartitionByLong">
<property name="partitionCount">2</property>
<property name="partitionLength">512</property>
[root@tong1 conf]# vim server.xml
<!-- 系統參數定義,服務端口、管理端口,處理器個數、線程池等。 -->
<system>
<property name="serverPort">8066</property> --cobar服務啓動端口
<property name="managerPort">9066</property> --管理端口
<property name="initExecutor">16</property>
<property name="timerExecutor">4</property>
<property name="managerExecutor">4</property>
<property name="processors">4</property>
<property name="processorHandler">8</property>
<property name="processorExecutor">8</property>
<property name="clusterHeartbeatUser">_HEARTBEAT_USER_</property>
<property name="clusterHeartbeatPass">_HEARTBEAT_PASS_</property>
</system>
<!-- 用戶訪問定義,用戶名、密碼、schema等信息。 -->
<user name="tong"> --登錄用戶名
<property name="password">system</property> --密碼
<property name="schemas">test</property> --架構名,用戶鏈接的數據庫
</user>
[root@tong1 bin]# ./startup.sh --啓動服務
"/usr/local/jdk1.7.0_71/bin/java" -Dcobar.home="/usr/local/cobar-server-1.2.7" -classpath "/usr/local/cobar-server-1.2.7/conf:/usr/local/cobar-server-1.2.7/lib/classes:/usr/local/cobar-server-1.2.7/lib/cobar-server-1.2.7.jar:/usr/local/cobar-server-1.2.7/lib/log4j-1.2.16.jar" -server -Xms1024m -Xmx1024m -Xmn256m -Xss256k -XX:+AggressiveOpts -XX:+UseBiasedLocking -XX:+UseFastAccessorMethods -XX:+DisableExplicitGC -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=75 com.alibaba.cobar.CobarStartup >> "/usr/local/cobar-server-1.2.7/logs/console.log" 2>&1 &
[root@tong1 bin]# cat ../logs/stdout.log --查看日誌
15:33:02,933 INFO ===============================================
15:33:02,934 INFO Cobar is ready to startup ...
15:33:02,934 INFO Startup processors ...
15:33:03,026 INFO Startup connector ...
15:33:03,031 INFO Initialize dataNodes ...
15:33:03,051 INFO test2:0 init success
15:33:03,053 INFO test1:0 init success
15:33:03,055 INFO test3:0 init success
15:33:03,066 INFO CobarManager is started and listening on 9066
15:33:03,068 INFO CobarServer is started and listening on 8066
15:33:03,071 INFO ===============================================
[root@tong1 bin]# cat ../logs/console.log
log4j:WARN 2015-05-14 15:33:02 [/usr/local/cobar-server-1.2.7/conf/log4j.xml] load completed.[root@tong1 bin]# netstat -antup | grep java
tcp 0 0 :::8066 :::* LISTEN 25359/java
tcp 0 0 :::9066 :::* LISTEN 25359/java
tcp 0 0 ::ffff:192.168.1.247:52451 ::ffff:192.168.1.247:3306 ESTABLISHED 25359/java
tcp 0 0 ::ffff:192.168.1.247:52450 ::ffff:192.168.1.247:3306 ESTABLISHED 25359/java
tcp 0 0 ::ffff:192.168.1.247:52452 ::ffff:192.168.1.247:3306 ESTABLISHED 25359/java
[root@tong1 bin]#算法
4.登錄數據庫插入數據(如下紅色部分不能少)
[root@tong1 data]# /usr/local/mysql-5.6.23/bin/mysql -h 192.168.1.247 -utong -p -P8066 -Dtest
mysql> show databases; --客戶端鏈接的數據庫名
+----------+
| DATABASE |
+----------+
| test |
+----------+
1 row in set (0.00 sec)
mysql> \u test
Database changed
mysql> show tables; --表名
+----------------+
| Tables_in_test |
+----------------+
| t1 |
+----------------+
2 rows in set (0.00 sec)
mysql> insert into t1 values(1,'c'); --插入數據
Query OK, 2 rows affected (0.14 sec)
mysql> insert into t1 values(2,'z');
Query OK, 2 rows affected (0.20 sec)
mysql> select * from t1; --不知道爲何test1數據庫中有重複的數據
+------+------+
| a | b |
+------+------+
| 1 | c |
| 2 | z |
| 1 | c |
| 2 | z |
+------+------+
4 rows in set (0.01 sec)
mysql>
5.在另外兩個數據庫中查看數據
[root@tong1 bin]# /usr/local/mysql-5.6.23/bin/mysql -u root -p -D test2 --在test2查看數據
mysql> select * from t1; --有數據
+------+------+
| a | b |
+------+------+
| 1 | c |
| 2 | z |
+------+------+
3 rows in set (0.00 sec)
mysql> select * from test3.t1; --在test3數據庫查看數據
+------+------+
| a | b |
+------+------+
| 1 | c |
| 2 | z |
+------+------+
3 rows in set (0.00 sec)
mysql>sql