MySql分片規(guī)則·求模分片

一、求模分片規(guī)則(mod-long)

  • 表中必須有名叫id的表頭
  • 根據(jù)id表頭與設(shè)定數(shù)字取余的結(jié)果存儲數(shù)據(jù)
    余數(shù)是 0 數(shù)據(jù)存儲到 dn1
    余數(shù)是 1 數(shù)據(jù)存儲到 dn2
    余數(shù)是 2 數(shù)據(jù)存儲到 dn3

二、相關(guān)配置

  • schema.xml文件中設(shè)置mod-long規(guī)則
# 查看使用mod-long分片規(guī)則的表名:
[root@maxscale56 ~]# vim /usr/local/mycat/conf/schema.xml                   
<table name="hotnews" primaryKey="ID" autoIncrement="true" 
       dataNode="dn1,dn2,dn3"  rule="mod-long" />
  • rule.xml存儲分片規(guī)則對應(yīng)的 分片字段名 以及 求模的數(shù)值
# 設(shè)置取余計算的數(shù)字
# <colums>存儲分片字段名 
# <algorithm>存儲算法規(guī)則
# <property>定義分片字段做求模計算的數(shù)字
[root@maxscale56 ~]# vim /usr/local/mycat/conf/rule.xml                 
<tableRule name="mod-long"> 
    <rule>
        <columns>id</columns> 
        <algorithm>mod-long</algorithm> 
    </rule>
</tableRule>
<function name="mod-long" 
          class="io.mycat.route.function.PartitionByMod">
    <!-- how many data nodes -->
    # 定義分片字段 做求模計算的數(shù)字(是數(shù)據(jù)庫服務(wù)器的臺數(shù))
    <property name="count">3</property> 
</function>

三、示例:

  • 建表并存儲數(shù)據(jù)
# 客戶端進(jìn)入分片服務(wù)器
[root@host50 ~]# mysql -h192.168.88.60 -P8066 -uroot -p123456
# 進(jìn)入虛擬庫
mysql> use TESTDB;
mysql> create  table  hotnews(id  int  , 
  title char(30),
  comment varchar(150) , 
  worker char(3) 
);
# 插入數(shù)據(jù)
mysql> insert into hotnews(id,title,comment,worker)
  values(9,"a","a","a");    
mysql> insert into hotnews(id,title,comment,worker)
  values(10,"b","a","a");       
mysql> insert into hotnews(id,title,comment,worker)
  values(11,"b","a","a");
mysql> insert into hotnews(id,title,comment,worker)
  values(7,"b","a","a");
mysql> insert into hotnews(id,title,comment,worker)
  values(1000,"d","a","a");
  • 在數(shù)據(jù)庫服務(wù)器查看數(shù)據(jù)(確認(rèn)不同的數(shù)據(jù)根據(jù)不同的id值被分配到不同的數(shù)據(jù)庫即為成功)
[root@host63 ~]# mysql -uroot -p123qqq...A -e 'select  * from db1.hotnews'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+-------+---------+--------+
| id   | title | comment | worker |
+------+-------+---------+--------+
|    9 | a     | a       | a      |
|    0 | d     | a       | a      |
+------+-------+---------+--------+

[root@host64 ~]# mysql -uroot -p123qqq...A -e 'select  * from db2.hotnews'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+-------+---------+--------+
| id   | title | comment | worker |
+------+-------+---------+--------+
|   10 | b     | a       | a      |
| 1000 | d     | a       | a      |
+------+-------+---------+--------+

[root@host65 ~]# mysql -uroot -p123qqq...A -e 'select  * from db3.hotnews'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+-------+---------+--------+
| id   | title | comment | worker |
+------+-------+---------+--------+
|   11 | c     | a       | a      |
+------+-------+---------+--------+
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容