亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

數(shù)據(jù)庫–Cobar分布式數(shù)據(jù)庫集群MySQL中間件

系統(tǒng) 3276 0

運(yùn)行環(huán)境:

  • 主機(jī)1:Ubuntu14.04 Desktop + MySQL5.5 + JDK 1.7(HP Z400) ?內(nèi)網(wǎng)IP地址:192.168.137.8
  • NODE1:Ubuntu 13.04 server + MySQL5.5 ?內(nèi)網(wǎng)IP地址:192.168.137.31
  • NODE2:Ubuntu 13.04 server + MySQL5.5 ?內(nèi)網(wǎng)IP地址:192.168.137.32

注:(NODE1和NODE2運(yùn)行于XEN虛擬化平臺(tái),硬件環(huán)境HP Z800)

Cobar簡介:

Cobar是關(guān)系型數(shù)據(jù)庫的分布式處理系統(tǒng),它可以在分布式的環(huán)境下看上去像傳統(tǒng)數(shù)據(jù)庫一樣為您提供海量數(shù)據(jù)服務(wù)。

  • ? ? ? ?產(chǎn)品在阿里巴巴B2B公司已經(jīng)穩(wěn)定運(yùn)行了3年以上。
  • ? ? ? ?目前已經(jīng)接管了3000+個(gè)MySQL數(shù)據(jù)庫的schema,為應(yīng)用提供數(shù)據(jù)服務(wù)。
  • ? ? ? ?據(jù)最近統(tǒng)計(jì)cobar集群目前平均每天處理近50億次的SQL執(zhí)行請(qǐng)求。

Cobar最主要解決的問題是:分布式和HA。

分布式:主要是通過將同一個(gè)表的數(shù)據(jù)拆分成多個(gè),放入不同的數(shù)據(jù)庫實(shí)例,查詢的時(shí)候也會(huì)按照同樣的操作方式,來更新具體數(shù)據(jù)庫實(shí)例中的對(duì)應(yīng)的數(shù)據(jù)。

HA:高可用性,在設(shè)置了MYSQL心跳的情況下,如果主數(shù)據(jù)庫發(fā)生了異常,Cobar會(huì)自動(dòng)連接從數(shù)據(jù)庫,如果主數(shù)據(jù)庫恢復(fù)正常,只能手動(dòng)恢復(fù)到主數(shù)據(jù)庫。Cobar只負(fù)責(zé)切換數(shù)據(jù)庫實(shí)例,不負(fù)責(zé)主從數(shù)據(jù)庫的同步,所以需要提前將主從數(shù)據(jù)庫設(shè)置雙向同步。

存在的不足:

  • ? (1).不支持跨庫情況下的join、分頁、排序、子查詢操作。
  • ? (2).SET語句執(zhí)行會(huì)被忽略,事務(wù)和字符集設(shè)置除外。
  • ? (3).分庫情況下,insert語句必須包含拆分字段列名。
  • ? (4).分庫情況下,update語句不能更新拆分字段的值。
  • ? (5).不支持SAVEPOINT操作。
  • ? (6).暫時(shí)只支持MySQL數(shù)據(jù)節(jié)點(diǎn)。
  • ? (7).使用JDBC時(shí),不支持rewriteBatchedStatements=true參數(shù)設(shè)置(默認(rèn)為false)。
  • ? (8).使用JDBC時(shí),不支持useServerPrepStmts=true參數(shù)設(shè)置(默認(rèn)為false)。
  • ? (9).使用JDBC時(shí),BLOB, BINARY, VARBINARY字段不能使用setBlob()或setBinaryStream()方法設(shè)置參數(shù)。

當(dāng)然,如果想努力實(shí)現(xiàn)這些功能,可以fork官方的源碼: https://github.com/alibaba/cobar

環(huán)境搭建

  • Cobar服務(wù)器:192.168.137.8:8066 用戶名/密碼:root/sa 實(shí)例名:dbtest
  • 主機(jī)1:192.168.137.8:3306 用戶名/密碼:cobar/sa 實(shí)例名:dbtest1
  • Node1:192.168.137.31:3306 用戶名/密碼:cobar/sa 實(shí)例名:dbtest2
  • Node2:192.168.137.32:3306 用戶名/密碼:cobar/sa 實(shí)例名:dbtest3

Cobar-Server-1.2.7版本下載: http://pan.baidu.com/s/1pJudQh9

實(shí)驗(yàn)拓?fù)鋱D如下:

?

cobar

?

首先分別在三個(gè)主機(jī)創(chuàng)建數(shù)據(jù)庫實(shí)例:

      
        #創(chuàng)建dbtest1腳本

   dropdatabaseif exists dbtest1;

   createdatabasedbtest1;

   use dbtest1;

   #在dbtest1上創(chuàng)建tb1

   createtabletb1(

   id   intnotnull,

   gmt   datetime);

#創(chuàng)建dbtest2

   dropdatabaseif exists dbtest2;

   createdatabasedbtest2;

   use dbtest2;

   #在dbtest2上創(chuàng)建tb2

   createtabletb2(

   id   intnotnull,

   val  varchar(
      
      
        256
      
      
        ));

