1、mycat介紹
- Mycat 是一個開源的分布式數據庫系統,但是由于真正的數據庫需要存儲引擎,而 Mycat 并沒有存 儲引擎,所以并不是完全意義的分布式數據庫系統。
- MyCat是目前最流行的基于Java語言編寫的數據庫中間件,也可以理解為是數據庫代理。
- 在架構體系中是位于數據庫和應用層之間的一個組件,并且對于應用層是透明的,即數據庫 感受不到mycat的存在,認為是直接連接的mysql數據庫(實際上是連接的mycat,mycat實現了mysql的原生協議)。
- MyCat是基于阿里開源的Cobar產品而研發,Cobar的穩定性、可靠性、優秀的架構和性能以及眾多成熟的使用案例使得MyCat變得非常的強大。
- mycat的三大功能:分庫分表、讀寫分離、主從切換
2、mycat配置
Mycat conf目錄下有三個重要配置文件,分別是:
- schema.xml:定義邏輯庫,表,分片節點等內容;
- rule.xml:定義分片規則;
- server.xml:定義用戶以及系統相關變量;
schema.xml:
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/"><!-- --><schema name="TESTDB" checkSQLschema="true" sqlMaxLimit="100" randomDataNode="dn1"><!-- auto sharding by id (long) --><!--splitTableNames 啟用<table name 屬性使用逗號分割配置多個表,即多個表使用這個配置--><table name="travelrecord,address" dataNode="dn1,dn2,dn3" rule="auto-sharding-long" splitTableNames ="true"/><!-- <table name="oc_call" primaryKey="ID" dataNode="dn1$0-743" rule="latest-month-calldate"/> --></schema><!-- 數據庫節點 --><dataNode name="dn1" dataHost="localhost1" database="db1" /><dataNode name="dn2" dataHost="localhost1" database="db2" /><dataNode name="dn3" dataHost="localhost1" database="db3" /><!--<dataNode name="dn4" dataHost="sequoiadb1" database="SAMPLE" /><dataNode name="jdbc_dn1" dataHost="jdbchost" database="db1" /><dataNode name="jdbc_dn2" dataHost="jdbchost" database="db2" /><dataNode name="jdbc_dn3" dataHost="jdbchost" database="db3" /> --><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="hostM1" url="localhost:3306" user="root"password="123456"></writeHost><!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> --></dataHost><!--<dataHost name="sequoiadb1" maxCon="1000" minCon="1" balance="0" dbType="sequoiadb" dbDriver="jdbc"><heartbeat> </heartbeat><writeHost host="hostM1" url="sequoiadb://1426587161.dbaas.sequoialab.net:11920/SAMPLE" user="jifeng" password="jifeng"></writeHost></dataHost><dataHost name="oracle1" maxCon="1000" minCon="1" balance="0" writeType="0" dbType="oracle" dbDriver="jdbc"> <heartbeat>select 1 from dual</heartbeat><connectionInitSql>alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss'</connectionInitSql><writeHost host="hostM1" url="jdbc:oracle:thin:@127.0.0.1:1521:nange" user="base" password="123456" > </writeHost> </dataHost><dataHost name="jdbchost" maxCon="1000" minCon="1" balance="0" writeType="0" dbType="mongodb" dbDriver="jdbc"><heartbeat>select user()</heartbeat><writeHost host="hostM" url="mongodb://192.168.0.99/test" user="admin" password="123456" ></writeHost> </dataHost><dataHost name="sparksql" maxCon="1000" minCon="1" balance="0" dbType="spark" dbDriver="jdbc"><heartbeat> </heartbeat><writeHost host="hostM1" url="jdbc:hive2://feng01:10000" user="jifeng" password="jifeng"></writeHost> </dataHost> --><!-- <dataHost name="jdbchost" maxCon="1000" minCon="10" balance="0" dbType="mysql"dbDriver="jdbc"> <heartbeat>select user()</heartbeat> <writeHost host="hostM1"url="jdbc:mysql://localhost:3306" user="root" password="123456"> </writeHost></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="sharding-by-date"><rule><columns>createTime</columns><algorithm>partbyday</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">3</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="partbyday"class="io.mycat.route.function.PartitionByDate"><property name="dateFormat">yyyy-MM-dd</property><property name="sNaturalDay">0</property><property name="sBeginDate">2014-01-01</property><property name="sEndDate">2014-01-31</property><property name="sPartionDay">10</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>
server.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:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://io.mycat/"><system><property name="nonePasswordLogin">0</property> <!-- 0為需要密碼登陸、1為不需要密碼登陸 ,默認為0,設置為1則需要指定默認賬戶--><property name="ignoreUnknownCommand">0</property><!-- 0遇上沒有實現的報文(Unknown command:),就會報錯、1為忽略該報文,返回ok報文。在某些mysql客戶端存在客戶端已經登錄的時候還會繼續發送登錄報文,mycat會報錯,該設置可以繞過這個錯誤--><property name="useHandshakeV10">1</property><property name="removeGraveAccent">1</property><property name="useSqlStat">0</property> <!-- 1為開啟實時統計、0為關閉 --><property name="useGlobleTableCheck">0</property> <!-- 1為開啟全加班一致性檢測、0為關閉 --><property name="sqlExecuteTimeout">300</property> <!-- SQL 執行超時 單位:秒--><property name="sequnceHandlerType">1</property><!--<property name="sequnceHandlerPattern">(?:(\s*next\s+value\s+for\s*MYCATSEQ_(\w+))(,|\)|\s)*)+</property>INSERT INTO `travelrecord` (`id`,user_id) VALUES ('next value for MYCATSEQ_GLOBAL',"xxx");--><!--必須帶有MYCATSEQ_或者 mycatseq_進入序列匹配流程 注意MYCATSEQ_有空格的情況--><property name="sequnceHandlerPattern">(?:(\s*next\s+value\s+for\s*MYCATSEQ_(\w+))(,|\)|\s)*)+</property><property name="subqueryRelationshipCheck">false</property> <!-- 子查詢中存在關聯查詢的情況下,檢查關聯字段中是否有分片字段 .默認 false --><property name="sequenceHanlderClass">io.mycat.route.sequence.handler.HttpIncrSequenceHandler</property><!-- <property name="useCompression">1</property>--> <!--1為開啟mysql壓縮協議--><!-- <property name="fakeMySQLVersion">5.6.20</property>--> <!--設置模擬的MySQL版本號--><!-- <property name="processorBufferChunk">40960</property> --><!-- <property name="processors">1</property> <property name="processorExecutor">32</property> --><!--默認為type 0: DirectByteBufferPool | type 1 ByteBufferArena | type 2 NettyBufferPool --><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="dataNodeIdleCheckPeriod">300000</property> 5 * 60 * 1000L; //連接空閑檢查<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">0</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>--><!--如果為 true的話 嚴格遵守隔離級別,不會在僅僅只有select語句的時候在事務中切換連接--><property name="strictTxIsolation">false</property><property name="useZKSwitch">true</property><!--如果為0的話,涉及多個DataNode的catlet任務不會跨線程執行--><property name="parallExecute">0</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">123456</property><!-- schemas:配置連接的邏輯庫 --><property name="schemas">TESTDB</property><property name="defaultSchema">TESTDB</property><!--No MyCAT Database selected 錯誤前會嘗試使用該schema作為schema,不設置則為null,報錯 --><!-- 表級 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">TESTDB</property><property name="readOnly">true</property><property name="defaultSchema">TESTDB</property></user></mycat:server>
既然是直接當做數據庫系統用,所以直接用navicat連接mycat即可。
3、mysql主從復制的原理
mysql自帶的功能:主從復制是把一臺主機的數據復制到其他一臺或多臺主機上;
主從復制原理:
- 主從同步過程中主服務器有一個工作線程I/O dump thread,從服務器有兩個工作線程I/O thread和SQL thread。
- 主庫把外界接收的SQL請求記錄到自己的binlog日志中,從庫的I/O thread去請求主庫的binlog日志,并將binlog日志寫到中繼日志中,然后從庫重做中繼日志的SQL語句。主庫通過I/O dump thread給從庫I/O thread傳送binlog日志。