項目中存在多個數據庫,每個數據庫中有很多張表,可以通過mycat指定查詢該表的時候去那個庫中查詢,這樣減少了代碼的大量書寫和配置。
1.增加customer表 該表在mycat02庫中
2.配置 schema.xml
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1">
<table name="customer" dataNode="dn2" ></table>
</schema>
<dataNode name="dn1" dataHost="host1" database="mycat01" />
<dataNode name="dn2" dataHost="host2" database="mycat02" />
<dataHost name="host1" maxCon="1000" minCon="10" balance="2"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostm1" url="xxxxxx:3306" user="root"
password="123123">
<readHost host="hosts1" url="aaaaaaa:3306" user="root"
password="123123">
</readHost>
</writeHost>
</dataHost>
<!-- 制定customer表所在的mysql服務器地址 -->
<dataHost name="host2" maxCon="1000" minCon="10" balance="0"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostm2" url="xxxxxxxx:3306" user="root"
password="123123">
</writeHost>
</dataHost>
</mycat:schema>
2.重啓mycat
./mycat start