學(xué)會(huì)數(shù)據(jù)庫(kù)讀寫分離振定、分表分庫(kù)——用Mycat必怜,這一篇就夠了!(轉(zhuǎn)載)

系統(tǒng)開發(fā)中后频,數(shù)據(jù)庫(kù)是非常重要的一個(gè)點(diǎn)梳庆。除了程序的本身的優(yōu)化,如:SQL語(yǔ)句優(yōu)化卑惜、代碼優(yōu)化膏执,數(shù)據(jù)庫(kù)的處理本身優(yōu)化也是非常重要的。主從露久、熱備更米、分表分庫(kù)等都是系統(tǒng)發(fā)展遲早會(huì)遇到的技術(shù)問(wèn)題問(wèn)題。Mycat是一個(gè)廣受好評(píng)的數(shù)據(jù)庫(kù)中間件毫痕,已經(jīng)在很多產(chǎn)品上進(jìn)行使用了征峦。希望通過(guò)這篇文章的介紹,能學(xué)會(huì)Mycat的使用消请。

安裝

Mycat官網(wǎng):http://www.mycat.io/ 可以了解下Mycat的背景和應(yīng)用情況栏笆,這樣使用起來(lái)比較有信心。

Mycat下載地址:http://dl.mycat.io/ 官網(wǎng)有個(gè)文檔臊泰,屬于詳細(xì)的介紹蛉加,初次入門,看起來(lái)比較花時(shí)間。

下載: 建議大家選擇 1.6-RELEASE 版本针饥,畢竟是比較穩(wěn)定的版本厂抽。

安裝: 根據(jù)不同的系統(tǒng)選擇不同的版本。包括linux丁眼、windows筷凤、mac,作者考慮還是非常周全的,當(dāng)然户盯,也有源碼版的嵌施。(ps:源碼版的下載后饲化,只要配置正確莽鸭,就可以正常運(yùn)行調(diào)試,這個(gè)贊一下吃靠。)

image

Mycat的安裝其實(shí)只要解壓下載的目錄就可以了硫眨,非常簡(jiǎn)單。 安裝完成后巢块,目錄如下:

目錄 說(shuō)明
bin mycat命令礁阁,啟動(dòng)、重啟族奢、停止等
catlet catlet為Mycat的一個(gè)擴(kuò)展功能
conf Mycat 配置信息,重點(diǎn)關(guān)注
lib Mycat引用的jar包姥闭,Mycat是java開發(fā)的
logs 日志文件,包括Mycat啟動(dòng)的日志和運(yùn)行的日志越走。

配置

Mycat的配置文件都在conf目錄里面棚品,這里介紹幾個(gè)常用的文件:

文件 說(shuō)明
server.xml Mycat的配置文件,設(shè)置賬號(hào)廊敌、參數(shù)等
schema.xml Mycat對(duì)應(yīng)的物理數(shù)據(jù)庫(kù)和數(shù)據(jù)庫(kù)表的配置
rule.xml Mycat分片(分庫(kù)分表)規(guī)則

Mycat的架構(gòu)其實(shí)很好理解铜跑,Mycat是代理,Mycat后面就是物理數(shù)據(jù)庫(kù)骡澈。和Web服務(wù)器的Nginx類似锅纺。對(duì)于使用者來(lái)說(shuō),訪問(wèn)的都是Mycat肋殴,不會(huì)接觸到后端的數(shù)據(jù)庫(kù)囤锉。 我們現(xiàn)在做一個(gè)主從、讀寫分離护锤,簡(jiǎn)單分表的示例官地。結(jié)構(gòu)如下圖:
image
服務(wù)器 IP 說(shuō)明
Mycat 192.168.0.2 mycat服務(wù)器,連接數(shù)據(jù)庫(kù)時(shí)蔽豺,連接此服務(wù)器
database1 192.168.0.3 物理數(shù)據(jù)庫(kù)1区丑,真正存儲(chǔ)數(shù)據(jù)的數(shù)據(jù)庫(kù)
database2 192.168.0.4 物理數(shù)據(jù)庫(kù)2,真正存儲(chǔ)數(shù)據(jù)的數(shù)據(jù)庫(kù)