#創(chuàng)建dbtest3

   dropdatabaseif exists dbtest3;

   createdatabasedbtest3;

   use dbtest3;

   #在dbtest3上創(chuàng)建tb2

   createtabletb2(

   id   intnotnull,

   val  varchar(
      
      
        256
      
      ));
    

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>

  <!-- 數(shù)據(jù)節(jié)點(diǎn)定義,數(shù)據(jù)節(jié)點(diǎn)由數(shù)據(jù)源和其他一些參數(shù)組織而成。-->

  <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>

  <!-- 數(shù)據(jù)源定義,數(shù)據(jù)源是一個(gè)具體的后端數(shù)據(jù)連接的表示。-->

  <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/
      
      
        "
      
      >

  <!-- 路由規(guī)則定義,定義什么表,什么字段,采用什么路由算法 -->

  <tableRule name=
      
        "
      
      
        rule1
      
      
        "
      
      >

    <rule>

      <columns>val</columns>

      <algorithm><![CDATA[ func2(${val}) ]]></algorithm>

    </rule>

  </tableRule>

  <!-- 路由函數(shù)定義 -->

  <function name=
      
        "
      
      
        func1
      
      
        "
      
      
        class
      
      =
      
        "
      
      
        com.alibaba.cobar.route.function.PartitionByLong
      
      
        "
      
      >

    <property name=
      
        "
      
      
        partitionCount
      
      
        "
      
      >
      
        2
      
      </property>

    <property name=
      
        "
      
      
        partitionLength
      
      
        "
      
      >
      
        512
      
      </property>

  </function>

  <!-- 路由函數(shù)定義 -->

  <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語句中必須包含路由規(guī)則定義的字段,否則Cobar不會(huì)對(duì)數(shù)據(jù)進(jìn)行拆分,同時(shí)存儲(chǔ)到多個(gè)數(shù)據(jù)庫實(shí)例中,比如上面的tb2表中,id字段如果設(shè)置了auto_increment,插入語句中不用指明id字段,這樣就無法達(dá)到數(shù)據(jù)包拆分的目的。

準(zhǔn)備完成之后直接運(yùn)行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服務(wù)端就已經(jīng)啟動(dòng)。

直接使用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進(jìn)行數(shù)據(jù)庫的插入操作:

      #!/usr/bin/
      
        env python

#coding
      
      =utf-
      
        8
      
      
        

import MySQLdb



#連接

cxn 
      
      = MySQLdb.Connect(host=
      
        '
      
      
        192.168.137.8
      
      
        '
      
      ,port=
      
        8066
      
      , user = 
      
        '
      
      
        root
      
      
        '
      
      , passwd = 
      
        '
      
      
        sa
      
      
        '
      
      
        )

#游標(biāo)

cur 
      
      =
      
         cxn.cursor()



cur.execute(
      
      
        "
      
      
        USE dbtest
      
      
        "
      
      
        )




      
      
        for
      
       i 
      
        in
      
       range(
      
        1
      
      ,
      
        200
      
      
        ):

    cur.execute(
      
      
        "
      
      
        INSERT INTO tb2 (val) values ('this is a test record %d')
      
      
        "
      
      %
      
        i)

    print 
      
      
        '
      
      
        insert the %d record into cobar
      
      
        '
      
      %
      
        i 



cur.close()

cxn.commit()

cxn.close()
      
    

插入后查看數(shù)據(jù)庫dbtest2和數(shù)據(jù)dbtest3的情況:

可以看到有100條數(shù)據(jù)插入了dbtest2,99條數(shù)據(jù)插入了dbtest3。

后面會(huì)對(duì)Cobar進(jìn)行更深入的了解。我的Fork分支

(完)

?

?

原創(chuàng)作品,轉(zhuǎn)載請(qǐng)標(biāo)明:http://blog.geekcome.com/archives/252

數(shù)據(jù)庫–Cobar分布式數(shù)據(jù)庫集群MySQL中間件


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會(huì)非常 感謝您的哦?。?!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: xxxxyoujizz护士 | 日韩中文字幕在线视频 | 国产日韩欧美一区二区三区综合 | 亚洲欧美一区二区三区在饯 | 亚洲乱视频 | 国产精品手机在线观看 | 久久国产精品免费网站 | h网站国产 | 免费h片 | 一级播放| 色婷婷av777| 亚洲毛片网 | a级毛片视频 | 亚洲欧美在线观看一区二区 | 亚洲欧美中文日韩在线 | 四虎永久免费影院 | 久久综合狠狠综合久久综合88 | 免费a一级毛片在线播放 | 奇米色吧| 中文字幕在线观看日韩 | 精品精品国产自在久久高清 | 夜夜躁日日躁狠狠 | 亚洲国产高清精品线久久 | 欧美视频三区 | 92在线视频| 久久久不卡国产精品一区二区 | 欧美日韩1区 | 国内精品一区二区2021在线 | 国产精品免费看 | 久久久久久国产精品三级 | 久久国产热 | 欧美精品福利视频 | 四虎国产精品永久地址48 | 久久久久亚洲国产 | 天天操狠狠 | 天天躁狠狠躁夜躁2021 | 奇米七七七 | 国产欧洲亚洲 | 2021国产成人综合亚洲精品 | 精品热久国产福利视频 | 久久99国产亚洲高清观看首页 |