原文地址: http://code.alibabatech.com/wiki/display/cobar/Home;jsessionid=779959E690AE94BBC8079BB8F7D8B244
概述
Cobar是关系型数据的分布式处理系统,它可以在分布式的环境下看上去像传统数据库一样为您提供海量数据服务。
- 产品在阿里巴巴B2B公司已经稳定运行了3年以上。
- 目前已经接管了3000+个MySQL数据库的schema,为应用提供数据服务。
- 据最近统计cobar集群目前平均每天处理近50亿次的SQL执行请求。
快速启动
场景描述
- 系统对外提供的数据库名是dbtest,并且其中有两张表tb1和tb2。
- tb1表的数据被映射到物理数据库dbtest1的tb1上。
- tb2表的一部分数据被映射到物理数据库dbtest2的tb2上,另外一部分数据被映射到物理数据库dbtest3的tb2上。
如下图所示:
步骤一:环境准备
-
软件准备
操作系统: Linux或者Windows (推荐在Linux环境下运行Cobar)
MySQL: http://www.mysql.com/downloads/ (推荐使用5.1以上版本)
JDK: http://www.oracle.com/technetwork/java/javase/downloads/ (推荐使用1.6以上版本)
Cobar: http://code.alibabatech.com/wiki/display/cobar/release/ (下载tar.gz或者zip文件) -
数据准备
假设本文MySQL所在服务器IP为192.168.0.1,端口为3306,用户名为test,密码为空,我们需要创建schema:dbtest1、dbtest2、dbtest3,table:tb1、tb2,脚本如下:
数据库创建脚本#创建dbtest1dropdatabaseif exists dbtest1;createdatabasedbtest1;use dbtest1;#在dbtest1上创建tb1createtabletb1(idintnotnull,gmt datetime);#创建dbtest2dropdatabaseif exists dbtest2;createdatabasedbtest2;use dbtest2;#在dbtest2上创建tb2createtabletb2(idintnotnull,valvarchar(256));#创建dbtest3dropdatabaseif exists dbtest3;createdatabasedbtest3;use dbtest3;#在dbtest3上创建tb2createtabletb2(idintnotnull,valvarchar(256));
步骤二:部署和配置Cobar
| 请确保机器上设置了JAVA环境变量JAVA_HOME |
- 下载Cobar压缩文件并解压,进入conf目录可以看到schema.xml, rule.xml, server.xml等相关的配置文件
wget http://code.alibabatech.com/mvn/releases/com/alibaba/cobar/cobar-server/1.2.4/cobar-server-1.2.4.tar.gztarzxf cobar-server-1.2.4.tar.gzcdcobar-server-1.2.4#可以看到bin,conf,lib,logs四个目录
- schema.xml配置如下(注意:schema.xml包含MySQL的IP、端口、用户名、密码等配置,您需要按照注释替换为您的MySQL信息。)
schema.xml 配置
<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPE cobar:schema SYSTEM "schema.dtd"><!-- schema定义 --><schemaname="dbtest"dataNode="dnTest1"><tablename="tb2"dataNode="dnTest2,dnTest3"rule="rule1"/></schema><!-- 数据节点定义,数据节点由数据源和其他一些参数组织而成。--><dataNodename="dnTest1"><propertyname="dataSource"><dataSourceRef>dsTest[0]</dataSourceRef></property></dataNode><dataNodename="dnTest2"><propertyname="dataSource"><dataSourceRef>dsTest[1]</dataSourceRef></property></dataNode><dataNodename="dnTest3"><propertyname="dataSource"><dataSourceRef>dsTest[2]</dataSourceRef></property></dataNode><!-- 数据源定义,数据源是一个具体的后端数据连接的表示。--><dataSourcename="dsTest"type="mysql"><propertyname="location"><location>192.168.0.1:3306/dbtest1</location><!--注意:替换为您的MySQL IP和Port--><location>192.168.0.1:3306/dbtest2</location><!--注意:替换为您的MySQL IP和Port--><location>192.168.0.1:3306/dbtest3</location><!--注意:替换为您的MySQL IP和Port--></property><propertyname="user">test</property><!--注意:替换为您的MySQL用户名--><propertyname="password"></property><!--注意:替换为您的MySQL密码--><propertyname="sqlMode">STRICT_TRANS_TABLES</property></dataSource></cobar:schema> - rule.xml配置如下(本文仅以数字类型的id字段作为拆分字段,将数据拆分到两个库中。)
rule.xml 配置
<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPE cobar:rule SYSTEM "rule.dtd"><!-- 路由规则定义,定义什么表,什么字段,采用什么路由算法。--><tableRulename="rule1"><rule><columns>id</columns><algorithm><![CDATA[ func1(${id})]]></algorithm></rule></tableRule><!-- 路由函数定义,应用在路由规则的算法定义中,路由函数可以自定义扩展。--><functionname="func1"class="com.alibaba.cobar.route.function.PartitionByLong"><propertyname="partitionCount">2</property><propertyname="partitionLength">512</property></function></cobar:rule>
- server.xml配置如下
server.xml 配置
<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPE cobar:server SYSTEM "server.dtd"><!--定义Cobar用户名,密码--><username="test"><propertyname="password">test</property><propertyname="schemas">dbtest</property></user></cobar:server>
步骤三:启动和使用Cobar
- 启动Cobar,进入bin目录可以看到Cobar的启动、停止与重启脚本
./startup.sh#Cobar进程名为CobarStartup - 查看logs目录下stdout.log, 启动成功日志如下
10:54:19,264INFO ===============================================10:54:19,265INFO Cobar is ready to startup ...10:54:19,265INFO Startup processors ...10:54:19,443INFO Startup connector ...10:54:19,446INFO Initialize dataNodes ...10:54:19,470INFO dnTest1:0init success10:54:19,472INFO dnTest3:0init success10:54:19,473INFO dnTest2:0init success10:54:19,481INFO CobarManager is started and listening on906610:54:19,483INFO CobarServer is started and listening on806610:54:19,484INFO =============================================== - 访问Cobar同访问MySQL的方式完全相同, 常用访问方式如下(注意:本文将Cobar部署在192.168.0.1这台机器上,否则请替换为您的Cobar所在IP,其他信息不变)
#命令行mysql -h192.168.0.1-utest -ptest -P8066 -Ddbtest#JDBC(建议5.1以上的mysql driver版本)Class.forName("com.mysql.jdbc.Driver");Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.0.1:8066/dbtest","test","test");...... - SQL执行示例,执行语句时与使用传统单一数据库无区别
mysql>show databases; #dbtest1、dbtest2、dbtest3对用户透明+----------+|DATABASE|+----------+| dbtest |+----------+mysql>show tables; #dbtest中有两张表tb1和tb2+-------------------+| Tables_in_dbtest1 |+-------------------+| tb1 || tb2 |+-------------------+mysql>insertintotb1 (id, gmt)values(1, now()); #向表tb1插入一条数据mysql>insertintotb2 (id, val)values(1,"part1"); #向表tb2插入一条数据mysql>insertintotb2 (id, val)values(2,"part1"), (513,"part2"); #向表tb2同时插入多条数据mysql>select*fromtb1; #查询表tb1,验证数据被成功插入+----+---------------------+| id | gmt |+----+---------------------+| 1 | 2012-06-12 15:00:42 |+----+---------------------+mysql>select*fromtb2; #查询tb2,验证数据被成功插入+-----+-------+| id | val |+-----+-------+| 1 | part1 || 2 | part1 || 513 | part2 |+-----+-------+mysql>select*fromtb2whereidin(1, 513); #根据id查询+-----+-------+| id | val |+-----+-------+| 1 | part1 || 513 | part2 |+-----+-------+ - 查看后端MySQL数据库dbtest1,dbtest2和dbtest3,验证数据分布在不同的库中
浙公网安备 33010602011771号