myctプローブ

16903 ワード

1:クライアントのインストール
yum install mysql 
2:サービス・エンドのインストール
yum install mysql-server 
3:mycaでは大文字と小文字を区別しないことが求められます
my.cnf(/etc/my.cnf)の[mysqld]セグメントで増加:lower_case_table_names=1
4:mysqlの起動
service mysqld start 
5:ユーザーの作成
mysqladmin -u root password 110110 
6:mysqlにログイン
mysql -u root; 
7:リモートログイン権限付与
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '110110' WITH GRANT OPTION;flush privileges; 
8:mycatをサーバにアップロード(java要求1.7以上)
9:mycaの起動
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にログインしてデータベースを構築します.データベース名db 1,db 2,db 3
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:グローバル・テーブル

各行は各スライスに同時に記録される
 
2:列挙
schema.xml

rule.xml
sharding_id hash-int
partition-hash-int.txt
partition-hash-int.txt
10000=010010=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
# 64、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     
* 64、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