Mycat學習筆記 第二篇. MySql 讀寫分離與日誌分析——主從多結點

1    環境說明html

接上篇環境   《Mycat學習筆記》 第一篇. MySql 讀寫分離與日誌分析——主從單結點 http://www.cnblogs.com/kaye0110/p/5134588.htmljava

增長一套 mysql 實例,端口爲3308 ,經過Binlog方式同步主機狀況node

localhost : 3306 主機,    在mycat 中配置爲 writehost 1mysql

localhost : 3307 從機 a ,在mycat 中配置爲 readhostsql

localhost : 3308 從機 b ,在mycat 中配置爲 writehost 2數據庫

 

2    MyCat 配置學習

基本配置參考上篇,差別在於 switchType 和 balance  和 心跳監控語句url

根據參數說明,其實在此處配置readhost已無心義spa

switchType屬性.net

-  1 表示不自動切換-   1 默認值,自動切換

-  2 基於MySQL主從同步的狀態決定是否切換

      心跳語句爲 show slave status

-  3 基於MySQL galera cluster切換機制(適合集羣)(1.4.1

      心跳語句爲 show status like ‘wsrep%’

 

balance="0", 不開啓讀寫分離機制,全部讀操做都發送到當前可用的writeHost上。 

 

<dataHost name="localhost1" maxCon="1000" minCon="10" balance="0"
  writeType="0" dbType="mysql" dbDriver="native" switchType="2" slaveThreshold="100">
  <heartbeat>show slave status</heartbeat>


  <!-- can have multi write hosts -->
  <writeHost host="hostM1" url="localhost:3306" user="root" password="root123">
  <!-- can have multi read hosts -->
    <readHost host="hostS1" url="localhost:3307" user="root" password="root123" />
  </writeHost>

  <writeHost host="hostM2" url="localhost:3308" user="root" password="root123"></writeHost>

</dataHost>

 

3    基本SQL操做驗證

 直接看下insert 操做的日誌,目前數據是直接進入了3306端口的主機了。

01/17 10:53:58.596 DEBUG [$_NIOREACTOR-1-RW] (ServerQueryHandler.java:56) -ServerConnection [id=1, schema=TESTDB, host=0:0:0:0:0:0:0:1, user=test,txIsolation=3, autocommit=true, schema=TESTDB]insert into aaa (id,context) values (7,' insert by M1')


01/17 10:53:58.601 DEBUG [$_NIOREACTOR-1-RW] (NonBlockingSession.java:113) -ServerConnection [id=1, schema=TESTDB, host=0:0:0:0:0:0:0:1, user=test,txIsolation=3, autocommit=true, schema=TESTDB]insert into aaa (id,context) values (7,' insert by M1'), route={
1 -> dn1{insert into aaa (id,context) values (7,' insert by M1')}
} rrs


01/17 10:53:58.601 DEBUG [$_NIOREACTOR-1-RW] (MySQLConnection.java:445) -con need syn ,total syn cmd 1 commands SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;schema change:false con:MySQLConnection [id=1, lastTime=1452999238601, user=root, schema=mycat_sync_test, old shema=mycat_sync_test, borrowed=true, fromSlaveDB=false, threadId=43, charset=utf8, txIsolation=0, autocommit=true, attachment=dn1{insert into aaa (id,context) values (7,' insert by M1')}, respHandler=SingleNodeHandler [node=dn1{insert into aaa (id,context) values (7,' insert by M1')}, packetId=0], host=localhost, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=true]

 

 

再看下select 操做的日誌 ,數據從M1 結點獲取,即爲3306口的Mysql主機

01/17 10:54:04.466 DEBUG [$_NIOREACTOR-1-RW] (ServerQueryHandler.java:56) -ServerConnection [id=1, schema=TESTDB, host=0:0:0:0:0:0:0:1, user=test,txIsolation=3, autocommit=true, schema=TESTDB]select * from aaa


01/17 10:54:04.467 DEBUG [$_NIOREACTOR-1-RW] (EnchachePool.java:70) -SQLRouteCache hit cache ,key:TESTDBselect * from aaa


01/17 10:54:04.467 DEBUG [$_NIOREACTOR-1-RW] (NonBlockingSession.java:113) -ServerConnection [id=1, schema=TESTDB, host=0:0:0:0:0:0:0:1, user=test,txIsolation=3, autocommit=true, schema=TESTDB]select * from aaa, route={
1 -> dn1{SELECT *
FROM aaa
LIMIT 100}
} rrs
01/17 10:54:04.467 DEBUG [$_NIOREACTOR-1-RW] (PhysicalDBPool.java:452) -select read source hostM1 for dataHost:localhost1


01/17 10:54:04.468 DEBUG [$_NIOREACTOR-1-RW] (MySQLConnection.java:445) -con need syn ,total syn cmd 1 commands SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;schema change:false con:MySQLConnection [id=3, lastTime=1452999244468, user=root, schema=mycat_sync_test, old shema=mycat_sync_test, borrowed=true, fromSlaveDB=false, threadId=38, charset=utf8, txIsolation=0, autocommit=true, attachment=dn1{SELECT *
FROM aaa
LIMIT 100}, respHandler=SingleNodeHandler [node=dn1{SELECT *
FROM aaa
LIMIT 100}, packetId=0], host=localhost, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]

 

4    主機下線,讀寫驗證

經過Windows 服務中止Mysql 主機 3306 的服務,開始驗證Mycat 主從自動切換效果。

 

數據庫SQL驗證,前兩次查詢時直接返回異常,第三次已能正常返回數據

mysql> select * from aaa;
ERROR 1184 (HY000): Connection refused: no further information
mysql> select * from aaa;
ERROR 1184 (HY000): Connection refused: no further information
mysql> select * from aaa;
+----+----------------+
| id | context |
+----+----------------+
| 1 | hello 1 |
| 2 | hello 2 |
| 3 | hello3 |
| 4 | hello4 |
| 5 | hell world5 |
| 6 | new mysql 3308 |
| 7 | insert by M1 |
+----+----------------+
7 rows in set (0.00 sec)

 

再來看下logs/mycat.log 日誌文檔

仍是從dn1獲取數據,不過已經改從hostM2取數了。

01/17 11:09:04.975 DEBUG [$_NIOREACTOR-1-RW] (ServerQueryHandler.java:56) -ServerConnection [id=1, schema=TESTDB, host=0:0:0:0:0:0:0:1, user=test,txIsolation=3, autocommit=true, schema=TESTDB]select * from aaa
01/17 11:09:04.980 DEBUG [$_NIOREACTOR-1-RW] (EnchachePool.java:76) -SQLRouteCache miss cache ,key:TESTDBselect * from aaa
01/17 11:09:05.110 DEBUG [$_NIOREACTOR-1-RW] (EnchachePool.java:59) -SQLRouteCache add cache ,key:TESTDBselect * from aaa value:select * from aaa, route={
1 -> dn1{SELECT *
FROM aaa
LIMIT 100}
}
01/17 11:09:05.111 DEBUG [$_NIOREACTOR-1-RW] (NonBlockingSession.java:113) -ServerConnection [id=1, schema=TESTDB, host=0:0:0:0:0:0:0:1, user=test,txIsolation=3, autocommit=true, schema=TESTDB]select * from aaa, route={
1 -> dn1{SELECT *
FROM aaa
LIMIT 100}
} rrs
01/17 11:09:05.113 DEBUG [$_NIOREACTOR-1-RW] (PhysicalDBPool.java:452) -select read source hostM2 for dataHost:localhost1
01/17 11:09:05.114 DEBUG [$_NIOREACTOR-1-RW] (MySQLConnection.java:445) -con need syn ,total syn cmd 1 commands SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;schema change:false con:MySQLConnection [id=14, lastTime=1453000145114, user=root, schema=mycat_sync_test, old shema=mycat_sync_test, borrowed=true, fromSlaveDB=false, threadId=10, charset=utf8, txIsolation=0, autocommit=true, attachment=dn1{SELECT *
FROM aaa
LIMIT 100}, respHandler=SingleNodeHandler [node=dn1{SELECT *
FROM aaa
LIMIT 100}, packetId=0], host=localhost, port=3308, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]

 

同時mycat 出現警告,通知hostM2切爲主機,hostS1失效。

01/17 11:09:17.412 WARN [$_NIOREACTOR-0-RW] (MySQLDetector.java:139) -found MySQL master/slave Replication err !!! DBHostConfig [hostName=hostM2, url=localhost:3308]error reconnecting to master 'mycat_sync@192.168.1.247:3306' - retry-time: 60 retries: 86400
01/17 11:09:17.413 DEBUG [$_NIOREACTOR-1-RW] (PhysicalDatasource.java:403) -release channel MySQLConnection [id=11, lastTime=1453000157394, user=root, schema=mycat_sync_test, old shema=mycat_sync_test, borrowed=true, fromSlaveDB=true, threadId=53, charset=utf8, txIsolation=0, autocommit=true, attachment=null, respHandler=null, host=localhost, port=3307, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]
01/17 11:09:17.413 WARN [$_NIOREACTOR-1-RW] (MySQLDetector.java:139) -found MySQL master/slave Replication err !!! DBHostConfig [hostName=hostS1, url=localhost:3307]error reconnecting to master 'mycat_sync@192.168.1.247:3306' - retry-time: 60 retries: 86400
01/17 11:09:18.418 INFO [$_NIOConnector] (AbstractConnection.java:458) -close connection,reason:java.net.ConnectException: Connection refused: no further information ,MySQLConnection [id=0, lastTime=1453000157394, user=root, schema=mycat_sync_test, old shema=mycat_sync_test, borrowed=false, fromSlaveDB=false, threadId=0, charset=utf8, txIsolation=0, autocommit=true, attachment=null, respHandler=null, host=localhost, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]
01/17 11:09:18.418 INFO [$_NIOConnector] (SQLJob.java:111) -can't get connection for sql :show slave status
01/17 11:09:18.418 INFO [$_NIOConnector] (PhysicalDatasource.java:373) -not ilde connection in pool,create new connection for hostM1 of schema mycat_sync_test

 

經過Mycat數據庫insert 操做驗證

 

mysql> insert into aaa (id,context) values (8,'insert by M2');
Query OK, 1 row affected (0.01 sec)

 

mysql> select * from aaa;
+----+----------------+
| id | context |
+----+----------------+
| 1 | hello 1 |
| 2 | hello 2 |
| 3 | hello3 |
| 4 | hello4 |
| 5 | hell world5 |
| 6 | new mysql 3308 |
| 7 | insert by M1 |
| 8 | insert by M2 |
+----+----------------+
8 rows in set (0.00 sec)

 

再來看下物理機 3307 的狀況,查詢無最新數據

mysql> use mycat_sync_test
Database changed
mysql> select * from aaa;
+----+----------------+
| id | context |
+----+----------------+
| 1 | hello 1 |
| 2 | hello 2 |
| 3 | hello3 |
| 4 | hello4 |
| 5 | hell world5 |
| 6 | new mysql 3308 |
| 7 | insert by M1 |
+----+----------------+
7 rows in set (0.00 sec)

 

再來看下物理機 3308 M2 的狀況,查詢有最新數據,說明mycat 自動切換正常。

mysql> use mycat_sync_test
Database changed
mysql> select * from aaa;
+----+----------------+
| id | context |
+----+----------------+
| 1 | hello 1 |
| 2 | hello 2 |
| 3 | hello3 |
| 4 | hello4 |
| 5 | hell world5 |
| 6 | new mysql 3308 |
| 7 | insert by M1 |
| 8 | insert by M2 |
+----+----------------+
8 rows in set (0.00 sec)

 

5    MyCat 監控管理功能

經過9066端口登錄MyCat管理功能 

mysql -u test -ptest -P 9066

 

mysql> show @@datasource;
+----------+--------+-------+-----------+------+------+--------+------+------+--
-------+
| DATANODE | NAME | TYPE | HOST | PORT | W/R | ACTIVE | IDLE | SIZE | EXECUTE |
+----------+--------+-------+-----------+------+------+--------+------+------+---------+
| dn2 | hostM1 | mysql | localhost | 3306 | W | 0 | 0 | 1000 |0 |
| dn2 | hostM2 | mysql | localhost | 3308 | W | 0 | 8 | 1000 |161 |
| dn2 | hostS1 | mysql | localhost | 3307 | R | 0 | 8 | 1000 |137 |
| dn1 | hostM1 | mysql | localhost | 3306 | W | 0 | 0 | 1000 |0 |
| dn1 | hostM2 | mysql | localhost | 3308 | W | 0 | 8 | 1000 |161 |
| dn1 | hostS1 | mysql | localhost | 3307 | R | 0 | 8 | 1000 |137 |
+----------+--------+-------+-----------+------+------+--------+------+------+---------+
6 rows in set (0.02 sec)

 

6    補充說明

回頭還要驗證下主從恢復功能

另有一篇文章也有提到能夠參考:http://www.2cto.com/database/201511/448344.html

 

本篇完。

相關文章
相關標籤/搜索