Mycat作為主數(shù)據(jù)庫(kù)中間件,肯定是與代碼弱關(guān)聯(lián)的沧侥,所以代碼是不用修改的可霎,使用Mycat后,連接數(shù)據(jù)庫(kù)是不變的宴杀,默認(rèn)端口是8066癣朗。連接方式和普通數(shù)據(jù)庫(kù)一樣,如:jdbc:mysql://192.168.0.2:8066/

server.xml

示例

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="xml" contenteditable="false" cid="n65" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; page-break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: inherit inherit; background-repeat: inherit inherit;">Copy<user name="test">
 <property name="password">test</property>
 <property name="schemas">lunch</property>
 <property name="readOnly">false</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></pre>

重點(diǎn)關(guān)注下面這段旺罢,其他默認(rèn)即可旷余。

參數(shù) 說(shuō)明
user 用戶配置節(jié)點(diǎn)
--name 登錄的用戶名,也就是連接Mycat的用戶名
--password 登錄的密碼扁达,也就是連接Mycat的密碼
--schemas 數(shù)據(jù)庫(kù)名正卧,這里會(huì)和schema.xml中的配置關(guān)聯(lián),多個(gè)用逗號(hào)分開跪解,例如需要這個(gè)用戶需要管理兩個(gè)數(shù)據(jù)庫(kù)db1,db2炉旷,則配置db1,dbs
--privileges 配置用戶針對(duì)表的增刪改查的權(quán)限,具體見文檔吧

我這里配置了一個(gè)賬號(hào)test 密碼也是test,針對(duì)數(shù)據(jù)庫(kù)lunch,讀寫權(quán)限都有叉讥,沒(méi)有針對(duì)表做任何特殊的權(quán)限窘行。

schema.xml schema.xml是最主要的配置項(xiàng),首先看我的配置文件图仓。

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="xml" contenteditable="false" cid="n88" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; page-break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: inherit inherit; background-repeat: inherit inherit;">Copy<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
?
<!-- 數(shù)據(jù)庫(kù)配置罐盔,與server.xml中的數(shù)據(jù)庫(kù)對(duì)應(yīng) -->
 <schema name="lunch" checkSQLschema="false" sqlMaxLimit="100">
 <table name="lunchmenu" dataNode="dn1"  />
 <table name="restaurant" dataNode="dn1"  />
 <table name="userlunch" dataNode="dn1"  />
 <table name="users" dataNode="dn1"  />
 <table name="dictionary" primaryKey="id" autoIncrement="true" dataNode="dn1,dn2"  rule="mod-long" />
?

 </schema>
?
<!-- 分片配置 -->
 <dataNode name="dn1" dataHost="test1" database="lunch" />
 <dataNode name="dn2" dataHost="test2" database="lunch" />
?
<!-- 物理數(shù)據(jù)庫(kù)配置 -->
 <dataHost name="test1" maxCon="1000" minCon="10" balance="0"  writeType="0" dbType="mysql" dbDriver="native">
 <heartbeat>select user();</heartbeat>
 <writeHost host="hostM1" url="192.168.0.2:3306" user="root" password="123456">
 </writeHost>
 </dataHost>
?
 <dataHost name="test2" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native">
 <heartbeat>select user();</heartbeat>
 <writeHost host="hostS1" url="192.168.0.3:3306" user="root" password="123456">
 </writeHost>
 </dataHost>
?
</mycat:schema></pre>
參數(shù) 說(shuō)明
schema 數(shù)據(jù)庫(kù)設(shè)置,此數(shù)據(jù)庫(kù)為邏輯數(shù)據(jù)庫(kù)救崔,name與server.xml中schema對(duì)應(yīng)
dataNode 分片信息惶看,也就是分庫(kù)相關(guān)配置
dataHost 物理數(shù)據(jù)庫(kù),真正存儲(chǔ)數(shù)據(jù)的數(shù)據(jù)庫(kù)

每個(gè)節(jié)點(diǎn)的屬性逐一說(shuō)明:

schema:

屬性 說(shuō)明
name 邏輯數(shù)據(jù)庫(kù)名帚豪,與server.xml中的schema對(duì)應(yīng)
checkSQLschema 數(shù)據(jù)庫(kù)前綴相關(guān)設(shè)置碳竟,建議看文檔,這里暫時(shí)設(shè)為folse
sqlMaxLimit select 時(shí)默認(rèn)的limit狸臣,避免查詢?nèi)?/td>

table:

屬性 說(shuō)明
name 表名莹桅,物理數(shù)據(jù)庫(kù)中表名
dataNode 表存儲(chǔ)到哪些節(jié)點(diǎn),多個(gè)節(jié)點(diǎn)用逗號(hào)分隔烛亦。節(jié)點(diǎn)為下文dataNode設(shè)置的name
primaryKey 主鍵字段名诈泼,自動(dòng)生成主鍵時(shí)需要設(shè)置
autoIncrement 是否自增
rule 分片規(guī)則名,具體規(guī)則下文rule詳細(xì)介紹

dataNode

屬性 說(shuō)明
name 節(jié)點(diǎn)名煤禽,與table中dataNode對(duì)應(yīng)
datahost 物理數(shù)據(jù)庫(kù)名铐达,與datahost中name對(duì)應(yīng)
database 物理數(shù)據(jù)庫(kù)中數(shù)據(jù)庫(kù)名

dataHost

屬性 說(shuō)明
name 物理數(shù)據(jù)庫(kù)名,與dataNode中dataHost對(duì)應(yīng)
balance 均衡負(fù)載的方式
writeType 寫入方式
dbType 數(shù)據(jù)庫(kù)類型
heartbeat 心跳檢測(cè)語(yǔ)句檬果,注意語(yǔ)句結(jié)尾的分號(hào)要加瓮孙。

應(yīng)用場(chǎng)景

數(shù)據(jù)庫(kù)分表分庫(kù)

配置如下:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="xml" contenteditable="false" cid="n174" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; page-break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: inherit inherit; background-repeat: inherit inherit;">Copy<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
?
<!-- 數(shù)據(jù)庫(kù)配置唐断,與server.xml中的數(shù)據(jù)庫(kù)對(duì)應(yīng) -->
 <schema name="lunch" checkSQLschema="false" sqlMaxLimit="100">
 <table name="lunchmenu" dataNode="dn1"  />
 <table name="restaurant" dataNode="dn1"  />
 <table name="userlunch" dataNode="dn1"  />
 <table name="users" dataNode="dn1"  />
 <table name="dictionary" primaryKey="id" autoIncrement="true" dataNode="dn1,dn2"  rule="mod-long" />
?

 </schema>
?
<!-- 分片配置 -->
 <dataNode name="dn1" dataHost="test1" database="lunch" />
 <dataNode name="dn2" dataHost="test2" database="lunch" />
?
<!-- 物理數(shù)據(jù)庫(kù)配置 -->
 <dataHost name="test1" maxCon="1000" minCon="10" balance="0"  writeType="0" dbType="mysql" dbDriver="native">
 <heartbeat>select user();</heartbeat>
 <writeHost host="hostM1" url="192.168.0.2:3306" user="root" password="123456">
 </writeHost>
 </dataHost>
?
 <dataHost name="test2" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native">
 <heartbeat>select user();</heartbeat>
 <writeHost host="hostS1" url="192.168.0.3:3306" user="root" password="123456">
 </writeHost>
 </dataHost>
?
</mycat:schema></pre>

