下載解壓及創建用戶組和目錄屬性
下載地址:1、https://github.com/MyCATApache/Mycat-download。2、http://dl.mycat.io/ wget http://dl.mycat.io/1.6.6.1/Mycat-server-1.6.6.1-release-20180908155252-linux.tar.gz tar -zxf Mycat-server-1.6.6.1-release-20180908155252-linux.tar.gz -C /usr/local/ groupadd mycat useradd mycat -g mycat chown -R mycat:mycat mycat
mycat主要配置3個參數文件需要配置
--server.xml:是Mycat服務器參數調整和用戶授權的配置文件。 --schema.xml:是邏輯庫定義和表以及分片定義的配置文件。 --rule.xml: 是分片規則的配置文件,分片規則的具體一些參數信息單獨存放為文件,也在這個目錄下,配置文件修改需要重啟MyCAT。
配置server.xml
server.xml,用戶權限及表的精細權限
<user name="ht">
<property name="password">ocm123</property>
<property name="schemas">sakila</property>
</user>
配置schema.xml
[mycat@redis04 conf]$ cat schema.xml <?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/"> <schema name="sakila" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1"> </schema> <dataNode name="dn1" dataHost="localhost1" database="sakila" /> <dataHost name="localhost1" 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.20.201:3306" user="ht" password="ocm123"> <!-- can have multi read hosts --> <readHost host="hostS2" url="192.168.20.203:3306" user="ht" password="ocm123" /> </writeHost> <!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> --> </dataHost> </mycat:schema> schema.xml schema的值與server.xml中schema的值是多對1的關系,server.xml中schema的值可以是多個、schema.xml schema的值是唯一,可以有多個schema。schema 相對於mysql中的database dataNode 數據分片。一個dataNode標簽就是一個獨立的數據分片。 dataHost 物理主機 balance屬性 負載均衡類型,目前的取值有3種: 1. balance="0", 不開啟讀寫分離機制,所有讀操作都發送到當前可用的writeHost上。 2. balance="1",全部的readHost與stand by writeHost參與select語句的負載均衡,簡單的說,當雙主雙從模式(M1->S1,M2->S2,並且M1與 M2互為主備),正常情況下,M2,S1,S2都參與select語句的負載均衡。 3. balance="2",所有讀操作都隨機的在writeHost、readhost上分發。 4. balance="3",所有讀請求隨機的分發到wiriterHost對應的readhost執行,writerHost不負擔讀壓力,注意balance=3只在1.4及其以后版本有,1.3沒有。 writeType屬性 負載均衡類型,目前的取值有3種: 1. writeType="0", 所有寫操作發送到配置的第一個writeHost,第一個掛了切到還生存的第二個writeHost,重新啟動后已切換后的為准,切換記錄在配置文件中:dnindex.properties . 2. writeType="1",所有寫操作都隨機的發送到配置的writeHost,1.5以后廢棄不推薦。 switchType屬性 - -1 表示不自動切換 - 1 默認值,自動切換 - 2 基於MySQL主從同步的狀態決定是否切換 - 3 基於MySQL galary cluster的切換機制(適合集群)(1.4.1)心跳語句為 show status like ‘wsrep%’.
啟動mycat
mycat start 啟動 mycat stop 停止 mycat console 前台運行 mycat restart 重啟服務 mycat pause 暫停 mycat status 查看啟動狀態
連接和管理mycat
mysql -h 192.168.20.204 -P 8066 -u ht -p --應該用連接 mysql -h 192.168.20.204 -P 9066 -u ht -p --管理 ,查看mycat的詳細信息
打開mycat日志debug,為下面查看具體路由和調試做准備
vi log4j2.xml
<!--<AsyncLogger name="io.mycat" level="debug" includeLocation="true" additivity="false">-->
<!--<AppenderRef ref="Console"/>-->
<!--<AppenderRef ref="RollingFile"/>-->
<!--</AsyncLogger>-->
執行select語句看后台日志
SELECT * from sakila.actor a where a.actor_id=1;
2018-09-12 14:13:42.433 DEBUG [$_NIOREACTOR-2-RW] (io.mycat.server.ServerQueryHandler.query(ServerQueryHandler.java:57)) - ServerConnection [id=1, schema=null, host=192.168.20.204, user=ht,txIsolation=3, autocommit=true, schema=null]SELECT * from sakila.actor a where a.actor_id=1
2018-09-12 14:13:42.434 DEBUG [$_NIOREACTOR-2-RW] (io.mycat.cache.impl.EnchachePool.get(EnchachePool.java:77)) - SQLRouteCache miss cache ,key:sakilaSELECT * from sakila.actor a where a.actor_id=1
2018-09-12 14:13:42.434 DEBUG [$_NIOREACTOR-2-RW] (io.mycat.server.NonBlockingSession.execute(NonBlockingSession.java:126)) - ServerConnection [id=1, schema=null, host=192.168.20.204, user=ht,txIsolation=3, autocommit=true, schema=null]SELECT * from sakila.actor a where a.actor_id=1, route={
1 -> dn1{SELECT * from sakila.actor a where a.actor_id=1}
} rrs
2018-09-12 14:13:42.434 DEBUG [$_NIOREACTOR-2-RW] (io.mycat.backend.mysql.nio.handler.SingleNodeHandler.execute(SingleNodeHandler.java:167)) - rrs.getRunOnSlave() default
2018-09-12 14:13:42.434 DEBUG [$_NIOREACTOR-2-RW] (io.mycat.backend.mysql.nio.handler.SingleNodeHandler.execute(SingleNodeHandler.java:169)) - node.getRunOnSlave() default
2018-09-12 14:13:42.434 DEBUG [$_NIOREACTOR-2-RW] (io.mycat.backend.mysql.nio.handler.SingleNodeHandler.execute(SingleNodeHandler.java:178)) - node.getRunOnSlave() default
2018-09-12 14:13:42.434 DEBUG [$_NIOREACTOR-2-RW] (io.mycat.backend.mysql.nio.handler.SingleNodeHandler.execute(SingleNodeHandler.java:180)) - node.getRunOnSlave() default
2018-09-12 14:13:42.434 DEBUG [$_NIOREACTOR-2-RW] (io.mycat.backend.datasource.PhysicalDBNode.getConnection(PhysicalDBNode.java:96)) - rrs.getRunOnSlave() default
2018-09-12 14:13:42.434 DEBUG [$_NIOREACTOR-2-RW] (io.mycat.backend.datasource.PhysicalDBNode.getConnection(PhysicalDBNode.java:127)) - rrs.getRunOnSlave() default
2018-09-12 14:13:42.434 DEBUG [$_NIOREACTOR-2-RW] (io.mycat.backend.datasource.PhysicalDBPool.getRWBanlanceCon(PhysicalDBPool.java:530)) - select read source hostS2 for dataHost:localhost1
2018-09-12 14:13:42.434 DEBUG [$_NIOREACTOR-2-RW] (io.mycat.backend.mysql.nio.MySQLConnection.synAndDoExecute(MySQLConnection.java:462)) - con need syn ,total syn cmd 1 commands SET names utf8;schema change:false con:MySQLConnection [id=13, lastTime=1536732822434, user=ht, schema=sakila, old shema=sakila, borrowed=true, fromSlaveDB=true, threadId=357, charset=utf8, txIsolation=3, autocommit=true, attachment=dn1{SELECT * from sakila.actor a where a.actor_id=1}, respHandler=SingleNodeHandler [node=dn1{SELECT * from sakila.actor a where a.actor_id=1}, packetId=0], host=192.168.20.203, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]
執行update語句查看后台日志
update sakila.actor a set a.last_name='sahnghai' where a.actor_id=1;
2018-09-12 14:13:41.830 DEBUG [$_NIOREACTOR-2-RW] (io.mycat.server.NonBlockingSession.execute(NonBlockingSession.java:126)) - ServerConnection [id=1, schema=null, host=192.168.20.204, user=ht,txIsolation=3, autocommit=true, schema=null]update sakila.actor a set a.last_name='sahnghai' where a.actor_id=1, route={
1 -> dn1{update sakila.actor a set a.last_name='sahnghai' where a.actor_id=1}
} rrs
2018-09-12 14:13:41.830 DEBUG [$_NIOREACTOR-2-RW] (io.mycat.backend.mysql.nio.handler.SingleNodeHandler.execute(SingleNodeHandler.java:167)) - rrs.getRunOnSlave() default
2018-09-12 14:13:41.830 DEBUG [$_NIOREACTOR-2-RW] (io.mycat.backend.mysql.nio.handler.SingleNodeHandler.execute(SingleNodeHandler.java:169)) - node.getRunOnSlave() default
2018-09-12 14:13:41.830 DEBUG [$_NIOREACTOR-2-RW] (io.mycat.backend.mysql.nio.handler.SingleNodeHandler.execute(SingleNodeHandler.java:178)) - node.getRunOnSlave() default
2018-09-12 14:13:41.830 DEBUG [$_NIOREACTOR-2-RW] (io.mycat.backend.mysql.nio.handler.SingleNodeHandler.execute(SingleNodeHandler.java:180)) - node.getRunOnSlave() default
2018-09-12 14:13:41.830 DEBUG [$_NIOREACTOR-2-RW] (io.mycat.backend.datasource.PhysicalDBNode.getConnection(PhysicalDBNode.java:96)) - rrs.getRunOnSlave() default
2018-09-12 14:13:41.830 DEBUG [$_NIOREACTOR-2-RW] (io.mycat.backend.datasource.PhysicalDBNode.getConnection(PhysicalDBNode.java:127)) - rrs.getRunOnSlave() default
2018-09-12 14:13:41.830 DEBUG [$_NIOREACTOR-2-RW] (io.mycat.backend.mysql.nio.MySQLConnection.synAndDoExecute(MySQLConnection.java:462)) - con need syn ,total syn cmd 1 commands SET names utf8;schema change:false con:MySQLConnection [id=10, lastTime=1536732821830, user=ht, schema=sakila, old shema=sakila, borrowed=true, fromSlaveDB=false, threadId=410, charset=utf8, txIsolation=3, autocommit=true, attachment=dn1{update sakila.actor a set a.last_name='sahnghai' where a.actor_id=1}, respHandler=SingleNodeHandler [node=dn1{update sakila.actor a set a.last_name='sahnghai' where a.actor_id=1}, packetId=0], host=192.168.20.201, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=true]
2018-09-12 14:13:41.940 DEBUG [$_NIOREACTOR-1-RW] (io.mycat.server.NonBlockingSession.releaseConnection(NonBlockingSession.java:386)) - release connection MySQLConnection [id=10, lastTime=1536732821828, user=ht, schema=sakila, old shema=sakila, borrowed=true, fromSlaveDB=false, threadId=410, charset=utf8, txIsolation=3, autocommit=true, attachment=dn1{update sakila.actor a set a.last_name='sahnghai' where a.actor_id=1}, respHandler=SingleNodeHandler [node=dn1{update sakila.actor a set a.last_name='sahnghai' where a.actor_id=1}, packetId=1], host=192.168.20.201, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=true]
開啟mycat日志看到讀寫分離測試成功
