1)系統環境說明
MySql 5.5 主從節點
127.0.0.1:3306 主結點,為驗證主從切換效果,手動停止服務
127.0.0.1: 3307 從結點 1
127.0.0.1:338 從結點 2 ,為驗證主從切換效果,在主結點停止后,新增兩個記錄。
MyCat 1.5 schema.xml 配置
具體配置說明,參考上篇: 《Mycat學習筆記》 第二篇. MySql 讀寫分離與日志分析——主從多結點
<dataHost name="localhost1" 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="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>
2)整體切換流程
- 原主機(3306)停止服務
- MyCat 自動檢測進行主從切換(hostM1轉為hostM2)
- 通過mycat insert data,數據只在 3308 的數據庫中體現
- 啟動原主機mysql 3306 服務
- 手動同步數據 mysql workbench (暫時不知道別的方法,)
- 調整mysql master 和 slave的關系 ,將3308設為主機,3306設為從機,3307配置不動
- 通過mycat 驗證新的主從配置
- mycat 不用重啟,服務會自動切換
3)具體操作步驟
3.1)停止3308 環境 slave模式 並 開啟 master
停止slave
mysql> stop slave;
配置3308為主機模式
mysql> GRANT REPLICATION SLAVE ON *.* to 'mycat_sync_3308'@'%' identified by 'mycat_sync_3308';
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000005 | 446 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
3.2)3306 環境開啟slave ,數據庫其他備份方式參考第4點
從3308 服務中導出新增的記錄
mysql> select * from aaa where id > 7 into outfile 'D:/bin/mysql/MySQL_3308/dump/aaa_20160118.data'
3306 服務中導入數據
mysql> load data infile 'D:/bin/mysql/MySQL_3308/dump/aaa_20160118.data' into t
able aaa;
Query OK, 2 rows affected (0.04 sec)
Records: 2 Deleted: 0 Skipped: 0 Warnings: 0
設置3306 為 3308 的 從機
mysql> change master to master_host='192.168.1.247'
-> ,master_port=3308
-> ,master_user='mycat_sync_3308'
-> ,master_password='mycat_sync_3308'
-> ,master_log_file='mysql-bin.000005'
-> ,master_log_pos=446;
Query OK, 0 rows affected (0.03 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
3.3)mycat 驗證
登陸mycat 8066口,新增數據
mysql> use TESTDB
Database changed
mysql> insert into aaa (id,context) values (10,'master3308 slave3306 slave3307')
;
Query OK, 1 row affected (0.02 sec)
通過mysql 主機 (3308) 確認
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 |
| 9 | need to sync |
| 10 | master3308 slave3306 slave3307 |
+----+--------------------------------+
10 rows in set (0.00 sec)
通過mysql 從機 (3306) 確認
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 |
| 9 | need to sync |
| 10 | master3308 slave3306 slave3307 |
+----+--------------------------------+
10 rows in set (0.00 sec)
通過 mysql 從機 3307 確認 (原3306的從機)。發現數據沒有同步更新,那就還需要再把3307的數據同步服務指向3308,具體操作如上。
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 |
| 9 | need to sync |
+----+----------------+
9 rows in set (0.00 sec)
3.4)再來看下mycat 日志文件 logs/mycat.log
insert 操作使用 3308 服務進行處理
01/18 14:37:16.417 DEBUG [$_NIOREACTOR-1-RW] (ServerQueryHandler.java:56) -ServerConnection [id=4, 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 (10,'master3308 slave3306 slave3307')
01/18 14:37:16.429 DEBUG [$_NIOREACTOR-1-RW] (NonBlockingSession.java:113) -ServerConnection [id=4, 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 (10,'master3308 slave3306 slave3307'), route={
1 -> dn1{insert into aaa (id,context) values (10,'master3308 slave3306 slave3307')}
} rrs
01/18 14:37:16.432 DEBUG [$_NIOREACTOR-0-RW] (NonBlockingSession.java:229) -release connection MySQLConnection [id=2, lastTime=1453099036418, user=root, schema=mycat_sync_test, old shema=mycat_sync_test, borrowed=true, fromSlaveDB=false, threadId=8, charset=utf8, txIsolation=3, autocommit=true, attachment=dn1{insert into aaa (id,context) values (10,'master3308 slave3306 slave3307')}, respHandler=SingleNodeHandler [node=dn1{insert into aaa (id,context) values (10,'master3308 slave3306 slave3307')}, packetId=0], host=localhost, port=3308, statusSync=null, writeQueue=0, modifiedSQLExecuted=true]
數據庫查詢操作已切換至3306服務中,確認mycat 已自動檢測到3306恢復正常
01/18 14:44:52.547 DEBUG [$_NIOREACTOR-1-RW] (EnchachePool.java:76) -SQLRouteCache miss cache ,key:TESTDBselect * from aaa
01/18 14:44:52.564 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/18 14:44:52.564 DEBUG [$_NIOREACTOR-1-RW] (NonBlockingSession.java:113) -ServerConnection [id=4, 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/18 14:44:52.564 DEBUG [$_NIOREACTOR-1-RW] (PhysicalDBPool.java:452) -select read source hostM1 for dataHost:localhost1
01/18 14:44:52.564 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=15, lastTime=1453099492564, user=root, schema=mycat_sync_test, old shema=mycat_sync_test, borrowed=true, fromSlaveDB=false, threadId=1, 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]
01/18 14:44:52.565 DEBUG [$_NIOREACTOR-1-RW] (NonBlockingSession.java:229) -release connection MySQLConnection [id=15, lastTime=1453099492560, user=root, schema=mycat_sync_test, old shema=mycat_sync_test, borrowed=true, fromSlaveDB=false, threadId=1, charset=utf8, txIsolation=3, autocommit=true, attachment=dn1{SELECT *
FROM aaa
LIMIT 100}, respHandler=SingleNodeHandler [node=dn1{SELECT *
FROM aaa
LIMIT 100}, packetId=14], host=localhost, port=3306, statusSync=org.opencloudb.mysql.nio.MySQLConnection$StatusSync@1d516b8, writeQueue=0, modifiedSQLExecuted=false]
4)數據庫備份策略
mysql備份的三種方式詳解