mycat初探

1:安裝客戶端

yum install mysql java

2:安裝服務端

yum install mysql-server node

3:mycat要求不區分大小寫

my.cnf(/etc/my.cnf)的[mysqld]區段下增長: lower_case_table_names=1mysql

4:啓動mysql

service mysqld start nginx

5:建立用戶

mysqladmin -u root password 110110 算法

6:登錄mysql

mysql -u root; sql

7:賦予遠程登錄權限

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '110110' WITH GRANT OPTION;
flush privileges; 數據庫

8:上傳mycat到服務器(java要求1.7以上)

9:啓動mycat

chmod 777 ./* 在bin目錄下編程

./startup_nowrap.sh數組

10:修改配置文件

	<dataHost name="localhost1" maxCon="1000" minCon="10" balance="0"
		writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
		<heartbeat>select user()</heartbeat>
		<!-- can have multi write hosts -->
		<writeHost host="hostM1" url="10.97.190.27:3306" user="qidian"
			password="qidian">
			<!-- can have multi read hosts -->

		</writeHost>
		<!-- 
		<writeHost host="hostS1" url="localhost:3316" user="root"
			password="123456" />
		<writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->
	</dataHost>

連接本身的mysql服務器服務器

11:登錄mysql創建數據庫。數據庫名字db1,db2,db3

12:登錄mycat管理

 mysql -utest -ptest -h127.0.0.1 -P9066

show @@help

其中reload配置文件須要reload @@config_all,相似於nginx的reload

conf/server.xml 存儲mycat的帳戶,和mysql帳戶沒有關係

conf/schema.xml 邏輯表 

13:登錄mycat

mysql -utest -ptest -h127.0.0.1 -P8066 -DTESTDB

14:分片

1:全局表

<table name="company" primaryKey="ID" type="global" dataNode="dn1,dn2,dn3" />

每行記錄在每一個分片上同時存在

 

2:枚舉

schema.xml

<table name="employee" primaryKey="ID" dataNode="dn1,dn2" rule="sharding-by-intfile" />

rule.xml

<tableRule name="sharding-by-intfile">
<rule>
<columns>sharding_id</columns>
<algorithm>hash-int</algorithm>
</rule>
</tableRule>

<function name="hash-int"
class="org.opencloudb.route.function.PartitionByFileMap">
<property name="mapFile">partition-hash-int.txt</property>
</function>

partition-hash-int.txt

10000=0
10010=1

DEFAULT_NODE=1

若是你輸入

insert into employee(id,name,sharding_id) values(4, 'mydog',10011);則出錯由於分片策略沒有枚舉10011的分片位置

上面columns 標識將要分片的表字段,algorithm 分片函數,
其中分片函數配置中,mapFile標識配置文件名稱,type默認值爲0,0表示Integer,非零表示String,
全部的節點配置都是從0開始,及0表明節點1
/**
*  defaultNode 默認節點:小於0表示不設置默認節點,大於等於0表示設置默認節點
* 
默認節點的做用:枚舉分片時,若是碰到不識別的枚舉值,就讓它路由到默認節點
*                若是不配置默認節點(defaultNode值小於0表示不配置默認節點),碰到
*                不識別的枚舉值就會報錯,
*                like this:can't find datanode for sharding column:column_name val:ffffffff    
*/

 

3:父子表

		<table name="customer" primaryKey="ID" dataNode="dn1,dn2"
			rule="sharding-by-intfile">
			<childTable name="orders" primaryKey="ID" joinKey="customer_id"
				parentKey="id">
				<childTable name="order_items" joinKey="order_id"
					parentKey="id" />
			</childTable>
			<childTable name="customer_addr" primaryKey="ID" joinKey="customer_id"
				parentKey="id" />
		</table>

 

explain create table customer(id int not null primary key,name varchar(100),company_id int not null,sharding_id int not null);
explain insert into customer (id,name,company_id,sharding_id )values(1,'wang',1,10000);  
explain insert into customer (id,name,company_id,sharding_id )values(2,'xue',2,10010);  
explain insert into customer (id,name,company_id,sharding_id )values(3,'feng',3,10000); 
explain Select * from  customer; 


