数据库–Cobar分布式数据库集群MySQL中间件(下)

本文介绍Cobar数据库中间件的配置方法,包括schema.xml、server.xml及rule.xml的具体配置细节,并通过Python示例演示如何进行数据插入操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Cobar配置:

schema.xml配置如下

<!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.137.8:3306/dbtest1</location>
      <location>192.168.137.31:3306/dbtest2</location>
      <location>192.168.137.32:3306/dbtest3</location>
    </property>
    <property name="user">cobar</property>
    <property name="password">sa</property>
    <property name="sqlMode">STRICT_TRANS_TABLES</property>
  </dataSource>
</cobar:schema>

server.xml简单配置

<!DOCTYPE cobar:server SYSTEM "server.dtd">
<cobar:server xmlns:cobar="http://cobar.alibaba.com/">
  <!-- 用户访问定义,用户名、密码、schema等信息。 -->
  <user name="root">
    <property name="password">sa</property>
    <property name="schemas">dbtest</property>
  </user>
</cobar:server>

rule.xml配置

<!DOCTYPE cobar:rule SYSTEM "rule.dtd">
<cobar:rule xmlns:cobar="http://cobar.alibaba.com/">
  <!-- 路由规则定义,定义什么表,什么字段,采用什么路由算法 -->
  <tableRule name="rule1">
    <rule>
      <columns>val</columns>
      <algorithm><![CDATA[ func2(${val}) ]]></algorithm>
    </rule>
  </tableRule>
  <!-- 路由函数定义 -->
  <function name="func1" class="com.alibaba.cobar.route.function.PartitionByLong">
    <property name="partitionCount">2</property>
    <property name="partitionLength">512</property>
  </function>
  <!-- 路由函数定义 -->
  <function name="func2" class="com.alibaba.cobar.route.function.PartitionByString">
    <property name="partitionCount">2</property>
    <property name="partitionLength">512</property>
    <property name="hashSlice">-5:</property>
  </function>
</cobar:rule>

这里需要说明,INSERT语句中必须包含路由规则定义的字段,否则Cobar不会对数据进行拆分,同时存储到多个数据库实例中,比如上面的tb2表中,id字段如果设置了auto_increment,插入语句中不用指明id字段,这样就无法达到数据包拆分的目的。

准备完成之后直接运行bin目录下的./startup.sh即可。

然后查看输入的日志:

yan@yan-Z400:~/cobar-server-1.2.7/logs$ tail -f stdout.log
09:57:00,155 INFO  Cobar is ready to startup ...
09:57:00,155 INFO  Startup processors ...
09:57:00,198 INFO  Startup connector ...
09:57:00,202 INFO  Initialize dataNodes ...
09:57:00,811 INFO  dnTest1:0 init success
09:57:00,816 INFO  dnTest3:0 init success
09:57:00,821 INFO  dnTest2:0 init success
09:57:00,835 INFO  CobarManager is started and listening on 9066
09:57:00,837 INFO  CobarServer is started and listening on 8066
09:57:00,837 INFO  ===============================================

这样cobar服务端就已经启动。

直接使用jdbc或mysql终端连接cobar:

yan@yan-Z400:~$ mysql -uroot -psa -P8066 -h192.168.137.8
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.48-cobar-1.2.7 Cobar Server (ALIBABA)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 下面使用几句Pyhton进行数据库的插入操作:
01#!/usr/bin/env python
02#coding=utf-8
03import MySQLdb
04 
05#连接
06cxn = MySQLdb.Connect(host='192.168.137.8',port=8066, user = 'root', passwd = 'sa')
07#游标
08cur = cxn.cursor()
09 
10cur.execute("USE dbtest")
11 
12for in range(1,200):
13    cur.execute("INSERT INTO tb2 (val) values ('this is a test record %d')"%i)
14    print 'insert the %d record into cobar'%i
15 
16cur.close()
17cxn.commit()
18cxn.close()

插入后查看数据库dbtest2和数据dbtest3的情况:

可以看到有100条数据插入了dbtest2,99条数据插入了dbtest3。


参考文档: http://blog.youkuaiyun.com/kongqz/article/details/7960869
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值