阿里巴巴解决数据拆分的伪分布式数据库 中间件Cobar正式开源 六月 19, 2012 by admin · 1 Comment 编者加注: 阿里巴巴于2012年6月19日,正式对外开源的数据库中间件Cobar,前身是早已经开源的Amoeba,不过其作者陈思儒离职去盛大之后,阿里巴巴内部考虑到A
阿里巴巴解决数据拆分的伪分布式数据库 中间件Cobar正式开源
六月 19, 2012 by
admin · 1 Comment
编者加注:
阿里巴巴于2012年6月19日,正式对外开源的数据库中间件Cobar,前身是早已经开源的Amoeba,不过其作者陈思儒离职去盛大之后,阿里巴巴内部考虑到Amoeba的稳定性、
性能和功能支持,以及其他因素,重新设立了一个项目组并且更换名称为Cobar,当时的开发人员是贺贤懋(备注:可能名字的字不对,实在有点久远,虽然当时一起合作),还
有一位百阿的同事也加入这个团队(备注:旺旺密码不记得了,所以他的名字也无法记起,看见请莫怪我这位记忆力不好的百阿同学),开发语言是Java,一开始只支持MySQL
数据库,并且用在新项目BRMMS(中文名称:商人社区,BRMMS是项目代号,一般只记得代号,实在参与过太多项目研发),后来也支持Oracle数据库,因为阿里巴巴中文站
的Offer数据库,需要从Oracle数据库+存储设备,迁移到MySQL+PC Server平台上,为保证用户数据的安全性,迁移过程是每128分之一切换的模式。虽然测试的非常严格,我
们几乎所有可能碰到的情况,甚至极端情况都测试过,但是依然碰过一些莫名其妙的问题,比如从MySQL双主复制模式,从主A切换为B,出现过某个小集群的应用程序连接确实
切换成功,但是又自己切换回来了,直到我离开也没有找出原因,不过后来再切换又从未出现过,Cobar开源对大家解决数据的垂直拆分和水平拆分,那是如虎贴翼,非常方便!
场景描述
Cobar是关系型数据的分布式处理系统,它可以在分布式的环境下像传统数据库一样为您提供海量数据服务。以下是快速启动场景:
系统对外提供的数据库名是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,脚本如下:
数据库创建脚本:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
#创建dbtest1
drop
database if exists dbtest1;
create
database dbtest1;
use dbtest1;
#在dbtest1上创建tb1
create
table tb1(
id int
not null,
gmt datetime);
#创建dbtest2
drop
database if exists dbtest2;
create
database dbtest2;
use dbtest2;
#在dbtest2上创建tb2
create
table tb2(
id int
not null,
val varchar(256));
#创建dbtest3
drop
database if exists dbtest3;
create
database dbtest3;
use dbtest3;
#在dbtest3上创建tb2
create
table tb2(
id int
not null,
val varchar(256));
|
步骤二:部署和配置Cobar
请确保机器上设置了JAVA环境变量JAVA_HOME 下载Cobar压缩文件并解压,进入conf目录可以看到schema.xml, rule.xml, server.xml等相关的配置文件
?
|
1
2
3
4
5
6
|
wget http://code.alibabatech.com/mvn/releases/com/alibaba/cobar/cobar-server/1.2.4/cobar-server-1.2.4.tar.gz
tar
zxf cobar-server-1.2.4.tar.gz
cd
cobar-server-1.2.4 #可以看到bin,conf,lib,logs四个目录
schema.xml配置如下<span style="color: #ff0000;">(注意:schema.xml包含MySQL的IP、端口、用户名、密码等配置,您需要按照注释替换为您的MySQL信息。)</span>
<strong>schema.xml 配置</strong>:
|
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
"1.0"
encoding="UTF-8"?>
"schema.dtd">
"http://cobar.alibaba.com/">
"dbtest"
dataNode="dnTest1">
"tb2"
dataNode="dnTest2,dnTest3"
rule="rule1"
/>
"dnTest1">
"dataSource">
dsTest[0]
"dnTest2">
"dataSource">
dsTest[1]
"dnTest3">
"dataSource">
dsTest[2]
"dsTest"
type="mysql">
"location">
192.168.0.1:3306/dbtest1
192.168.0.1:3306/dbtest2
192.168.0.1:3306/dbtest3
"user">test
"password">
"sqlMode">STRICT_TRANS_TABLES
?
|
1
|
rule.xml配置如下"color: #ff0000;">(本文仅以数字类型的id字段作为拆分字段,将数据拆分到两个库中。)
|
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
"1.0"
encoding="UTF-8"?>
"rule.dtd">
"http://cobar.alibaba.com/">
"rule1">
id
<function
name="func1"
class="com.alibaba.cobar.route.function.PartitionByLong">
"partitionCount">2
"partitionLength">512
function>
|
?
?
|
1
2
3
4
5
6
7
8
9
10
|
"1.0"
encoding="UTF-8"?>
"server.dtd">
"http://cobar.alibaba.com/">
"test">
"password">test
"schemas">dbtest
|
步骤三:启动和使用Cobar
启动Cobar,进入bin目录可以看到Cobar的启动、停止与重启脚本
?
|
1
2
3
|
./startup.sh
#Cobar进程名为CobarStartup
查看logs目录下stdout.log, 启动成功日志如下</strong>
|
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
10:54:19,264 INFO ===============================================
10:54:19,265 INFO Cobar is ready to startup ...
10:54:19,265 INFO Startup processors ...
10:54:19,443 INFO Startup connector ...
10:54:19,446 INFO Initialize dataNodes ...
10:54:19,470 INFO dnTest1:0 init success
10:54:19,472 INFO dnTest3:0 init success
10:54:19,473 INFO dnTest2:0 init success
10:54:19,481 INFO CobarManager is started and listening on 9066
10:54:19,483 INFO CobarServer is started and listening on 8066
10:54:19,484 INFO ===============================================
访问Cobar同访问MySQL的方式完全相同, 常用访问方式如下</strong>"color: #ff0000;">(注意:本文将Cobar部署在192.168.0.1这台机器上,否则请替换为您的Cobar所在IP,其他信息不变)</span>
|
?
|
1
2
3
4
5
6
7
8
9
|
#命令行
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执行示例,执行语句时与使用传统单一数据库无区别</strong>
|
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
mysql>show databases; #dbtest1、dbtest2、dbtest3对用户透明
+----------+
| DATABASE
|
+----------+
| dbtest |
+----------+
mysql>show tables; #dbtest中有两张表tb1和tb2
+-------------------+
| Tables_in_dbtest1 |
+-------------------+
| tb1 |
| tb2 |
+-------------------+
mysql>insert
into tb1 (id, gmt)
values (1, now()); #向表tb1插入一条数据
mysql>insert
into tb2 (id, val)
values (1, "part1"); #向表tb2插入一条数据
mysql>insert
into tb2 (id, val)
values (2, "part1"), (513,
"part2"); #向表tb2同时插入多条数据
mysql>select
* from
tb1; #查询表tb1,验证数据被成功插入
+----+---------------------+
| id | gmt |
+----+---------------------+
| 1 | 2012-06-12 15:00:42 |
+----+---------------------+
mysql>select
* from
tb2; #查询tb2,验证数据被成功插入
+-----+-------+
| id | val |
+-----+-------+
| 1 | part1 |
| 2 | part1 |
| 513 | part2 |
+-----+-------+
mysql>select
* from
tb2 where id
in (1, 513); #根据id查询
+-----+-------+
| id | val |
+-----+-------+
| 1 | part1 |
| 513 | part2 |
+-----+-------+
|
查看后端MySQL数据库dbtest1,dbtest2和dbtest3,验证数据分布在不同的库中
产品约束
使用JDBC时,推荐使用5.1以上版本Driver进行连接不支持跨库的关联操作:join、分页、排序、子查询。不支持rewriteBatchedStatements=true参数设置。默认为false不支持useServerPrepStmts=true参数设置。默认为falseBLOB, BINARY, VARBINARY字段不能使用。若特殊需求需要这三种字段,禁止使用PreparedStatement的setBlob()或setBinaryStream()方法设置参数。不支持SAVEPOINT操作。不支持SET语句的执行,事务和字符集设置语句除外对于拆分表(一个表的数据被映射到多个MySQL数据库),不能更新已有记录的拆分字段(分库字段)值只支持MySQL数据节点。对于拆分表,插入操作须给出列名,必须包含拆分字段。
源码下载地址:http://code.alibabatech.com/svn/cobar/
MySQLOPS数据库与运维自动化技术分享
MySQLOPS数据库与运维自动化技术分享:阿里巴巴解决数据拆分的伪分布式数据库 中间件Cobar正式开源 六月 19, 2012 by admin · 1 Comment 编者加注: 阿里巴巴于2012年6月19日,正式对外开源的数据库中间件Cobar,前身是早已经开源的Amoeba,不过其作者陈思儒离职去盛大之后,阿里巴巴内部考虑到
Top
|