create table orders (id int not null primary key ,customer_id int not null,sataus int ,note varchar(100) );
        insert into orders(id,customer_id) values(1,1); //stored in db1 because customer table with id=1 stored in db1   
        insert into orders(id,customer_id) values(2,2); //stored in db2 because customer table with id=1 stored in db2    
        explain insert into orders(id,customer_id) values(2,2); 
        select customer.name ,orders.* from customer ,orders where customer.id=orders.customer_id; 

4:範圍約定

<table name="travelrecord" dataNode="dn1,dn2,dn3" rule="auto-sharding-long" />

	<tableRule name="auto-sharding-long">
		<rule>
			<columns>id</columns>
			<algorithm>rang-long</algorithm>
		</rule>
	</tableRule>

	<function name="rang-long"
		class="org.opencloudb.route.function.AutoPartitionByLong">
		<property name="mapFile">autopartition-long.txt</property>
	</function>

# range start-end ,data node index
# K=1000,M=10000.
0-500M=0
500M-1000M=1
1000M-1500M=2

5:固定分片hash算法

<tableRule name="rule1">
    <rule>
      <columns>user_id</columns>
      <algorithm>func1</algorithm>
    </rule>
</tableRule>

  <function name="func1" class="org.opencloudb.route.function.PartitionByLong">
    <property name="partitionCount">2,1</property>
    <property name="partitionLength">256,512</property>
  </function>

配置說明:
上面columns 標識將要分片的表字段,algorithm 分片函數,
partitionCount 分片個數列表,partitionLength 分片範圍列表
分區長度:默認爲最大2^n=1024 ,即最大支持1024分區
約束 :
count,length兩個數組的長度必須是一致的。
1024 = sum((count[i]*length[i])). count和length兩個向量的點積恆等於1024
用法例子:
        本例的分區策略:但願將數據水平分紅3份,前兩份各佔25%,第三份佔50%。(故本例非均勻分區)
        // |<---------------------1024------------------------>|
        // |<----256--->|<----256--->|<----------512---------->|
        // | partition0 | partition1 | partition2 |
        // | 共2份,故count[0]=2 | 共1份,故count[1]=1 |
        int[] count = new int[] { 2, 1 };
        int[] length = new int[] { 256, 512 };
        PartitionUtil pu = new PartitionUtil(count, length);

        // 下面代碼演示分別以offerId字段或memberId字段根據上述分區策略拆分的分配結果
        int DEFAULT_STR_HEAD_LEN = 8; // cobar默認會配置爲此值
        long offerId = 12345;
        String memberId = "qiushuo";

        // 若根據offerId分配,partNo1將等於0,即按照上述分區策略,offerId爲12345時將會被分配到partition0中
        int partNo1 = pu.partition(offerId);

        // 若根據memberId分配,partNo2將等於2,即按照上述分區策略,memberId爲qiushuo時將會被分到partition2中
        int partNo2 = pu.partition(memberId, 0, DEFAULT_STR_HEAD_LEN);

若是須要平均分配設置:平均分爲4分片,partitionCount*partitionLength=1024
<function name="func1" class="org.opencloudb.route.function.PartitionByLong">
    <property name="partitionCount">4</property>
    <property name="partitionLength">256</property>
  </function>

6:求模法

<tableRule name="mod-long">
    <rule>
      <columns>user_id</columns>
      <algorithm>mod-long</algorithm>
    </rule>
  </tableRule>
  <function name="mod-long" class="org.opencloudb.route.function.PartitionByMod">
   <!-- how many data nodes  -->
    <property name="count">3</property>
  </function> 
配置說明:
上面columns 標識將要分片的表字段,algorithm 分片函數,
此種配置很是明確即根據id進行十進制求模預算,相比方式1,此種在批量插入時須要切換數據源,id不連續

7:日期列分區

<tableRule name="sharding-by-date">
      <rule>
        <columns>create_time</columns>
        <algorithm>sharding-by-date</algorithm>
      </rule>
   </tableRule>  