我在192.168.0.2、192.168.0.3均有數(shù)據(jù)庫(kù)lunch杭抠。 lunchmenu脸甘、restaurant、userlunch偏灿、users這些表都只寫入節(jié)點(diǎn)dn1丹诀,也就是192.168.0.2這個(gè)服務(wù),而dictionary寫入了dn1翁垂、dn2兩個(gè)節(jié)點(diǎn)铆遭,也就是192.168.0.2、192.168.0.3這兩臺(tái)服務(wù)器沿猜。分片的規(guī)則為:mod-long枚荣。 主要關(guān)注rule屬性,rule屬性的內(nèi)容來(lái)源于rule.xml這個(gè)文件邢疙,Mycat支持10種分表分庫(kù)的規(guī)則棍弄,基本能滿足你所需要的要求,這個(gè)必須贊一個(gè)疟游,其他數(shù)據(jù)庫(kù)中間件好像都沒(méi)有這么多。 table中的rule屬性對(duì)應(yīng)的就是rule.xml文件中tableRule的name,具體有哪些分表和分庫(kù)的實(shí)現(xiàn)痕支,建議還是看下文檔颁虐。我這里選擇的mod-long就是將數(shù)據(jù)平均拆分。因?yàn)槲液蠖耸莾膳_(tái)物理庫(kù)卧须,所以rule.xml中mod-long對(duì)應(yīng)的function count為2另绩,見下面部分代碼:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="xml" contenteditable="false" cid="n176" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; page-break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: inherit inherit; background-repeat: inherit inherit;">Copy<tableRule name="mod-long">
 <rule>
 <columns>id</columns>
 <algorithm>mod-long</algorithm>
 </rule>
 </tableRule>

<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
 <!-- how many data nodes -->
 <property name="count">2</property>
 </function></pre>

數(shù)據(jù)庫(kù)讀寫分離

配置如下:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="xml" contenteditable="false" cid="n179" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; page-break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: inherit inherit; background-repeat: inherit inherit;">Copy<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
?
<!-- 數(shù)據(jù)庫(kù)配置,與server.xml中的數(shù)據(jù)庫(kù)對(duì)應(yīng) -->
 <schema name="lunch" checkSQLschema="false" sqlMaxLimit="100">
 <table name="lunchmenu" dataNode="dn1"  />
 <table name="restaurant" dataNode="dn1"  />
 <table name="userlunch" dataNode="dn1"  />
 <table name="users" dataNode="dn1"  />
 <table name="dictionary" primaryKey="id" autoIncrement="true" dataNode="dn1"  />
?

 </schema>
?
<!-- 分片配置 -->
 <dataNode name="dn1" dataHost="test1" database="lunch" />
?
?
<!-- 物理數(shù)據(jù)庫(kù)配置 -->
 <dataHost name="test1" maxCon="1000" minCon="10" balance="1"  writeType="0" dbType="mysql" dbDriver="native">
 <heartbeat>select user();</heartbeat>
 <writeHost host="hostM1" url="192.168.0.2:3306" user="root" password="123456">
 <readHost host="hostM1" url="192.168.0.3:3306" user="root" password="123456"> 
 </readHost>
 </writeHost>
 </dataHost>
?
?
</mycat:schema></pre>

這樣的配置與前一個(gè)示例配置改動(dòng)如下: 刪除了table分配的規(guī)則,以及datanode只有一個(gè) datahost也只有一臺(tái)花嘶,但是writehost總添加了readhost,balance改為1笋籽,表示讀寫分離。 以上配置達(dá)到的效果就是102.168.0.2為主庫(kù)椭员,192.168.0.3為從庫(kù)车海。

注意:Mycat主從分離只是在讀的時(shí)候做了處理,寫入數(shù)據(jù)的時(shí)候隘击,只會(huì)寫入到writehost侍芝,需要通過(guò)mycat的主從復(fù)制將數(shù)據(jù)復(fù)制到readhost,這個(gè)問(wèn)題當(dāng)時(shí)候我糾結(jié)了好久埋同,數(shù)據(jù)寫入writehost后州叠,readhost一直沒(méi)有數(shù)據(jù),以為是自己配置的問(wèn)題凶赁,后面才發(fā)現(xiàn)Mycat就沒(méi)有實(shí)現(xiàn)主從復(fù)制的功能咧栗,畢竟數(shù)據(jù)庫(kù)本身自帶的這個(gè)功能才是最高效穩(wěn)定的逆甜。

至于其他的場(chǎng)景,如同時(shí)主從和分表分庫(kù)也是支持的了致板,只要了解這個(gè)實(shí)現(xiàn)以后再去修改配置忆绰,都是可以實(shí)現(xiàn)的。而熱備及故障專業(yè)官方推薦使用haproxy配合一起使用可岂,大家可以試試错敢。

