Mycat分库分表 读写分离 主从切换
一、Mycat及MySQL实例部署
Mycat:
IP:10.20.8.57,Port:3310/3311
MySQL:
db1-M1,IP:10.20.8.126,Port:3306
db1-M2,IP:10.20.8.126,Port:3307
db2-M1,IP:10.25.80.7,Port:3307
二、配置Mycat
(1)server.xml:
<!DOCTYPE mycat:server SYSTEM "server.dtd"><mycat:server xmlns:mycat="http://io.mycat/">
<system>
<property name="useSqlStat">1
</property>
<property name="useGlobleTableCheck">0
</property>
<property name="defaultSqlParser">druidparser
</property>
<property name="sequnceHandlerType">2
</property>
<property name="processorBufferPoolType">0
</property>
<property name="serverPort">3310
</property>
<!-- mycat的使用端口 -->
<property name="managerPort">3311
</property>
<!-- mycat的管理端口 -->
<property name="handleDistributedTransactions">0
</property>
<property name="useOffHeapForMerge">1
</property>
<property name="memoryPageSize">1m
</property>
<property name="spillsFileBufferSize">1k
</property>
<property name="useStreamOutput">0
</property>
<property name="systemReserveMemorySize">389m
</property>
</system>
<user name="root">
<property name="password">123456
</property>
<property name="schemas">db
</property>
<property name="readOnly">false
</property>
</user></mycat:server>
schema.xml:
<?xml version="1.0"?><!DOCTYPE mycat:schema SYSTEM "schema.dtd"><mycat:schema xmlns:mycat="http://io.mycat/">
<!-- 数据库配置,与server.xml中的数据库对应 -->
<schema name="db" checkSQLschema="false" sqlMaxLimit="100">
<table name="t1" dataNode="dn1,dn2" rule="mod-long" />
<!-- 对2取模,详见rule.xml -->
</schema>
<!-- 分片配置 -->
<dataNode name="dn1" dataHost="shard1" database="db1" />
<dataNode name="dn2" dataHost="shard2" database="db2" />
<!-- 物理数据库配置 -->
<dataHost name="shard1" maxCon="1000" minCon="10" balance="3" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user();
</heartbeat>
<writeHost host="db1-M1" url="10.20.8.126:3306" user="root" password="123456">
<readHost host="db1-M2" url="10.20.8.126:3307" user="root" password="123456" />
</writeHost>
<!-- 配置standby writeHost -->
<writeHost host="db1-M2" url="10.20.8.126:3307" user="root" password="123456">
</writeHost>
</dataHost>
<dataHost name="shard2" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user();
</heartbeat>
<writeHost host="db2-M1" url="10.25.80.7:3307" user="root" password="123456">
</writeHost>
</dataHost>
</mycat:schema>
rule.xml:
<tableRule name="mod-long">
<rule>
<columns>id
</columns>
<!-- t1的分片列 -->
<algorithm>mod-long
</algorithm>
</rule></tableRule>
<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
<!-- how many data nodes -->
<property name="count">2
</property>
<!-- count值与分片个数相同 --></function>
(二)启动Mycat
[root@SZB-L0059021 bin]# ./mycat start
Starting Mycat-server...
[root@SZB-L0059021 bin]# ./mycat status
Mycat-server is running (27020).
[root@SZB-L0059021 bin]# mysql -uroot -p123456 -Ddb -h127.0.0.1 -P3310