mycat 主從讀寫分離-主存切換(驗證)

轉自:http://blog.csdn.net/mchdba/article/details/50616534

前面主從實現文章基礎上實現mycat讀寫分離

6,讀寫分離模式

6.1然後修改mycat的schema.xml:

balance爲1:讓全部的readHost及備用的writeHost參與select的負載均衡。 
switchType爲2:基於MySQL主從同步的狀態決定是否切換。 
heartbeat:主從切換的心跳語句必須爲show slave status。 
這裏寫圖片描述 
有配置讀節點: 
這裏寫圖片描述 

6.2 打開debug模式

[root@wgq_idc_squid_1_11 conf]# vim log4j.xml
<level value="info" /> 變成  <level value="debug" />
  • 1
  • 2

6.3 執行查詢讀操作

在mycat窗口上執行select操作:

mysql> explain select * from company where id=1;
+-----------+----------------------------------------------+
| DATA_NODE | SQL                                          |
+-----------+----------------------------------------------+
| dn3       | SELECT * FROM company WHERE id = 1 LIMIT 100 |
+-----------+----------------------------------------------+
1 row in set (0.14 sec)

mysql> select * from company where id=1;
+----+---------+
| id | name    |
+----+---------+
|  1 | alibaba |
+----+---------+
1 row in set (0.01 sec)

mysql>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

後臺日誌顯示出只在3327上執行:

01/18 01:35:01.536  DEBUG [$_NIOREACTOR-3-RW] (PhysicalDBPool.java:452) -select read source hostS2 for dataHost:wgq_idc_mon_1_11
01/18 01:35:01.537  DEBUG [$_NIOREACTOR-3-RW] (MySQLConnection.java:445) -con need syn ,total syn cmd 2 commands SET names latin1;SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;schema change:false con:MySQLConnection [id=14, lastTime=1453052101537, user=root, schema=db1, old shema=db1, borrowed=true, fromSlaveDB=false, threadId=3326, charset=latin1, txIsolation=0, autocommit=true, attachment=dn1{SELECT *
FROM company
WHERE id = 1
LIMIT 100}, respHandler=SingleNodeHandler [node=dn1{SELECT *
FROM company
WHERE id = 1
LIMIT 100}, packetId=0], host=127.0.0.1, port=3327, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]
01/18 01:35:01.546  DEBUG [$_NIOREACTOR-2-RW] (NonBlockingSession.java:229) -release connection MySQLConnection [id=14, lastTime=1453052101529, user=root, schema=db1, old shema=db1, borrowed=true, fromSlaveDB=false, threadId=3326, charset=latin1, txIsolation=3, autocommit=true, attachment=dn1{SELECT *
FROM company
WHERE id = 1
LIMIT 100}, respHandler=SingleNodeHandler [node=dn1{SELECT *
FROM company
WHERE id = 1
LIMIT 100}, packetId=5], host=127.0.0.1, port=3327, statusSync=org.opencloudb.mysql.nio.MySQLConnection$StatusSync@7fb43f0f, writeQueue=0, modifiedSQLExecuted=false]
01/18 01:35:01.547  DEBUG [$_NIOREACTOR-2-RW] (PhysicalDatasource.java:403) -release channel MySQLConnection [id=14, lastTime=1453052101529, user=root, schema=db1, old shema=db1, borrowed=true, fromSlaveDB=false, threadId=3326, charset=latin1, txIsolation=3, autocommit=true, attachment=null, respHandler=null, host=127.0.0.1, port=3327, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]
^C
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

這裏寫圖片描述

6.4 執行寫操作

Mycat窗口寫操作: 
這裏寫圖片描述

後臺log顯示寫操作在3317上: 
這裏寫圖片描述

01/18 01:39:54.550  DEBUG [$_NIOREACTOR-3-RW] (NonBlockingSession.java:229) -release connection MySQLConnection [id=7, lastTime=1453052394535, user=root, schema=db3, old shema=db3, borrowed=true, fromSlaveDB=false, threadId=163, charset=latin1, txIsolation=3, autocommit=true, attachment=dn3{insert into company values(3,'baidu')}, respHandler=org.opencloudb.mysql.nio.handler.MultiNodeQueryHandler@42bed1e7, host=127.0.0.1, port=3317, statusSync=null, writeQueue=0, modifiedSQLExecuted=true]
01/18 01:39:54.550  DEBUG [$_NIOREACTOR-3-RW] (PhysicalDatasource.java:403) -release channel MySQLConnection [id=7, lastTime=1453052394535, user=root, schema=db3, old shema=db3, borrowed=true, fromSlaveDB=false, threadId=163, charset=latin1, txIsolation=3, autocommit=true, attachment=null, respHandler=null, host=127.0.0.1, port=3317, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]
01/18 01:39:54.550  DEBUG [$_NIOREACTOR-3-RW] (MultiNodeQueryHandler.java:171) -received ok response ,executeResponse:true from MySQLConnection [id=3, lastTime=1453052394535, user=root, schema=db2, old shema=db2, borrowed=true, fromSlaveDB=false, threadId=162, charset=latin1, txIsolation=3, autocommit=true, attachment=dn2{insert into company values(3,'baidu')}, respHandler=org.opencloudb.mysql.nio.handler.MultiNodeQueryHandler@42bed1e7, host=127.0.0.1, port=3317, statusSync=null, writeQueue=0, modifiedSQLExecuted=true]
01/18 01:39:54.551  DEBUG [$_NIOREACTOR-3-RW] (NonBlockingSession.java:229) -release connection MySQLConnection [id=3, lastTime=1453052394535, user=root, schema=db2, old shema=db2, borrowed=true, fromSlaveDB=false, threadId=162, charset=latin1, txIsolation=3, autocommit=true, attachment=dn2{insert into company values(3,'baidu')}, respHandler=org.opencloudb.mysql.nio.handler.MultiNodeQueryHandler@42bed1e7, host=127.0.0.1, port=3317, statusSync=null, writeQueue=0, modifiedSQLExecuted=true]
01/18 01:39:54.551  DEBUG [$_NIOREACTOR-3-RW] (PhysicalDatasource.java:403) -release channel MySQLConnection [id=3, lastTime=1453052394535, user=root, schema=db2, old shema=db2, borrowed=true, fromSlaveDB=false, threadId=162, charset=latin1, txIsolation=3, autocommit=true, attachment=null, respHandler=null, host=127.0.0.1, port=3317, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]
  • 1
  • 2
  • 3
  • 4
  • 5

