一,什么是cobar:

cobar是提供关系型数据库(MYSQL)分布式服务的中间件,它可以让传统的数据库得到良好的线性扩展,并看上去还是一个数据库,对应用保持透明。

二,为什么要用cobar:

当数据不断上升,单库里面的表记录不断增加的时候,查询和索引的更变就会变得异常的缓慢,这时候我们会想到分库和分表(水平拆分和竖直拆分),cobar这个中间件就是专门用来分库和分表的。

当然新生儿mycat也是一种选择。在mycat和cobar之间,我还是要推荐cobar,因为稳定。毕竟cobar在阿里稳定运行3年以上;接管了3000+个mysql数据库的schema;集群日处理在线sql请求50亿以上;集群日处理在线数据量TB级别以上。

三,安装cobar

在安装cobar前先要安装好jdk,因为cobar是基于java的中间件;安装好mysql数据库,因为我们要测试mysql的分库分表功能

cobar的安装非常简单,解压即可。我从官网下载的最新安装包cobar-server-1.2.7.tar.gz,解压到/usr/local下,并更名为cobar

ls /usr/local/cobar
bin  conf  lib  logs

原来是没有logs文件夹,是需要自己手动建上去的(注:版本不一样,有的自动包含logs文件夹);

ls /usr/local/cobar/bin/
restart.sh  shutdown.sh  startup.bat  startup.sh

启动和关闭脚本是startup.sh和shutdown.sh

四,数据的准备:

进入mysql,创建一个root@172.16.88.131账号让cobar访问:

mysql> grant all privileges on *.*  to  root@172.16.88.131  identified by  '123456';

创建数据库:dbtest1,dbtest2,dbtest3,表:tb1,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));

五,配置cobar:

ls /usr/local/cobar/conf/
log4j.xml  rule.xml  schema.xml  server.xml

先看一下schema.xml的配置:

cat  schema.xml
dsTest[0]
dsTest[1]
dsTest[2]
172.16.88.131:3306/dbtest1
172.16.88.131:3306/dbtest2
172.16.88.131:3306/dbtest3
root
123456
STRICT_TRANS_TABLES

再看看rule.xml配置:

cat rule.xml
id
2
512

然后看看server.xml配置

cat   server.xml
test
dbtest

六,启动和使用cobar:

启动:/usr/local/cobar/startup.sh

查看/usr/local/cobar/logs/sdout.log日志:

11:48:02,217 INFO  ===============================================
11:48:02,217 INFO  Cobar is ready to startup ...
11:48:02,221 INFO  Startup processors ...
11:48:02,256 INFO  Startup connector ...
11:48:02,259 INFO  Initialize dataNodes ...
11:48:02,481 INFO  dnTest3:0 init success
11:48:02,493 INFO  dnTest2:0 init success
11:48:02,500 INFO  dnTest1:0 init success
11:48:02,514 INFO  CobarManager is started and listening on 9066
11:48:02,515 INFO  CobarServer is started and listening on 8066
11:48:02,516 INFO  ===============================================

好了,cobar已经启动成功。

很多小伙伴也许会问,为什么创建数据库会用dbtest1,dbtest2,dbtest3,表都用tb1,tb2;因为默认的配置文件里面初始值是dbtest1-3,tb1-2,所以对于新手来说,这样修改的东西会很少,只需要改下IP,用户名和密码就OK了。

cobar的端口主要是8066和9066,一个是服务端口,一个是管理端口。

比如:修改了配置文件后,进入管理端口9066,重新加载一下即可

mysql -h172.16.88.131 -utest -ptest -P9066 -Ddbtest
mysql> reload @@config;
Query OK, 1 row affected (0.08 sec)
Reload config success

比如:查看节点和线程

mysql> show @@datanode;
+---------+-------------+-------+-------+--------+------+------+---------+------------+----------+---------+---------------+
| NAME    | DATASOURCES | INDEX | TYPE  | ACTIVE | IDLE | SIZE | EXECUTE | TOTAL_TIME | MAX_TIME | MAX_SQL | RECOVERY_TIME |
+---------+-------------+-------+-------+--------+------+------+---------+------------+----------+---------+---------------+
| dnTest1 | dsTest[0]   |     0 | mysql |      0 |    0 |  128 |       3 |          0 |        0 |       0 |            -1 |
| dnTest2 | dsTest[1]   |     0 | mysql |      0 |    0 |  128 |       9 |          0 |        0 |       0 |            -1 |
| dnTest3 | dsTest[2]   |     0 | mysql |      0 |    0 |  128 |       6 |          0 |        0 |       0 |            -1 |
+---------+-------------+-------+-------+--------+------+------+---------+------------+----------+---------+---------------+

3 rows in set (0.02 sec)

mysql> show @@threadpool;
+-----------------+-----------+--------------+-----------------+----------------+------------+
| NAME            | POOL_SIZE | ACTIVE_COUNT | TASK_QUEUE_SIZE | COMPLETED_TASK | TOTAL_TASK |
+-----------------+-----------+--------------+-----------------+----------------+------------+
| InitExecutor    |         1 |            0 |               0 |              9 |          9 |
| TimerExecutor   |         1 |            0 |               0 |           5857 |       5857 |
| ManagerExecutor |         1 |            1 |               0 |             14 |         15 |
| Processor0-H    |         1 |            0 |               0 |             50 |         50 |
| Processor0-E    |         1 |            0 |               0 |             43 |         43 |
+-----------------+-----------+--------------+-----------------+----------------+------------+

5 rows in set (0.01 sec)

现在开始测试cobar的分库分表的功能,进入8066端口:

mysql -h172.16.88.131 -utest -ptest -P9066 -Ddbtest
mysql> show databases;
+----------+
| DATABASE |
+----------+
| dbtest   |
+----------+
1 row in set (0.01 sec)
mysql> use dbtest;
Database changed
mysql> show tables;
+------------------+
| Tables_in_dbtest |
+------------------+
| tb2              |
| tb1              |
+------------------+
2 rows in set (0.01 sec)

插入几条数据:

mysql>insert into tb1(id,gmt) values (1,now());
mysql>insert into tb2(id,val) values (1,"user1");
mysql>insert into tb2(id,val) values (2,"user1");
mysql>insert into tb2(id,val) values (6,"user2");
mysql> select * from tb1;
+----+---------------------+
| id | gmt                 |
+----+---------------------+
|  1 | 2015-01-12 11:50:13 |
+----+---------------------+
1 row in set (0.00 sec)
mysql> select * from tb2;
+----+-------+
| id | val   |
+----+-------+
|  1 | user1 |
|  2 | user1 |
|  6 | user2 |
+----+-------+

3 rows in set (0.00 sec)

退出cobar的服务8066端口,再进入mysql的里面查看验证下数据是否分布在不同的库中。