mycat1.6.5 入門搭建
作為分散式資料庫中間外掛,mycat入門配置很簡單(我感覺是這樣,除了遇到了坑~~)先放幾個學習的時候看到的部落格,先留著。
mycat下載地址:http://dl.mycat.io/
mycat簡介:https://blog.csdn.net/wrs120/article/details/80417345
mycat schema.xml詳解:https://blog.csdn.net/linlinv3/article/details/51777324
mycat最主要配置檔案有三個,server.xml、schema.xml、與rule.xml。其中server.xml:是Mycat伺服器引數調整和使用者授權的配置檔案。schema.xml:是邏輯庫定義和表以及分片定義的配置檔案。rule.xml: 是分片規則的配置檔案,分片規則的具體一些引數資訊單獨存放為檔案。
server.xml配置,需要修改的有:登陸的賬戶密碼、邏輯資料庫。此處設定的登陸賬戶名密碼是root / xichuan123456,邏輯資料庫是db_xichuan。mycat預設的服務埠是8066,預設的管理埠是9066。
<?xml version="1.0" encoding="UTF-8"?> <!-- - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. --> <!DOCTYPE mycat:server SYSTEM "server.dtd"> <mycat:server xmlns:mycat="http://io.mycat/"> <system> <property name="nonePasswordLogin">0</property> <!-- 0為需要密碼登陸、1為不需要密碼登陸 ,預設為0,設定為1則需要指定預設賬戶--> <property name="useHandshakeV10">1</property> <property name="useSqlStat">1</property> <!-- 1為開啟實時統計、0為關閉 --> <property name="useGlobleTableCheck">1</property> <!-- 1為開啟全加班一致性檢測、0為關閉 --> <property name="sequnceHandlerType">2</property> <property name="subqueryRelationshipCheck">false</property> <!-- 子查詢中存在關聯查詢的情況下,檢查關聯欄位中是否有分片欄位 .預設 false --> <property name="processorBufferPoolType">0</property> <!--預設是65535 64K 用於sql解析時最大文字長度 --> <!--<property name="maxStringLiteralLength">65535</property>--> <!--<property name="sequnceHandlerType">0</property>--> <!--<property name="backSocketNoDelay">1</property>--> <!--<property name="frontSocketNoDelay">1</property>--> <!--<property name="processorExecutor">16</property>--> <!-- <property name="serverPort">8066</property> <property name="managerPort">9066</property> <property name="idleTimeout">300000</property> <property name="bindIp">0.0.0.0</property> <property name="frontWriteQueueSize">4096</property> <property name="processors">32</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">64k</property> <!-- 單位為k --> <property name="spillsFileBufferSize">1k</property> <property name="useStreamOutput">0</property> <!-- 單位為m --> <property name="systemReserveMemorySize">384m</property> <!--是否採用zookeeper協調切換 --> <property name="useZKSwitch">false</property> <!-- XA Recovery Log日誌路徑 --> <!--<property name="XARecoveryLogBaseDir">./</property>--> <!-- XA Recovery Log日誌名稱 --> <!--<property name="XARecoveryLogBaseName">tmlog</property>--> </system> <!-- 全域性SQL防火牆設定 --> <!--白名單可以使用萬用字元%或著*--> <!--例如<host host="127.0.0.*" user="root"/>--> <!--例如<host host="127.0.*" user="root"/>--> <!--例如<host host="127.*" user="root"/>--> <!--例如<host host="1*7.*" user="root"/>--> <!--這些配置情況下對於127.0.0.1都能以root賬戶登入--> <!-- <firewall> <whitehost> <host host="1*7.0.0.*" user="root"/> </whitehost> <blacklist check="false"> </blacklist> </firewall> --> <user name="root" defaultAccount="true"> <property name="password">xichuan123456</property> <property name="schemas">db_xichuan</property> <!-- 表級 DML 許可權設定 --> <!-- <privileges check="false"> <schema name="TESTDB" dml="0110" > <table name="tb01" dml="0000"></table> <table name="tb02" dml="1111"></table> </schema> </privileges> --> </user> <user name="user"> <property name="password">user</property> <property name="schemas">db_xichuan</property> <property name="readOnly">true</property> </user> </mycat:server>
配置schema.xml。其中邏輯資料庫db_xichuan中配置兩張表:user、info。其中與三個節點,dn1、dn2、dn3,他們使用的連線源都是localhost1,對應的資料庫分別是:mycat1、mycat2、mycat3。且邏輯表user對應的是mycat1資料庫中的user表,邏輯表info對應的是mycat2、mycat3資料庫中的info表。rule="mod-long"代表切分規則。
checkSQLschema="true",不然會遇到錯誤,解決問題部落格:https://blog.csdn.net/zc_ad/article/details/83540995
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<!--注意 checkSQLschema="true",不然會報錯-->
<schema name="db_xichuan" checkSQLschema="true" sqlMaxLimit="100" >
<table name="user" primaryKey="id" dataNode="dn1" type="global" />
<table name="info" primaryKey="id" dataNode="dn2,dn3" rule="mod-long" />
</schema>
<dataNode name="dn1" dataHost="localhost1" database="mycat1" />
<dataNode name="dn2" dataHost="localhost1" database="mycat2" />
<dataNode name="dn3" dataHost="localhost1" database="mycat3" />
<!--真正資料庫連線地址-->
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="0"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostS1" url="12.12.12.12:3306" user="root"
password="xichuan123456" />
</dataHost>
</mycat:schema>
他們的對應關係如下簡圖:
rule.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!-- - - Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License. - You
may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0
- - Unless required by applicable law or agreed to in writing, software -
distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the
License for the specific language governing permissions and - limitations
under the License. -->
<!DOCTYPE mycat:rule SYSTEM "rule.dtd">
<mycat:rule xmlns:mycat="http://io.mycat/">
<tableRule name="rule1">
<rule>
<columns>id</columns>
<algorithm>func1</algorithm>
</rule>
</tableRule>
<tableRule name="rule2">
<rule>
<columns>user_id</columns>
<algorithm>func1</algorithm>
</rule>
</tableRule>
<tableRule name="sharding-by-intfile">
<rule>
<columns>sharding_id</columns>
<algorithm>hash-int</algorithm>
</rule>
</tableRule>
<tableRule name="auto-sharding-long">
<rule>
<columns>id</columns>
<algorithm>rang-long</algorithm>
</rule>
</tableRule>
<tableRule name="mod-long">
<rule>
<columns>id</columns>
<algorithm>mod-long</algorithm>
</rule>
</tableRule>
<tableRule name="sharding-by-murmur">
<rule>
<columns>id</columns>
<algorithm>murmur</algorithm>
</rule>
</tableRule>
<tableRule name="crc32slot">
<rule>
<columns>id</columns>
<algorithm>crc32slot</algorithm>
</rule>
</tableRule>
<tableRule name="sharding-by-month">
<rule>
<columns>create_time</columns>
<algorithm>partbymonth</algorithm>
</rule>
</tableRule>
<tableRule name="latest-month-calldate">
<rule>
<columns>calldate</columns>
<algorithm>latestMonth</algorithm>
</rule>
</tableRule>
<tableRule name="auto-sharding-rang-mod">
<rule>
<columns>id</columns>
<algorithm>rang-mod</algorithm>
</rule>
</tableRule>
<tableRule name="jch">
<rule>
<columns>id</columns>
<algorithm>jump-consistent-hash</algorithm>
</rule>
</tableRule>
<function name="murmur"
class="io.mycat.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>
<function name="crc32slot"
class="io.mycat.route.function.PartitionByCRC32PreSlot">
<property name="count">2</property><!-- 要分片的資料庫節點數量,必須指定,否則沒法分片 -->
</function>
<function name="hash-int"
class="io.mycat.route.function.PartitionByFileMap">
<property name="mapFile">partition-hash-int.txt</property>
</function>
<function name="rang-long"
class="io.mycat.route.function.AutoPartitionByLong">
<property name="mapFile">autopartition-long.txt</property>
</function>
<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
<!-- how many data nodes -->
<property name="count">2</property>
</function>
<function name="func1" class="io.mycat.route.function.PartitionByLong">
<property name="partitionCount">8</property>
<property name="partitionLength">128</property>
</function>
<function name="latestMonth"
class="io.mycat.route.function.LatestMonthPartion">
<property name="splitOneDay">24</property>
</function>
<function name="partbymonth"
class="io.mycat.route.function.PartitionByMonth">
<property name="dateFormat">yyyy-MM-dd</property>
<property name="sBeginDate">2015-01-01</property>
</function>
<function name="rang-mod" class="io.mycat.route.function.PartitionByRangeMod">
<property name="mapFile">partition-range-mod.txt</property>
</function>
<function name="jump-consistent-hash" class="io.mycat.route.function.PartitionByJumpConsistentHash">
<property name="totalBuckets">3</property>
</function>
</mycat:rule>
在官網上下載mycat
1.使用命令進行解壓:tar xzvf Mycat-server-1.6.5-release-20180122220033-linux.tar.gz,然後進入conf目錄下,將上面三個檔案覆蓋,然後啟動mycat。
2.進入mycat/bin目錄 ,
啟動命令:./mycat start
停止命令:./mycat stop
重啟命令:./mycat restart
檢視狀態命令:./mycat status。
3.進入mycat/log目錄,檢視日誌:tail -f mycat.log。檢視是否報錯。
4.使用Navicat進行連線
附件:資料庫以及表的建立
create DATABASE mycat1;
use mycat1;
CREATE TABLE `user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
);
INSERT INTO `user` VALUES (1, 'mycat1_user_1');
INSERT INTO `user` VALUES (2, 'mycat1_user_2');
create DATABASE mycat2;
use mycat2;
CREATE TABLE `info` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ;
INSERT INTO `info` VALUES (14, 'mycat2_info_1');
INSERT INTO `info` VALUES (15, 'mycat2_info_2');
create DATABASE mycat3;
use mycat3;
CREATE TABLE `info` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ;
INSERT INTO `info` VALUES (14, 'mycat3_info_1');
INSERT INTO `info` VALUES (15, 'mycat3_info_2');
搭建成功後的效果如下: