http://wuyanzan60688.blog.163.com/blog/static/12777616320121075123646/
据说该产品在阿里的B2B上已经运行3年之久,通过Cobar中间件,可以像传统数据库一样为分布式数据库集群提供海量数据服务,据称Cobar集群每天处理50亿次的SQL请求。
3.Cobar配置dbtest1——单独数据库,存放tb1的数据
dbtest2,dbtest3——两个数据库,共同存放tb2的数据
create
database
dbtest1;
use dbtest1;
create
table
tb1(
id
int
not
null
,
gmt datetime);
create
database
dbtest2;
use dbtest2;
create
table
tb2(
id
int
not
null
,
val
varchar
(256));
create
database
dbtest3;
use dbtest3;
create
table
tb2(
id
int
not
null
,
val
varchar
(256));
<?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>192.168.213.201:3306/dbtest1</location> <location>192.168.213.201:3306/dbtest2</location> <location>192.168.213.201:3306/dbtest3</location> </property> <property name="user">root</property> <property name="password"></property>
<property name="sqlMode">STRICT_TRANS_TABLES</property> </dataSource> </cobar:schema>
然后进logs文件夹查看启动情况[root@localhost bin]# ./startup.sh "/usr/java/jdk1.6.0_31/bin/java" -Dcobar.home="/home/wuyanzan/source/cobar-server-1.2.6" -classpath "/home/wuyanzan/source/cobar-server-1.2.6/conf:/home/wuyanzan/source/cobar-server-1.2.6/lib/classes:/home/wuyanzan/source/cobar-server-1.2.6/lib/cobar-common-1.2.6.jar:/home/wuyanzan/source/cobar-server-1.2.6/lib/cobar-config-1.2.6.jar:/home/wuyanzan/source/cobar-server-1.2.6/lib/cobar-net-1.2.6.jar:/home/wuyanzan/source/cobar-server-1.2.6/lib/cobar-parser-1.2.6.jar:/home/wuyanzan/source/cobar-server-1.2.6/lib/cobar-route-1.2.6.jar:/home/wuyanzan/source/cobar-server-1.2.6/lib/cobar-server-1.2.6.jar:/home/wuyanzan/source/cobar-server-1.2.6/lib/log4j-1.2.16.jar:/home/wuyanzan/source/cobar-server-1.2.6/lib/slf4j-api-1.6.4.jar:/home/wuyanzan/source/cobar-server-1.2.6/lib/slf4j-log4j12-1.6.4.jar" -server -Xms1024m -Xmx1024m -Xmn256m -Xss128k -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 >> "/home/wuyanzan/source/cobar-server-1.2.6/logs/console.log" 2>&1 &
说明启动成功,也就是说现在Cobar在192.168.213.9号节点上已经部署好了,对用户来说,该节点上已经有了一个mysql数据库schema,里面包含了两张表:tb1,tb2,database的名字可以从上面说到的schema.xml中看到(这里我是按默认设置,当然也可以修改),具体如下:17:19:06,623 INFO =============================================== 17:19:06,624 INFO Cobar is ready to startup ... 17:19:06,624 INFO Startup processors ... 17:19:07,047 INFO Startup connector ... 17:19:07,049 INFO Initialize dataNodes ... 17:19:07,068 INFO dnTest1:0 init success 17:19:07,069 INFO dnTest3:0 init success 17:19:07,070 INFO dnTest2:0 init success 17:19:07,077 INFO CobarManager is started and listening on 9066 17:19:07,078 INFO CobarServer is started and listening on 8066 17:19:07,078 INFO =============================================== 17:19:07,113 INFO [thread=Processor1-H0,class=ServerConnection,host=127.0.0.1,port=57823,schema=null]'_HEARTBEAT_USER_' login success
看到没,Cobar让原本在虚拟机192.168.213.201上的三个数据库dbtest1,dbtest2,dbtest3从外表上看转移到了192.168.213.9上来了,而且合并成了一个数据库dbtest,完了之后我们还可以对dbtest进行操作,比如我们按资料上来:# mysql -h 192.168.213.9 -utest -ptest -P8066 -Ddbtest
mysql> show databases; +----------+ | DATABASE | +----------+ | dbtest | +----------+
mysql> use dbtest; Database changed mysql> show tables; +------------------+ | Tables_in_dbtest | +------------------+ | 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同时插入多条数据
看到没,跟操作传统数据是一样一样的,但是我们再到192.68.213.201上去看看,看这里发生了什么mysql> select * from tb1; +----+---------------------+ | id | gmt | +----+---------------------+ | 1 | 2012-11-07 08:32:48 | +----+---------------------+
mysql> select * from tb2; +-----+-------+ | id | val | +-----+-------+ | 1 | part1 | | 2 | part1 | | 513 | part2 | +-----+-------+
看,tb2因为是dbtest2,dbtest3共同存储,因此在192.168.213.9上的insert tb2的操作就被分流写入到这两个数据库中了(这里Cobar用的是hash然后取模)。mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | dbtest1 | | dbtest2 | | dbtest3 | | mysql | | test | | tpcw2 | +--------------------+
mysql> use dbtest1; Database changed mysql> show tables; +-------------------+ | Tables_in_dbtest1 | +-------------------+ | tb1 | +-------------------+
mysql> select * from tb1; +----+---------------------+ | id | gmt | +----+---------------------+ | 1 | 2012-11-07 08:32:48 | +----+---------------------+
mysql> use dbtest2; Database changed mysql> show tables; +-------------------+ | Tables_in_dbtest2 | +-------------------+ | tb2 | +-------------------+
mysql> select * from tb2; +----+-------+ | id | val | +----+-------+ | 1 | part1 | | 2 | part1 | +----+-------+
mysql> use dbtest3; Database changed mysql> show tables; +-------------------+ | Tables_in_dbtest3 | +-------------------+ | tb2 | +-------------------+
mysql> select * from tb2; +-----+-------+ | id | val | +-----+-------+ | 513 | part2 | +-----+-------+
然后启动之后直接进入即可<dataSource name="dsTest" type="mysql"> <property name="location"> <location>127.0.0.1:3306/dbtest1</location> <location>127.0.0.1:3306/dbtest2</location> <location>127.0.0.1:3306/dbtest3</location> </property> <property name="user">root</property> <property name="password">111111</property> <property name="sqlMode">STRICT_TRANS_TABLES</property> </dataSource>
# mysql -h 127.0.0.1 -utest -ptest -P8066 -Ddbtest
而这需要192.168.213.9能连接到192.168.213.201的mysql,这是需要配置的,我们需要给前者访问权限,这里需要用grant命令,好吧我当复习下功课:( 注意这里我设置登录密码为空,所有schema.xml的password选项填空)<dataSource name="dsTest" type="mysql"> <property name="location"> <location>192.168.213.201:3306/dbtest1</location> <location>192.168.213.201:3306/dbtest2</location> <location>192.168.213.201:3306/dbtest3</location> </property> <property name="user">root</property> <property name="password"></property> <property name="sqlMode">STRICT_TRANS_TABLES</property> </dataSource>
然后我们就可以在database——mysql中的user表里查看了mysql>grant all on *.* to root@'192.168.213.9' identified by '';
即把该实例中所有数据库的所有操作权限(*.*)全部给192.168.213.9的root用户,而且不用密码(identified by '')
完了之后就可以像上面一样配置了。mysql> select user, host,password from user; +------+-----------------------+-------------------------------------------+ | user | host | password | +------+-----------------------+-------------------------------------------+ | root | localhost | *FD571203974BA9AFE270FE62151AE967ECA5E0AA | | root | localhost.localdomain | *FD571203974BA9AFE270FE62151AE967ECA5E0AA | | | localhost.localdomain | | | | localhost | | | root | 192.168.213.201 | | | root | 192.168.213.9 | | | root | node9 | | +------+-----------------------+-------------------------------------------+