系統(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è)贊一下吃靠。)
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ī)則 |
服務(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)參考官方文檔。