mysql8.0主從復(fù)制
http://www.reibang.com/p/af1690d8d045
讀寫(xiě)分離需要借助主從復(fù)制急黎,因?yàn)槿绻恍枰直淼臄?shù)據(jù),就需要將主DB的數(shù)據(jù)復(fù)制到從機(jī)上(也可以通過(guò)mycat設(shè)置成全局表)
通過(guò)mycat來(lái)控制權(quán)限侧到,主機(jī)有讀和寫(xiě)操作的權(quán)限勃教,從機(jī)只有讀操作權(quán)限
數(shù)據(jù)庫(kù)分表分庫(kù),讀寫(xiě)分離的出現(xiàn)匠抗,主要是數(shù)據(jù)庫(kù)性能瓶頸
影響數(shù)據(jù)庫(kù)性能的主要原因:
1.單表數(shù)據(jù)量太大
2.并發(fā)時(shí)的數(shù)據(jù)庫(kù)連接數(shù)過(guò)大故源,單臺(tái)數(shù)據(jù)庫(kù)節(jié)點(diǎn)支撐不了
3.硬件資源(QPS/TPS)
水平分表和垂直分庫(kù)
以上1 2 原因影響數(shù)據(jù)庫(kù)性能瓶頸的主要解決方法就是水平分表和垂直分庫(kù)
1.一般單表達(dá)到500萬(wàn)數(shù)據(jù)量和占2G硬盤(pán)內(nèi)存時(shí)就會(huì)考慮水平分表和垂直分庫(kù),每個(gè)數(shù)據(jù)庫(kù)有相同的表結(jié)構(gòu)汞贸,可以通過(guò)取模算法來(lái)判斷數(shù)據(jù)放到哪個(gè)數(shù)據(jù)庫(kù)表中(id%數(shù)據(jù)庫(kù)節(jié)點(diǎn)數(shù))
2.用戶訪問(wèn)數(shù)多時(shí)绳军,當(dāng)臺(tái)數(shù)據(jù)庫(kù)節(jié)點(diǎn)無(wú)法支撐時(shí)就會(huì)考慮業(yè)務(wù)分庫(kù)和讀寫(xiě)分離
目前主流的微服務(wù)架構(gòu)就是通過(guò)業(yè)務(wù)來(lái)分庫(kù)(比如user庫(kù)、order庫(kù))矢腻,可以減少單臺(tái)數(shù)據(jù)庫(kù)訪問(wèn)量
讀寫(xiě)分離门驾,通過(guò)mycat來(lái)控制,主機(jī)有讀和寫(xiě)操作的權(quán)限多柑,從機(jī)只有讀操作權(quán)限
mycat的一些概念
分表分庫(kù)和讀寫(xiě)分離需要用到mycat中間件來(lái)協(xié)助奶是。mycat內(nèi)置分表的方案(取模、時(shí)間戳等)竣灌,有全局表聂沙、分片表和ER表等概念
全局表具有以下特性:
? 全局表的插入、更新操作會(huì)實(shí)時(shí)在所有節(jié)點(diǎn)上執(zhí)行初嘹,保持各個(gè)分片的數(shù)據(jù)一致性
? 全局表的查詢操作及汉,只從一個(gè)節(jié)點(diǎn)獲取
? 全局表可以跟任何一個(gè)表進(jìn)行 JOIN 操作
比如 用戶等級(jí)表,權(quán)限表屯烦、數(shù)據(jù)字典表等可以用為全局表坷随。所有DB節(jié)點(diǎn)上都需要數(shù)據(jù)一致性
這些表不設(shè)置成全局表房铭,通過(guò)mysql主從復(fù)制來(lái)保證所有DB節(jié)點(diǎn)上都需要數(shù)據(jù)一致性也是可以的,mycat只做讀寫(xiě)分離温眉。
分片表:
數(shù)據(jù)量過(guò)大需要分表的數(shù)據(jù)庫(kù)表理解成分片表育叁,比如用戶表,訂單表芍殖。這些表數(shù)據(jù)庫(kù)都很大
ER 分片表
有一類(lèi)業(yè)務(wù),例如訂單(order)跟訂單明細(xì)(order_detail),明細(xì)表會(huì)依賴于訂單谴蔑,也就是說(shuō)會(huì)存在表的主
從關(guān)系豌骏,這類(lèi)似業(yè)務(wù)的切分可以抽象出合適的切分規(guī)則,比如根據(jù)用戶 ID 切分,其他相關(guān)的表都依賴于用戶 ID隐锭,再或者根據(jù)訂單 ID 切分窃躲,總之部分業(yè)務(wù)總會(huì)可以抽象出父子關(guān)系的表。這類(lèi)表適用于 ER 分片表钦睡,子表的記錄與所關(guān)聯(lián)的父表記錄存放在同一個(gè)數(shù)據(jù)分片上蒂窒,避免數(shù)據(jù) Join 跨庫(kù)操作。
比如:訂單表分表分庫(kù)時(shí)荞怒。001訂單在A節(jié)點(diǎn)(數(shù)據(jù)庫(kù))上洒琢,001訂單的明細(xì)表也需要在A節(jié)點(diǎn)上,才能作關(guān)聯(lián)查詢褐桌。這時(shí)訂單表就是分表片衰抑,訂單明細(xì)表就是子表(ER分片表)
但是ER分片表在訂單表進(jìn)行擴(kuò)容或縮容時(shí)很難進(jìn)行遷移。所有一般會(huì)把ER分片表也設(shè)置成分片表荧嵌,通過(guò)訂單明細(xì)關(guān)聯(lián)的訂單號(hào)來(lái)進(jìn)行分片(意思是訂單明細(xì)表的分片字段就是訂單表的分片字段(訂單明細(xì)表的外鍵))
mycat的概念在mycat官方文檔有說(shuō)明
http://www.mycat.io/document/mycat-definitive-guide.pdf
centos7搭建mycat并實(shí)現(xiàn)分表分庫(kù)和讀寫(xiě)分離
mycat安裝(linux版本)前的準(zhǔn)備:
需要先安裝jdk呛踊,和mysql,我用的jdk1.8和mysql8.0
實(shí)現(xiàn)分表分庫(kù)和讀寫(xiě)分離的示例場(chǎng)景:
1.有兩臺(tái)mysql服務(wù)器啦撮,搭建一主一從的主從復(fù)制(只復(fù)制db_store庫(kù))谭网。搭建的操作在文章最開(kāi)頭的鏈接。
2.搭建好主從復(fù)制后赃春,在主節(jié)點(diǎn)上創(chuàng)建兩個(gè)db_store(配置了主從復(fù)制的庫(kù))和db_user數(shù)據(jù)庫(kù)愉择,并在db_store庫(kù)創(chuàng)建store表(這個(gè)表通過(guò)主從復(fù)制來(lái)同步節(jié)點(diǎn)的數(shù)據(jù),mycat對(duì)這個(gè)表只作讀寫(xiě)分離作用)聘鳞。
在db_user庫(kù)創(chuàng)建data_dictionary(全局表)薄辅、user_address(ER分片表)、和users(分片表)
準(zhǔn)備工作完成抠璃,開(kāi)始搭建mycat
http://www.mycat.io/下載mycat1.6.7.1-release版本
解壓mycat
tar -zxvf Mycat-server-1.6.7.1-release-20190627191042-linux.tar.gz
主要修改mycat的bin目錄下的三個(gè)配置文件站楚,配置文件的參數(shù)解析看mycat的文檔,本文中有鏈接
第一個(gè):schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<!-- schema 配置mycat的邏輯庫(kù)搏嗡,與真實(shí)庫(kù)對(duì)應(yīng) -->
<schema name="db_store" checkSQLschema="false" sqlMaxLimit="100">
<table name="store" dataNode="db_store_dataNode" primaryKey="storeID"/>
</schema>
<schema name="db_user" checkSQLschema="false" sqlMaxLimit="100">
<table name="data_dictionary" type="global" dataNode="db_user_dataNode1,db_user_dataNode2" primaryKey="dataDictionaryID"/>
<table name="users" dataNode="db_user_dataNode$1-2" rule="mod-userID-long" primaryKey="userID">
<childTable name="user_address" joinKey="userID" parentKey="userID" primaryKey="addressID"/>
</table>
</schema>
<!-- 節(jié)點(diǎn)配置 -->
<!-- db_store -->
<dataNode name="db_store_dataNode" dataHost="db_storeHOST" database="db_store" />
<!-- db_user -->
<dataNode name="db_user_dataNode1" dataHost="db_userHOST1" database="db_user" />
<dataNode name="db_user_dataNode2" dataHost="db_userHOST2" database="db_user" />
<!-- 節(jié)點(diǎn)主機(jī)配置窿春、dataHost 物理數(shù)據(jù)庫(kù)拉一,真正存儲(chǔ)數(shù)據(jù)的數(shù)據(jù)庫(kù) -->
<!-- 配置db_store的節(jié)點(diǎn)主機(jī) -->
<dataHost name="db_storeHOST" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<!-- can have multi write hosts -->
<writeHost host="hostM1" url="192.168.79.142:3306" user="root" password="123456">
<!-- can have multi read hosts -->
<readHost host="hostS1" url="192.168.79.144:3306" user="root" password="123456" />
</writeHost>
</dataHost>
<!-- 配置db_user的節(jié)點(diǎn)主機(jī),users表需要分表分庫(kù)操作旧乞,所有要配置兩個(gè)dataHost-->
<dataHost name="db_userHOST1" maxCon="1000" minCon="10" balance="0"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="userHost1" url="192.168.79.142:3306" user="root" password="123456">
</writeHost>
</dataHost>
<dataHost name="db_userHOST2" 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="userHost2" url="192.168.79.144:3306" user="root" password="123456">
</writeHost>
</dataHost>
</mycat:schema>
第二個(gè):rule.xml
只需要增加mod-userID-long的tableRule蔚润,這個(gè)配置是給schema.xml引用的(rule="mod-userID-long"),這是定義users表的分片規(guī)則 <algorithm>mod-long</algorithm>表示通過(guò)schema.xml的primaryKey="userID"這個(gè)字段來(lái)取模進(jìn)行分片
<tableRule name="mod-userID-long">
<rule>
<columns>userID</columns>
<algorithm>mod-long</algorithm>
</rule>
</tableRule>
和修改<property name="count">2</property>的值為2尺栖,表示分片到兩個(gè)數(shù)據(jù)庫(kù)節(jié)點(diǎn)上嫡纠。
<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
<!-- how many data nodes -->
<property name="count">2</property>
</function>
整個(gè)rule.xml配置(只增加一處和修改一處,其他都是默認(rèn)的)
<?xml version="1.0" encoding="UTF-8"?>
<!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="mod-userID-long">
<rule>
<columns>userID</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><!-- 默認(rèn)是0 -->
<property name="count">2</property><!-- 要分片的數(shù)據(jù)庫(kù)節(jié)點(diǎn)數(shù)量延赌,必須指定除盏,否則沒(méi)法分片 -->
<property name="virtualBucketTimes">160</property><!-- 一個(gè)實(shí)際的數(shù)據(jù)庫(kù)節(jié)點(diǎn)被映射為這么多虛擬節(jié)點(diǎn),默認(rèn)是160倍挫以,也就是虛擬節(jié)點(diǎn)數(shù)是物理節(jié)點(diǎn)數(shù)的160倍 -->
<!-- <property name="weightMapFile">weightMapFile</property> 節(jié)點(diǎn)的權(quán)重者蠕,沒(méi)有指定權(quán)重的節(jié)點(diǎn)默認(rèn)是1。以properties文件的格式填寫(xiě)掐松,以從0開(kāi)始到count-1的整數(shù)值也就是節(jié)點(diǎn)索引為key踱侣,以節(jié)點(diǎn)權(quán)重值為值。所有權(quán)重值必須是正整數(shù)大磺,否則以1代替 -->
<!-- <property name="bucketMapPath">/etc/mycat/bucketMapPath</property>
用于測(cè)試時(shí)觀察各物理節(jié)點(diǎn)與虛擬節(jié)點(diǎn)的分布情況抡句,如果指定了這個(gè)屬性,會(huì)把虛擬節(jié)點(diǎn)的murmur hash值與物理節(jié)點(diǎn)的映射按行輸出到這個(gè)文件杠愧,沒(méi)有默認(rèn)值玉转,如果不指定,就不會(huì)輸出任何東西 -->
</function>
<function name="crc32slot"
class="io.mycat.route.function.PartitionByCRC32PreSlot">
</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>
第三個(gè):server.xml
設(shè)置mycat的登陸賬號(hào)root和密碼123456殴蹄。mycat邏輯庫(kù)db_store,db_user
<user name="root" defaultAccount="true">
<property name="password">123456</property>
<property name="schemas">db_store,db_user</property>
整個(gè)server.xml配置文件參考
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mycat:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://io.mycat/">
<system>
<property name="nonePasswordLogin">0</property> <!-- 0為需要密碼登陸究抓、1為不需要密碼登陸 ,默認(rèn)為0,設(shè)置為1則需要指定默認(rèn)賬戶-->
<property name="useHandshakeV10">1</property>
<property name="useSqlStat">0</property> <!-- 1為開(kāi)啟實(shí)時(shí)統(tǒng)計(jì)袭灯、0為關(guān)閉 -->
<property name="useGlobleTableCheck">0</property> <!-- 1為開(kāi)啟全加班一致性檢測(cè)刺下、0為關(guān)閉 -->
<property name="sequnceHandlerType">2</property>
<!--<property name="sequnceHandlerPattern">(?:(\s*next\s+value\s+for\s*MYCATSEQ_(\w+))(,|\)|\s)*)+</property>-->
<!--必須帶有MYCATSEQ_或者 mycatseq_進(jìn)入序列匹配流程 注意MYCATSEQ_有空格的情況-->
<property name="sequnceHandlerPattern">(?:(\s*next\s+value\s+for\s*MYCATSEQ_(\w+))(,|\)|\s)*)+</property>
<property name="subqueryRelationshipCheck">false</property> <!-- 子查詢中存在關(guān)聯(lián)查詢的情況下,檢查關(guān)聯(lián)字段中是否有分片字段 .默認(rèn) false -->
<!-- <property name="useCompression">1</property>--> <!--1為開(kāi)啟mysql壓縮協(xié)議-->
<!-- <property name="fakeMySQLVersion">5.6.20</property>--> <!--設(shè)置模擬的MySQL版本號(hào)-->
<!-- <property name="processorBufferChunk">40960</property> -->
<!--
<property name="processors">1</property>
<property name="processorExecutor">32</property>
-->
<!--默認(rèn)為type 0: DirectByteBufferPool | type 1 ByteBufferArena | type 2 NettyBufferPool -->
<property name="processorBufferPoolType">0</property>
<!--默認(rèn)是65535 64K 用于sql解析時(shí)最大文本長(zhǎng)度 -->
<!--<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> -->
<!--分布式事務(wù)開(kāi)關(guān),0為不過(guò)濾分布式事務(wù)稽荧,1為過(guò)濾分布式事務(wù)(如果分布式事務(wù)內(nèi)只涉及全局表橘茉,則不過(guò)濾),2為不過(guò)濾分布式事務(wù),但是記錄分布式事務(wù)日志-->
<property name="handleDistributedTransactions">0</property>
<!--
off heap for merge/order/group/limit 1開(kāi)啟 0關(guān)閉
-->
<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協(xié)調(diào)切換 -->
<property name="useZKSwitch">false</property>
<!-- XA Recovery Log日志路徑 -->
<!--<property name="XARecoveryLogBaseDir">./</property>-->
<!-- XA Recovery Log日志名稱(chēng) -->
<!--<property name="XARecoveryLogBaseName">tmlog</property>-->
<!--如果為 true的話 嚴(yán)格遵守隔離級(jí)別,不會(huì)在僅僅只有select語(yǔ)句的時(shí)候在事務(wù)中切換連接-->
<property name="strictTxIsolation">false</property>
<property name="useZKSwitch">true</property>
</system>
<!-- 全局SQL防火墻設(shè)置 -->
<!--白名單可以使用通配符%或著*-->
<!--例如<host host="127.0.0.*" user="root"/>-->
<!--例如<host host="127.0.*" user="root"/>-->
<!--例如<host host="127.*" user="root"/>-->
<!--例如<host host="1*7.*" user="root"/>-->
<!--這些配置情況下對(duì)于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>
<property name="schemas">db_store,db_user</property>
<!-- 表級(jí) DML 權(quán)限設(shè)置 -->
<!--
<privileges check="false">
<schema name="TESTDB" dml="0110" >
<table name="tb01" dml="0000"></table>
<table name="tb02" dml="1111"></table>
</schema>
</privileges>
-->
</user>
</mycat:server>
配置好以上三個(gè)文件姨丈,mycat的搭建就完成了
進(jìn)入mycat的bin目錄啟動(dòng)mycat
./mycat start
進(jìn)入mycat的logs目錄可以查詢到mycat的日志畅卓,conf目錄下的log4j.xml可以設(shè)置mycat的日志級(jí)別,默認(rèn)是info蟋恬。如果要打印出select語(yǔ)句的日志翁潘,就要修改成debug
通過(guò)Navicat連接mycat,mycat端口號(hào)為8066
測(cè)試環(huán)節(jié)歼争,所有操作都是連接在mycat庫(kù)操作的拜马,然后mycat會(huì)轉(zhuǎn)發(fā)到真實(shí)數(shù)據(jù)庫(kù)中
1.在users表上添加5條記錄渗勘,測(cè)試一下分表分庫(kù)是否成功
mycat users表的數(shù)據(jù)
142數(shù)據(jù)庫(kù)users表的數(shù)據(jù)
144數(shù)據(jù)庫(kù)users表的數(shù)據(jù)
可以看到,數(shù)據(jù)分片已經(jīng)成功俩莽,偶數(shù)userID在142數(shù)據(jù)庫(kù)旺坠,奇數(shù)在144數(shù)據(jù)庫(kù)。查詢數(shù)據(jù)時(shí)扮超,mycat會(huì)自動(dòng)匹配到對(duì)應(yīng)的數(shù)據(jù)庫(kù)
測(cè)試user_address ER表的數(shù)據(jù)是否與users表對(duì)應(yīng)同一個(gè)DB節(jié)點(diǎn)
mycat user_address表的數(shù)據(jù)
142數(shù)據(jù)庫(kù)user_address表的數(shù)據(jù)
144數(shù)據(jù)庫(kù)user_address表的數(shù)據(jù)
以上數(shù)據(jù)與users表所有的節(jié)點(diǎn)數(shù)據(jù)是對(duì)應(yīng)的取刃,查詢用戶的地址信息就可以關(guān)聯(lián)查詢了,這就是ER表的作用
測(cè)試data_dictionary 全局表的數(shù)據(jù)是否在所有節(jié)點(diǎn)上都相同
插入五條數(shù)據(jù),在mycat庫(kù)、142數(shù)據(jù)庫(kù)舒憾、144數(shù)據(jù)庫(kù)上都是相同的
db_user數(shù)據(jù)庫(kù)只作了分片操作,沒(méi)有作mycat的主從配置,當(dāng)某一個(gè)節(jié)點(diǎn)掛了濒翻,mycat的邏輯庫(kù)就連不上了
db_store數(shù)據(jù)庫(kù)測(cè)試屁柏,該數(shù)據(jù)庫(kù)配置了主從復(fù)制,在mycat庫(kù)中添加4條數(shù)據(jù)有送,看看所有節(jié)點(diǎn)的數(shù)據(jù)是否相同
3個(gè)數(shù)據(jù)庫(kù)的數(shù)據(jù)都是相同的淌喻,這里就不一一帖出來(lái)了。主從復(fù)制配置成功雀摘。似類(lèi)于全局表的功能裸删。
在上面的schema.xml配置了讀寫(xiě)分離
<dataHost name="db_storeHOST" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<!-- can have multi write hosts -->
<writeHost host="hostM1" url="192.168.79.142:3306" user="root" password="123456">
<!-- can have multi read hosts -->
<readHost host="hostS1" url="192.168.79.144:3306" user="root" password="123456" />
</writeHost>
</dataHost>
mycat配置了142是主機(jī)(writeHost),144是從機(jī)(readHost)阵赠。142可以讀和寫(xiě)涯塔,144只能讀。 默認(rèn)讀操作是在144上執(zhí)行的清蚀,當(dāng)144掛了之后匕荸,讀操作會(huì)到142執(zhí)行。但如果142掛了枷邪,讀和寫(xiě)操作都不能執(zhí)行了榛搔。
相當(dāng)于mycat配置的主機(jī)(writeHost)必須活著
mycat在java的整合的方式與mysql的方式是一樣的,只要引入mysql和jdbc的包东揣,只是端口改成8066