生活随笔
收集整理的這篇文章主要介紹了
Cobar部署与测试
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原文出自 ?http://www.abcdocker.com/abcdocker/84
Cobar部署與測試
一、Cobar介紹
1.1功能概述:
Cobar是關系型數據的分布式處理系統,它可以在分布式的環境下看上去像傳統數據庫一樣為您提供海量數據服務。
1.1.1 Cobar核心功能:
???分布式:Cobar的分布式主要是通過將表放入不同的庫來實現:
1. Cobar支持將一張表水平拆分成多份分別放入不同的庫來實現表的水平拆分。
2. Cobar也支持將不同的表放入不同的庫。
3.?多數情況下,用戶會將以上兩種方式混合使用這里需要強調的是,Cobar不支持將一張表,例如test表拆分成test_1, test_2, test_3…..放在同一個庫中,必須將拆分后的表分別放入不同的庫來實現分布式。
?
???HA:
在用戶配置了MySQL心跳的情況下,Cobar可以自動向后端連接的MySQL發送心跳,判斷MySQL運行狀況,一旦運行出現異常,Cobar可以自動切換到備機工作。但需要強調的是:
1. Cobar的主備切換有兩種觸發方式,一種是用戶手動觸發,一種是Cobar的心跳語句檢測到異常后自動觸發。那么,當心跳檢測到主機異常,切換到備機,如果主機恢復了,需要用戶手動切回主機工作,Cobar不會在主機恢復時自動切換回主機,除非備機的心跳也返回異常。
2. Cobar只檢查MySQL主備異常,不關心主備之間的數據同步,因此用戶需要在使用Cobar之前在MySQL主備上配置雙向同步,詳情可以參閱MySQL參考手冊。
1.1.2 Cobar的注意事項
1.請注意表的拆分方式,一張表水平拆分多份到不同的庫中,而不是放入同一個庫中。
2.如果使用HA功能,請在MySQL主備之間配置雙向同步
1.1.3目錄結構
1.1.3.1基本目錄
解壓cobar安裝包后,可以看到以下目錄:
bin #包含Cobar的啟動、重啟、停止等腳本文件
conf #包含Cobar所有配置文件
lib #包含Cobar及其依賴的jar文件
logs #包含Cobar所有日志文件
1.1.3.2啟動腳本
Cobar的所有啟動停止腳本全部放在bin目錄中,進入bin目錄,可以看到:
startup.sh???#Linux環境啟動腳本
startup.bat??#Windows環境啟動腳本
restart.sh ????#Linux環境重啟腳本
shutdown.sh ?#Linux環境停止腳本
1.1.3.3配置文件
Cobar的所有配置文件全部放在conf目錄中,進入conf目錄,可以看到:
server.xml ???#Cobar系統、用戶、集群等相關配置rule.xml ?????#分布式規則定義log4j.xml??? ?#日志相關配置schema.xml???#schema,dataNode,dataSource相關配置,其中:dataSource:數據源,表示一個具體的數據庫連接,與一個物理存在的schema一一對應。dataNode:數據節點,由主、備數據源,數據源的HA以及連接池共同組成,可以將一個dataNode理解為一個分庫。schema:cobar可以定義包含拆分表的schema(如schema1),也可以定義無拆分表的schema(如schema2)。
1.1.3.4配置文件詳解:
略!請見官方文檔!!!
二、Cobar配置及安裝
下面我們將使用一個最簡單的分庫分表的例子來說明Cobar的基本用法,數據庫schema如下圖(該實例也可參考:Cobar產品首頁)。
1.系統對外提供的數據庫名是dbtest,并且其中有兩張表tb1和tb2。
2.tb1表的數據被映射到物理數據庫dbtest1的tb1上。
3.tb2表的一部分數據被映射到物理數據庫dbtest2的tb2上,另外一部分數據被映射到物理數據庫dbtest3的tb2上。
2.1環境準備
操作系統:Linux?Centos6.7MySQL:5.5.49版本JDL:jdk-8u60-linux-x64.tarCobar:cobar-server-1.2.7.tar
2.2分布式環境部署
(1)提前安裝mysql,并提前在mysql /data/3306實例創建dbtest1庫與tb1表,dbtest2庫與tb2表,dbtest3庫與tb2表,并且授權。
mysql?-uroot?-poldboy123?-S?/data/3306/mysql.sockmysql>?grant all privileges on?*.*?to?'cobar'@'172.16.1.%'?identified by?'oldboy123';mysql>?flush privileges;create database dbtest1;create database dbtest2;create database dbtest3;use dbtest1;create table tb1(id?int(4)?not null,name?char(20)?not null,age tinyint(2)?NOT NULL?default?'0',dept varchar(16)?default?NULL,primary key(id),KEY index_name(name));use dbtest2;create table tb2?(id?int(4)?NOT NULL,name?char(20)?NOT NULL,PRIMARY KEY?(id));use dbtest3;create table tb2?(id?int(4)?NOT NULL,name?char(20)?NOT NULL,PRIMARY KEY?(id));
上傳jdk和cobar的gz包到/home/oldboy/tools
(2)配置java環境
cd?/application/tools/tar xf jdk-8u60-linux-x64.tar.gz?-C?/application/ln?-s?/application/jdk1.8.0_60?/application/jdksed?-i.ori?'$a export JAVA_HOME=/application/jdk\nexport PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH\nexport CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar'?/etc/profilesource?/etc/profile
(3)配置cobar環境
cd?/home/oldboy/toolstar xf cobar-server-1.2.7.tar.gzcp?-a cobar-server-1.2.7?/application/mv?/application/cobar-server-1.2.7?/application/cobarcd?/application/cobar/cd conf
(4)配置cobar配置文件
1.配置schema.xml文件
[root@db02 conf]#?cat schema.xml<?xml version="1.0"?encoding="UTF-8"?><!--?-?Copyright?1999-2012?Alibaba?Group.?-??-?Licensed?under the?Apache?License,?Version?2.0?(the?"License");?-?you may not use?this?file except in compliance with the?License.?-?You?may obtain a copy of the?License?at?-??-????? http://www.apache.org/licenses/LICENSE-2.0?-??-?Unless?required by applicable law or agreed to in writing,?software?-?distributed under the?License?is distributed on an?"AS IS"?BASIS,?-?WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,?either express or implied.?-?See?the?License?for?the specific language governing permissions and?-?limitations under the?License.--><!DOCTYPE cobar:schema SYSTEM?"schema.dtd"><cobar:schema xmlns:cobar="http://cobar.alibaba.com/">???<!--?schema定義?-->??<schema name="dbtest"?dataNode="dnTest1">??#這條規則意思是dbtest主要映射的是dnTest1庫,也就是下面172.16.1.52:3306/dbtest1庫????<table name="tb2"?dataNode="dnTest2,dnTest3"?rule="rule1"?/>??#tb2表則是按照規則rule1,被分配到dnTest2庫(即172.16.1.52:3306/dbtest2庫)和dnTest3庫(即172.16.1.52:3306/dbtest3庫)中??</schema>???<!--?數據節點定義,數據節點由數據源和其他一些參數組織而成。-->??<dataNode name="dnTest1">?????<property?name="dataSource">??????<dataSourceRef>dsTest[0]</dataSourceRef>????</property>??</dataNode>??<dataNode name="dnTest2">????<property?name="dataSource">??????<dataSourceRef>dsTest[1]</dataSourceRef>????</property>??</dataNode>??<dataNode name="dnTest3">????<property?name="dataSource">??????<dataSourceRef>dsTest[2]</dataSourceRef>????</property>??</dataNode>???<!--?數據源定義,數據源是一個具體的后端數據連接的表示。-->??<dataSource name="dsTest"?type="mysql">????<property?name="location">??????<location>172.16.1.52:3306/dbtest1</location>??#數據庫的IP和端口號??????<location>172.16.1.52:3306/dbtest2</location>??????<location>172.16.1.52:3306/dbtest3</location>????</property>????<property?name="user">cobar</property>??#數據庫給cobar的用戶名????<property?name="password">oldboy123</property>??#數據庫給cobar的密碼????<property?name="sqlMode">STRICT_TRANS_TABLES</property>??</dataSource>?</cobar:schema>
2.配置rule.xml文件
[root@db02 conf]#?cat rule.xml<?xml version="1.0"?encoding="UTF-8"?><!--?-?Copyright?1999-2012?Alibaba?Group.?-??-?Licensed?under the?Apache?License,?Version?2.0?(the?"License");?-?you may not use?this?file except in compliance with the?License.?-?You?may obtain a copy of the?License?at?-??-????? http://www.apache.org/licenses/LICENSE-2.0?-??-?Unless?required by applicable law or agreed to in writing,?software?-?distributed under the?License?is distributed on an?"AS IS"?BASIS,?-?WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,?either express or implied.?-?See?the?License?for?the specific language governing permissions and?-?limitations under the?License.--><!DOCTYPE cobar:rule SYSTEM?"rule.dtd"><cobar:rule xmlns:cobar="http://cobar.alibaba.com/">???<!--?路由規則定義,定義什么表,什么字段,采用什么路由算法?-->??<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>??#需要注意的就是這兩行,對數據庫插入大于512的會存儲至dnTest3對應的庫表中????<property?name="partitionLength">512</property>??</function>?</cobar:rule>3.配置server.xml文件[root@db02 conf]#?cat server.xml<?xml version="1.0"?encoding="UTF-8"?><!--?-?Copyright?1999-2012?Alibaba?Group.?-??-?Licensed?under the?Apache?License,?Version?2.0?(the?"License");?-?you may not use?this?file except in compliance with the?License.?-?You?may obtain a copy of the?License?at?-??-????? http://www.apache.org/licenses/LICENSE-2.0?-??-?Unless?required by applicable law or agreed to in writing,?software?-?distributed under the?License?is distributed on an?"AS IS"?BASIS,?-?WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,?either express or implied.?-?See?the?License?for?the specific language governing permissions and?-?limitations under the?License.--><!DOCTYPE cobar:server SYSTEM?"server.dtd"><cobar:server xmlns:cobar="http://cobar.alibaba.com/">???<!--?系統參數定義,服務端口、管理端口,處理器個數、線程池等。?-->??<!--??<system>????<property?name="serverPort">8066</property>????<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="cobar">????<property?name="password">oldboy123</property>????<property?name="schemas">dbtest</property>??</user>??<!--??<user name="root">????<property?name="password">oldboy123</property>??</user>??-->???<!--?集群列表定義,指定集群節點的主機和權重,用于集群間的心跳和客戶端負載均衡。?-->??<!--??<cluster>????<node name="cobar1">??????<property?name="host">127.0.0.1</property>??????<property?name="weight">1</property>????</node>??</cluster>???-->????<!--?隔離區定義,可以限定某個主機上只允許某個用戶登錄。?-->??<!--??<quarantine>????<host name="1.2.3.4">??????<property?name="user">test</property>????</host>??</quarantine>??-->?</cobar:server>#這里定義的帳號密碼就是命令行登錄數據庫的帳號密碼
登錄命令:
mysql?-ucobar?-poldboy123?-h172.16.1.52?-P8066
上面的說明:用戶就是server.xml定義的用戶cobar,密碼也是配置文件里面的密碼,cobar是用8066端口登錄的,這個也是啟用的java進程的端口號。
2.3分庫分表測試階段
啟動cobar:/application/cobar/bin/startup.sh查看啟動結果:[root@db02 conf]#?ps?-ef|grep cobarroot?????10219?????1??0?18:17??????????00:00:16?/application/jdk/bin/java?-Dcobar.home=/application/cobar?-classpath?/application/cobar/conf:/application/cobar/lib/classes:/application/cobar/lib/cobar-server-1.2.7.jar:/application/cobar/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.CobarStartuproot?????10251?????1??0?18:47??????????00:00:00?grep?--color=auto?cobar[root@db02 conf]#?netstat?-tulnap?|grep?8066tcp6???????0??????0?:::8066?????????????????:::*??????????????????? LISTEN??????10219/java使用cobar登錄,簡單測試一下:[root@db02 conf]#?mysql?-ucobar?-poldboy123?-h172.16.1.52?-P8066進入數據庫執行測試操作:mysql>?show databases;+----------+|?DATABASE?|+----------+|?dbtest???|+----------+mysql>?use dbtest;Database?changedmysql>?show tables;+------------------+|?Tables_in_dbtest?|+------------------+|?tb2??????????????||?tb1??????????????|+------------------+mysql>?insert into tb2(id,name)?values(55,'oldgirl');??mysql>?insert into tb2(id,name)?values(520,'oldboy');????#520大于512,因此會被存儲到3306實例的dbtest3庫的tb2表下mysql>?quit然后進入3306實例里面查看:[root@db02 conf]#?mysql?-uroot?-poldboy123?-S?/data/3306/mysql.sockmysql>?show databases;+--------------------+|?Database???????????|+--------------------+|?information_schema?||?dbtest?????????????||?dbtest1????????????||?dbtest2????????????||?dbtest3????????????||?mysql??????????????||?oldboy?????????????||?performance_schema?|+--------------------+mysql>?use dbtest3Database?changedmysql>?show tables;+-------------------+|?Tables_in_dbtest3?|+-------------------+|?tb2???????????????|+-------------------+mysql>?select?*?from tb2;+-----+--------+|?id??|?name???|+-----+--------+|?520?|?oldboy?|+-----+--------+果然只有520存儲到3306實例的dbtest3庫的tb2表下
?
添加一個實例測試:
啟動新實例:[root@db02 conf]#?data/3308/mysql start登錄數據庫:[root@db02 conf]#?mysql?-uroot?-poldboy123?-S?/data/3308/mysql.sockmysql>?grant all privileges on?*.*?to?'cobar'@'172.16.1.%'?identified by?'oldboy123';mysql>?flush privileges;create database dbtest1;create database dbtest2;create database dbtest3;use dbtest1;create table tb1(id?int(4)?not null,name?char(20)?not null,age tinyint(2)?NOT NULL?default?'0',dept varchar(16)?default?NULL,primary key(id),KEY index_name(name));use dbtest2;create table tb2?(id?int(4)?NOT NULL,name?char(20)?NOT NULL,PRIMARY KEY?(id));use dbtest3;create table tb2?(id?int(4)?NOT NULL,name?char(20)?NOT NULL,PRIMARY KEY?(id));修改cobar的schema.xml配置文件:[root@db02 conf]#?cat schema.xml<?xml version="1.0"?encoding="UTF-8"?><!--?-?Copyright?1999-2012?Alibaba?Group.?-??-?Licensed?under the?Apache?License,?Version?2.0?(the?"License");?-?you may not use?this?file except in compliance with the?License.?-?You?may obtain a copy of the?License?at?-??-http://www.apache.org/licenses/LICENSE-2.0?-??-?Unless?required by applicable law or agreed to in writing,?software?-?distributed under the?License?is distributed on an?"AS IS"?BASIS,?-?WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,?either express or implied.?-?See?the?License?for?the specific language governing permissions and?-?limitations under the?License.--><!DOCTYPE cobar:schema SYSTEM?"schema.dtd"><cobar:schema xmlns:cobar="http://cobar.alibaba.com/">?<!--?schema定義?--><schema name="dbtest"?dataNode="dnTest1"><table name="tb2"?dataNode="dnTest2,dnTest3"?rule="rule1"?/></schema>?<!--?數據節點定義,數據節點由數據源和其他一些參數組織而成。--><dataNode name="dnTest1"><property?name="dataSource"><dataSourceRef>dsTest[0]</dataSourceRef></property></dataNode><dataNode name="dnTest2"><property?name="dataSource"><dataSourceRef>dsTest[1]</dataSourceRef></property></dataNode><dataNode name="dnTest3"><property?name="dataSource"><dataSourceRef>dsTest[2]</dataSourceRef></property></dataNode>?<!--?數據源定義,數據源是一個具體的后端數據連接的表示。--><dataSource name="dsTest"?type="mysql"><property?name="location"><location>172.16.1.52:3306/dbtest1</location><location>172.16.1.52:3306/dbtest2</location><location>172.16.1.52:3308/dbtest3</location></property><property?name="user">cobar</property><property?name="password">oldboy123</property><property?name="sqlMode">STRICT_TRANS_TABLES</property></dataSource>?</cobar:schema>
在登錄cobar用戶:
[root@db02 conf]#?mysql?-ucobar?-poldboy123?-h172.16.1.52?-P8066
mysql>?insert into tb2(id,name)?values(555,’qiangge’);
!!!!!#在插入信息的時候一定要在表名后面加(id,name),不然不能實現分布式儲存的,只會同時寫入兩個表中,以至于成為雙寫。
再登錄3308實例查看是否555,’qiangge’插入到dbtest3的tb2中:
[root@db02 conf]#?mysql?-uroot?-poldboy123?-S?/data/3308/mysql.sockmysql>?use dbtest3;mysql>?select?*?from tb2;+-----+----------+|?id|?name ????|+-----+----------+|?555?|?qiangge2?|+-----+----------+
總結
以上是生活随笔為你收集整理的Cobar部署与测试的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。