Check下主從數據是否都已經寫入了: 
主庫3317端口: 
這裏寫圖片描述
從褲3327端口: 
這裏寫圖片描述


7,主從切換測試

7.1 mycat託管配置如下

     <dataHost name="wgq_idc_mon_1_11" maxCon="1000" minCon="10" balance="1"
                writeType="0" dbType="mysql" dbDriver="native" switchType="2"  slaveThreshold="100">
                <heartbeat>show slave status</heartbeat>
                <!-- can have multi write hosts -->
                <writeHost host="hostM1" url="127.0.0.1:3317" user="root" password="">
                        <readHost host="hostR1" url="127.0.0.1:3327" user="root" password=""/>
                </writeHost>
                <writeHost host="hostS2" url="127.0.0.1:3327" user="root" password="" />
        </dataHost>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

7.2 關閉主庫,寫操作切換到從褲3327端口

關閉主庫 
這裏寫圖片描述

[root@wgq_idc_squid_1_11 conf]# service mysql56m1 stop
Shutting down MySQL....                                    [確定]
[root@wgq_idc_squid_1_11 conf]#
  • 1
  • 2
  • 3

Mycat後臺報錯:

01/18 01:50:01.037   INFO [Timer0] (PhysicalDatasource.java:373) -not ilde connection in pool,create new connection for hostM1 of schema db1
01/18 01:50:01.038   INFO [$_NIOConnector] (AbstractConnection.java:458) -close connection,reason:java.net.ConnectException: 拒絕連接 ,MySQLConnection [id=0, lastTime=1453053001035, user=root, schema=db1, old shema=db1, borrowed=false, fromSlaveDB=false, threadId=0, charset=utf8, txIsolation=0, autocommit=true, attachment=null, respHandler=null, host=127.0.0.1, port=3317, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]
01/18 01:50:01.038   INFO [$_NIOConnector] (SQLJob.java:111) -can't get connection for sql :show slave status
01/18 01:50:01.038   WARN [$_NIOREACTOR-1-RW] (MySQLDetector.java:139) -found MySQL master/slave Replication err !!! DBHostConfig [hostName=hostR1, url=127.0.0.1:3327]error reconnecting to master 'repl@127.0.0.1:3317' - retry-time: 60  retries: 1
0
  • 1
  • 2
  • 3
  • 4
  • 5

看到主從失效,因爲主down了。

因爲我們通過mycat配置了主從切換模式,現在3317端口主庫down了,那麼寫庫應該自動切換到從褲3327上面,如果在mycat上面寫入,就應該寫到3327端口的從庫了,驗證如下: 
(1)在mycat窗口錄入數據: 
這裏寫圖片描述 
(2)在3327端口從褲驗證數據: 
這裏寫圖片描述

看到3327端口的數據有新錄入的4,meituan的數據。表明驗證成功。 
(3) dnindex屬性文件中writeHost已經變成了第二個了,如下所示: 
這裏寫圖片描述

7.3 再啓動原來的主庫3317端口,在mycat上做寫操作,後臺mysql寫庫還是原來的從庫3327端口:

這個時候就會報錯,主從連接失敗,原因是從庫3327會再重新從主庫3317同步所有的數據,但是從庫已經有了,所以就會報錯,如下所示: 
這裏寫圖片描述

我採用

stop slave;
 set global sql_slave_skip_counter=1;
 start slave;
 show slave status\G;
  • 1
  • 2
  • 3
  • 4

一個個忽略過後,就正常了,沒有報錯: 
這裏寫圖片描述

但是這個時候如果再在mycat窗口上錄入數據,debug分析面,還是會寫入到3327裏面去: 
這裏寫圖片描述

後臺debug日誌: 
這裏寫圖片描述

OK,至此,mycat主從切換成功。

補充點:實際生產環境需要配置雙主,不會切到讀庫


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章