<function name="sharding-by-date" class="org.opencloudb.route.function.PartitionByDate">
    <property name="dateFormat">yyyy-MM-dd</property>
    <property name="sBeginDate">2014-01-01</property>
    <property name="sPartionDay">10</property>
  </function>
配置說明:
上面columns 標識將要分片的表字段,algorithm 分片函數,
配置中配置了開始日期,分區天數,即默認從開始日期算起,分隔10天一個分區


Assert.assertEquals(true, 0 == partition.calculate("2014-01-01"));
Assert.assertEquals(true, 0 == partition.calculate("2014-01-10"));
Assert.assertEquals(true, 1 == partition.calculate("2014-01-11"));
Assert.assertEquals(true, 12 == partition.calculate("2014-05-01"));

8:通配取模

<tableRule name="sharding-by-pattern">
      <rule>
        <columns>user_id</columns>
        <algorithm>sharding-by-pattern</algorithm>
      </rule>
   </tableRule>
<function name="sharding-by-pattern" class="org.opencloudb.route.function.PartitionByPattern">
    <property name="patternValue">256</property>
    <property name="defaultNode">2</property>
    <property name="mapFile">partition-pattern.txt</property>

  </function>
partition-pattern.txt 
# id partition range start-end ,data node index
###### first host configuration
1-32=0
33-64=1
65-96=2
97-128=3
######## second host configuration
129-160=4
161-192=5
193-224=6
225-256=7
0-0=7

配置說明:
上面columns 標識將要分片的表字段,algorithm 分片函數,patternValue 即求模基數,defaoultNode 默認節點,若是配置了默認,則不會按照求模運算
mapFile 配置文件路徑
配置文件中,1-32 即表明id%256後分布的範圍,若是在1-32則在分區1,其餘類推,若是id非數據,則會分配在defaoultNode 默認節點

String idVal = "0";
Assert.assertEquals(true, 7 == autoPartition.calculate(idVal));
idVal = "45a";
Assert.assertEquals(true, 2 == autoPartition.calculate(idVal));

9:ASCII碼求模通配

<tableRule name="sharding-by-prefixpattern">
      <rule>
        <columns>user_id</columns>
        <algorithm>sharding-by-prefixpattern</algorithm>
      </rule>
   </tableRule>
<function name="sharding-by-pattern" class="org.opencloudb.route.function.PartitionByPattern">
    <property name="patternValue">256</property>
    <property name="prefixLength">5</property>
    <property name="mapFile">partition-pattern.txt</property>

  </function>

partition-pattern.txt

# range start-end ,data node index
# ASCII
# 48-57=0-9
# 6四、65-90=@、A-Z
# 97-122=a-z
###### first host configuration
1-4=0
5-8=1
9-12=2
13-16=3
###### second host configuration
17-20=4
21-24=5
25-28=6
29-32=7
0-0=7
配置說明:
上面columns 標識將要分片的表字段,algorithm 分片函數,patternValue 即求模基數,prefixLength ASCII 截取的位數
mapFile 配置文件路徑
配置文件中,1-32 即表明id%256後分布的範圍,若是在1-32則在分區1,其餘類推 

此種方式相似方式6只不過採起的是將列種獲取前prefixLength位列全部ASCII碼的和進行求模sum%patternValue ,獲取的值,在通配範圍內的
即 分片數,
/**
* ASCII編碼:
* 48-57=0-9阿拉伯數字
* 6四、65-90=@、A-Z
* 97-122=a-z
*
*/
如 

String idVal="gf89f9a";
Assert.assertEquals(true, 0==autoPartition.calculate(idVal));

idVal="8df99a";
Assert.assertEquals(true, 4==autoPartition.calculate(idVal));

idVal="8dhdf99a";
Assert.assertEquals(true, 3==autoPartition.calculate(idVal));

10:編程指定

<tableRule name="sharding-by-substring">
      <rule>
        <columns>user_id</columns>
        <algorithm>sharding-by-substring</algorithm>
      </rule>
   </tableRule>