使用

Mycat的啟動(dòng)也很簡(jiǎn)單,啟動(dòng)命令在Bin目錄:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" contenteditable="false" cid="n186" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; page-break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: inherit inherit; background-repeat: inherit inherit;">Copy##啟動(dòng)
mycat start
?
##停止
mycat stop
?
##重啟
mycat restart</pre>

如果在啟動(dòng)時(shí)發(fā)現(xiàn)異常缕粹,在logs目錄中查看日志稚茅。

  • wrapper.log 為程序啟動(dòng)的日志,啟動(dòng)時(shí)的問(wèn)題看這個(gè)

  • mycat.log 為腳本執(zhí)行時(shí)的日志平斩,SQL腳本執(zhí)行報(bào)錯(cuò)后的具體錯(cuò)誤內(nèi)容,查看這個(gè)文件亚享。mycat.log是最新的錯(cuò)誤日志,歷史日志會(huì)根據(jù)時(shí)間生成目錄保存绘面。

mycat啟動(dòng)后欺税,執(zhí)行命令不成功,可能實(shí)際上配置有錯(cuò)誤揭璃,導(dǎo)致后面的命令沒(méi)有很好的執(zhí)行晚凿。

Mycat帶來(lái)的最大好處就是使用是完全不用修改原有代碼的,在mycat通過(guò)命令啟動(dòng)后瘦馍,你只需要將數(shù)據(jù)庫(kù)連接切換到Mycat的地址就可以了歼秽。如下面就可以進(jìn)行連接了:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" contenteditable="false" cid="n195" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; page-break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: inherit inherit; background-repeat: inherit inherit;">Copy mysql -h192.168.0.1 -P8806 -uroot -p123456</pre>

連接成功后可以執(zhí)行sql腳本了。 所以情组,可以直接通過(guò)sql管理工具(如:navicat燥筷、datagrip)連接,執(zhí)行腳本院崇。我一直用datagrip來(lái)進(jìn)行日常簡(jiǎn)單的管理肆氓,這個(gè)很方便。

Mycat還有一個(gè)管理的連接底瓣,端口號(hào)是9906.

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" contenteditable="false" cid="n198" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; page-break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: inherit inherit; background-repeat: inherit inherit;">Copy mysql -h192.168.0.1 -P9906 -uroot -p123456</pre>

連接后可以根據(jù)管理命令查看Mycat的運(yùn)行情況谢揪,當(dāng)然,喜歡UI管理方式的人濒持,可以安裝一個(gè)Mycat-Web來(lái)進(jìn)行管理键耕,有興趣自行搜索。

簡(jiǎn)而言之柑营,開發(fā)中使用Mycat和直接使用Mysql機(jī)會(huì)沒(méi)有差別屈雄。

常見問(wèn)題

使用Mycat后總會(huì)遇到一些坑,我將自己遇到的一些問(wèn)題在這里列一下官套,希望能與大家有共鳴:

  • Mycat是不是配置以后酒奶,就能完全解決分表分庫(kù)和讀寫分離問(wèn)題蚁孔? Mycat配合數(shù)據(jù)庫(kù)本身的復(fù)制功能,可以解決讀寫分離的問(wèn)題惋嚎,但是針對(duì)分表分庫(kù)的問(wèn)題杠氢,不是完美的解決×砦椋或者說(shuō)鼻百,至今為止,業(yè)界沒(méi)有完美的解決方案摆尝。 分表分庫(kù)寫入能完美解決温艇,但是,不能完美解決主要是聯(lián)表查詢的問(wèn)題堕汞,Mycat支持兩個(gè)表聯(lián)表的查詢勺爱,多余兩個(gè)表的查詢不支持。 其實(shí)讯检,很多數(shù)據(jù)庫(kù)中間件關(guān)于分表分庫(kù)后查詢的問(wèn)題琐鲁,都是需要自己實(shí)現(xiàn)的,而且節(jié)本都不支持聯(lián)表查詢人灼,Mycat已經(jīng)算做地非常先進(jìn)了围段。 分表分庫(kù)的后聯(lián)表查詢問(wèn)題,大家通過(guò)合理數(shù)據(jù)庫(kù)設(shè)計(jì)來(lái)避免挡毅。

  • Mycat支持哪些數(shù)據(jù)庫(kù)蒜撮,其他平臺(tái)如 .net、PHP能用嗎跪呈? 官方說(shuō)了,支持的數(shù)據(jù)庫(kù)包括MySQL取逾、SQL Server耗绿、Oracle、DB2砾隅、PostgreSQL 等主流數(shù)據(jù)庫(kù)误阻,很贊。 盡量用Mysql,我試過(guò)SQL Server晴埂,會(huì)有些小問(wèn)題究反,因?yàn)椴糠终Z(yǔ)法有點(diǎn)差異。

  • Mycat 非JAVA平臺(tái)如 .net儒洛、PHP能用嗎精耐? 可以用。這一點(diǎn)MyCat做的也很棒琅锻。

