Mycat读写分离+高可用笔记
Mycat基础配置
用户创建及数据库导入
mysql -S /data/3307/mysql.sock
grant all on *.* to root@'10.0.0.%' identified by '123';
source /root/world.sql
mysql -S /data/3308/mysql.sock
grant all on *.* to root@'10.0.0.%' identified by '123';
source /root/world.sql
mycat实现1主1从读写分离
cd /app/mycat/conf/
mv schema.xml schema.xml.bak
vim schema.xml
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="sh1">
</schema>
<dataNode name="sh1" dataHost="worldtest" database= "world" />
<dataHost name="worldtest" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1">
<heartbeat>select user()</heartbeat>
<writeHost host="db1" url="10.0.0.12:3307" user="root" password="123">
<readHost host="db2" url="10.0.0.12:3309" user="root" password="123" />
</writeHost>
</dataHost>
</mycat:schema>
测试是否可用
mysql> begin;select @@server_id;commit;
Query OK, 0 rows affected (0.00 sec)
+-------------+
| @@server_id |
+-------------+
| 7 |
+-------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 9 |
+-------------+
1 row in set (0.01 sec)
Mycat高可用+读写分离
mv schema.xml schema.xml.1
vim schema.xml
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="sh1">
</schema>
<dataNode name="sh1" dataHost="worldtest" database= "world" />
<dataHost name="worldtest" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1">
<heartbeat>select user()</heartbeat>
<writeHost host="db1" url="10.0.0.12:3307" user="root" password="123">
<readHost host="db2" url="10.0.0.12:3309" user="root" password="123" />
</writeHost>
<writeHost host="db3" url="10.0.0.13:3307" user="root" password="123">
<readHost host="db4" url="10.0.0.13:3309" user="root" password="123" />
</writeHost>
</dataHost>
</mycat:schema>
第一个 whost: 10.0.0.12:3307 真正的写节点,负责写操作
第二个 whost: 10.0.0.13:3307 准备写节点,负责读,当 10.0.0.12:3307宕掉,会切换为真正的写节点
测试:
[root@db01 conf]# mysql -uroot -p123456 -h 10.0.0.10 -P 8066
读:
mysql> select @@server_id;
写:
mysql> begin ;select @@server_id; commit;