1. 程式人生 > >Mycat之資料庫分片(取模分片)-yellowcong

Mycat之資料庫分片(取模分片)-yellowcong

取模分片,簡單來講,根據資料庫的主鍵和儲存的節點數進行取模操作,然後根據取模的結果,將資料存放到對應的節點中,取模分表,可以將資料均勻的分配到各個庫中。實現的步驟:1、建立資料庫,2、配置schema.xml檔案,3、配置server.xml,4、新增rule.xml。這種方式,導致每一個庫裡面的資料不連續,導致查詢的時候,比較耗費資源,但是如果有一些需要按照數字來計算型別的,可以採用這個。

建立資料庫

我建立一個學生表,code是學好,我們的學好例子:2012020891 ,有10位,快超出了範圍,所以我們使用bigint。

-- 建立資料庫
CREATE DATABASE IF NOT
EXISTS local1;
-- 使用local1資料庫 use local1; -- 建立使用者表 create table student(code bigint(10) comment '學號',dbname varchar(32) comment '資料庫名',username varchar(32) comment '使用者名稱稱',age int(3) comment '年齡' ); CREATE DATABASE IF NOT EXISTS local2; use local2; create table student(code bigint(10) comment '學號'
,dbname varchar(32) comment '資料庫名',username varchar(32) comment '使用者名稱稱',age int(3) comment '年齡' );
CREATE DATABASE IF NOT EXISTS local3; use local3; create table student(code bigint(10) comment '學號',dbname varchar(32) comment '資料庫名',username varchar(32) comment '使用者名稱稱',age int(3) comment '年齡' );

檢視建立好的表結構
這裡寫圖片描述

配置mycat

1、配置schema.xml

vim ./conf/schema.xml

#新增表
#通過這種$的方法,解決了有很多資料庫的情況
#jdbc_node$1-3 ,表示的是jdbc_node、jdbc_node、jdbc_node3
<table name="student" dataNode="jdbc_node$1-3" rule="auto-sharding-rang-mod-student"/>

完整配置

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">

        <schema name="yellowcong" checkSQLschema="true" sqlMaxLimit="100">
                <table name="student" dataNode="jdbc_node$1-3" rule="auto-sharding-rang-mod-student"/>
        </schema>

        <dataNode name="jdbc_node1" dataHost="localhost" database="local1" />
        <dataNode name="jdbc_node2" dataHost="localhost" database="local2" />
        <dataNode name="jdbc_node3" dataHost="localhost" database="local3" />

        <dataHost name="localhost" maxCon="1000" minCon="10" balance="0"
                          writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
                <heartbeat>select user()</heartbeat>
                <writeHost host="host" url="127.0.0.1:3306" user="root" password="root"/>
        </dataHost>
</mycat:schema>

2、配置server.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mycat:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://io.mycat/">
        <system>
        <property name="useSqlStat">0</property>  <!-- 1為開啟實時統計、0為關閉 -->
        <property name="useGlobleTableCheck">0</property>  <!-- 1為開啟全加班一致性檢測、0為關閉 -->

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

                <!--預設為type 0: DirectByteBufferPool | type 1 ByteBufferArena-->
                <property name="processorBufferPoolType">0</property>           
                <!--分散式事務開關,0為不過濾分散式事務,1為過濾分散式事務(如果分散式事務內只涉及全域性表,則不過濾),2為不過濾分散式事務,但是記錄分散式事務日誌-->
                <property name="handleDistributedTransactions">0</property>

                        <!--
                        off heap for merge/order/group/limit      1開啟   0關閉
                -->
                <property name="useOffHeapForMerge">1</property>

                <!--
                        單位為m
                -->
                <property name="memoryPageSize">1m</property>

                <!--
                        單位為k
                -->
                <property name="spillsFileBufferSize">1k</property>

                <property name="useStreamOutput">0</property>

                <!--
                        單位為m
                -->
                <property name="systemReserveMemorySize">384m</property>


                <!--是否採用zookeeper協調切換  -->
                <property name="useZKSwitch">true</property>


        </system>

        <user name="root">
                <property name="password">root</property>
                <property name="schemas">yellowcong</property>

        </user>

        <user name="user">
                <property name="password">user</property>
                <property name="schemas">yellowcong</property>
                <property name="readOnly">true</property>
        </user>

</mycat:server>

3、配置rule.xml

通過學生的學號(code)欄位,進行分片操作。

vim ./conf/rule.xml

#新增rule ,
#設定通過code 這個欄位進行分片

<tableRule name="auto-sharding-rang-mod-student">
        <rule>
                <columns>code</columns>
                <algorithm>rang-mod-student</algorithm>
        </rule>
</tableRule>


#新增rang-mod-student的配置
#count 是分片的數量
<function name="rang-mod-student" class="io.mycat.route.function.PartitionByMod">
      <!-- how many data nodes -->
      <property name="count">3</property>
</function>

測試插入資料

插入資料的時候,必須指定列名。mycat不支援直接插入不帶列明的資料。

#連線mycat 
mysql -h 127.0.0.1 -P 8066 -u root -proot

#使用yellowcong 資料庫
use yellowcong 

#插入資料
insert into student (code,dbname,username,age) values (1,database(),'yellowcong',12);

insert into student (code,dbname,username,age) values (2,database(),'yellowcong',12);

insert into student (code,dbname,username,age) values (3,database(),'yellowcong',12);

insert into student (code,dbname,username,age) values (4,database(),'yellowcong',12);

插入的結果,會根據指定的code,進行與運算,運算結果如下表, 可以看到與的結果和對應,結果發現對應的node節點,開始是從 local1開始的。。。

code 與運算結果 對應資料庫
1 1%3 = 1 local2
2 2%3 = 2 local3
3 3%3 = 0 local1
4 4%3 = 1 local2

實際結果
這裡寫圖片描述

問題集合

io.mycat.config.util.ConfigException: java.lang.NullPointerException

這個問題的原因,多半就是節點不存在的情況。簡單來講,就是表的節點配置有問題
這裡寫圖片描述

檢查節點配置
這裡寫圖片描述

error java.net.BindException: 地址已在使用

ip地址佔用中,所以需要關閉8066埠的程式,然後再啟動mycat服務
這裡寫圖片描述

解決辦法

#檢視端口占用情況
netstat -anop | grep 8066

#殺死佔用埠的執行緒
kill 21396

#重啟mycat服務
 ./bin/mycat restart

這裡寫圖片描述

can’t find table define in schema USER schema:yellowcong

表不存在與schemal,簡單來講,就是俺的sql插入的表物件寫錯了。
這裡寫圖片描述