參考

《Mycat權(quán)威指南》: http://www.mycat.io/document/Mycat_V1.6.0.pdf 官網(wǎng) :http://www.mycat.io/

如果想熟練使用Mycat卦停,建議要仔細(xì)看看官方推薦的文檔向胡,可能需要花點(diǎn)時(shí)間。本文只是簡(jiǎn)單的介紹下Mycat的配置惊完,希望能快速讓大家對(duì)Mycat有個(gè)認(rèn)識(shí)僵芹,官方的文檔理解起來(lái)也很容易,只是需要的時(shí)間更多小槐,本文為說(shuō)明的參數(shù)拇派,請(qǐng)參考官方文檔。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末凿跳,一起剝皮案震驚了整個(gè)濱河市件豌,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌拄显,老刑警劉巖苟径,帶你破解...
    沈念sama閱讀 217,826評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異躬审,居然都是意外死亡棘街,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門承边,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)遭殉,“玉大人,你說(shuō)我怎么就攤上這事博助∠瘴郏” “怎么了?”我有些...
    開封第一講書人閱讀 164,234評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵富岳,是天一觀的道長(zhǎng)蛔糯。 經(jīng)常有香客問(wèn)我,道長(zhǎng)窖式,這世上最難降的妖魔是什么蚁飒? 我笑而不...
    開封第一講書人閱讀 58,562評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮萝喘,結(jié)果婚禮上淮逻,老公的妹妹穿的比我還像新娘。我一直安慰自己阁簸,他們只是感情好爬早,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,611評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著启妹,像睡著了一般筛严。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上翅溺,一...
    開封第一講書人閱讀 51,482評(píng)論 1 302
  • 那天脑漫,我揣著相機(jī)與錄音髓抑,去河邊找鬼。 笑死优幸,一個(gè)胖子當(dāng)著我的面吹牛吨拍,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播网杆,決...
    沈念sama閱讀 40,271評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼羹饰,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了碳却?” 一聲冷哼從身側(cè)響起队秩,我...
    開封第一講書人閱讀 39,166評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎昼浦,沒(méi)想到半個(gè)月后馍资,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,608評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡关噪,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,814評(píng)論 3 336
  • 正文 我和宋清朗相戀三年鸟蟹,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片使兔。...
    茶點(diǎn)故事閱讀 39,926評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡建钥,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出虐沥,到底是詐尸還是另有隱情熊经,我是刑警寧澤,帶...
    沈念sama閱讀 35,644評(píng)論 5 346
  • 正文 年R本政府宣布欲险,位于F島的核電站镐依,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏天试。R本人自食惡果不足惜馋吗,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,249評(píng)論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望秋秤。 院中可真熱鬧,春花似錦脚翘、人聲如沸灼卢。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)鞋真。三九已至,卻和暖如春沃于,著一層夾襖步出監(jiān)牢的瞬間涩咖,已是汗流浹背海诲。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留檩互,地道東北人特幔。 一個(gè)月前我還...
    沈念sama閱讀 48,063評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像闸昨,于是被迫代替她去往敵國(guó)和親蚯斯。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,871評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容