<function name="sharding-by-substring" class="org.opencloudb.route.function.PartitionDirectBySubString">
    <property name="startIndex">0</property> <!-- zero-based -->
    <property name="size">2</property>
    <property name="partitionCount">8</property>
    <property name="defaultPartition">0</property>
  </function>
配置說明:
上面columns 標識將要分片的表字段,algorithm 分片函數 
此方法爲直接根據字符子串(必須是數字)計算分區號(由應用傳遞參數,顯式指定分區號)。
例如id=05-100000002
在此配置中表明根據id中從startIndex=0,開始,截取siz=2位數字即05,05就是獲取的分區,若是沒傳默認分配到defaultPartition

11:字符串拆分hash解析

<tableRule name="sharding-by-stringhash">
      <rule>
        <columns>user_id</columns>
        <algorithm>sharding-by-stringhash</algorithm>
      </rule>
   </tableRule>
<function name="sharding-by-substring" class="org.opencloudb.route.function.PartitionDirectBySubString">
    <property name=length>512</property> <!-- zero-based -->
    <property name="count">2</property>
    <property name="hashSlice">0:2</property>
  </function>
配置說明:
上面columns 標識將要分片的表字段,algorithm 分片函數 
函數中length表明字符串hash求模基數,count分區數,hashSlice hash預算位

即根據子字符串 hash運算

	

hashSlice : 0 means str.length(), -1 means str.length()-1

/**
     * "2" -> (0,2)<br/>
     * "1:2" -> (1,2)<br/>
     * "1:" -> (1,0)<br/>
     * "-1:" -> (-1,0)<br/>
     * ":-1" -> (0,-1)<br/>
     * ":" -> (0,0)<br/>
     */
例子:
String idVal=null;
 rule.setPartitionLength("512");
 rule.setPartitionCount("2");
 rule.init();
 rule.setHashSlice("0:2");
//		idVal = "0";
//		Assert.assertEquals(true, 0 == rule.calculate(idVal));
//		idVal = "45a";
//		Assert.assertEquals(true, 1 == rule.calculate(idVal));

 
 
 //last 4
 rule = new PartitionByString();
 rule.setPartitionLength("512");
 rule.setPartitionCount("2");
 rule.init();
 //last 4 characters
 rule.setHashSlice("-4:0");
 idVal = "aaaabbb0000";
 Assert.assertEquals(true, 0 == rule.calculate(idVal));
 idVal = "aaaabbb2359";
 Assert.assertEquals(true, 0 == rule.calculate(idVal));

12:一致性hash

<tableRule name="sharding-by-murmur">
      <rule>
        <columns>user_id</columns>
        <algorithm>murmur</algorithm>
      </rule>
   </tableRule>
<function name="murmur" class="org.opencloudb.route.function.PartitionByMurmurHash">
      <property name="seed">0</property><!-- 默認是0-->
      <property name="count">2</property><!-- 要分片的數據庫節點數量,必須指定,不然無法分片-->
      <property name="virtualBucketTimes">160</property><!-- 一個實際的數據庫節點被映射爲這麼多虛擬節點,默認是160倍,也就是虛擬節點數是物理節點數的160倍-->
      <!--
      <property name="weightMapFile">weightMapFile</property>
                     節點的權重,沒有指定權重的節點默認是1。以properties文件的格式填寫,以從0開始到count-1的整數值也就是節點索引爲key,以節點權重值爲值。全部權重值必須是正整數,不然以1代替 -->
      <!--
      <property name="bucketMapPath">/etc/mycat/bucketMapPath</property>
                      用於測試時觀察各物理節點與虛擬節點的分佈狀況,若是指定了這個屬性,會把虛擬節點的murmur hash值與物理節點的映射按行輸出到這個文件,沒有默認值,若是不指定,就不會輸出任何東西 -->
  </function>
一致性hash預算有效解決了分佈式數據的擴容問題,前1-9中id規則都多少存在數據擴容難題,而10規則解決了數據擴容難點
相關文章
相關標籤/搜索