MyCat的核心概念

1.逻辑数据库 schema.xml
2.分片规则 rule.xml

单库大表拆分

  1. <?xml version="1.0"?>
  2. <!DOCTYPE mycat:schema SYSTEM "schema.dtd">
  3. <mycat:schema xmlns:mycat="http://io.mycat/">
  4. <!-- 逻辑库 -->
  5. <!--
  6. checkSQLschema=false 处理表名的序号
  7. sqlMaxLimit = 100 只返回100
  8. -->
  9. <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
  10. <!-- 逻辑表 -->
  11. <!--
  12. name:逻辑表名
  13. dataNode:节点
  14. rule:分片规则
  15. mod-long:取模
  16. -->
  17. <table name="company" subTables="company$1-3" dataNode="dn1" rule="mod-long"/>
  18. </schema>
  19. <!-- 数据节点 -->
  20. <dataNode name="dn1" dataHost="localhost1" database="db" />
  21. <!-- host配置 -->
  22. <dataHost name="localhost1" maxCon="1000" minCon="10" balance="0"
  23. writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
  24. <heartbeat>select user()</heartbeat>
  25. <writeHost host="hostM1" url="localhost:3306" user="root"
  26. password="123456">
  27. </writeHost>
  28. </dataHost>
  29. </mycat:schema>

跨库大表拆分

  1. <?xml version="1.0"?>
  2. <!DOCTYPE mycat:schema SYSTEM "schema.dtd">
  3. <mycat:schema xmlns:mycat="http://io.mycat/">
  4. <!-- 逻辑库 -->
  5. <!--
  6. checkSQLschema=false 处理表名的序号
  7. sqlMaxLimit = 100 只返回100
  8. -->
  9. <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" randomDataNode="dn1">
  10. <!-- 逻辑表 -->
  11. <!--
  12. name:逻辑表名
  13. dataNode:节点
  14. rule:分片规则
  15. mod-long:取模
  16. -->
  17. <table name="company" subTables="company$1-3" dataNode="dn1" rule="mod-long"/>
  18. <table name="record" dataNode="dn1,dn2,dn1" rule="mod-long"/>
  19. </schema>
  20. <!-- 数据节点 -->
  21. <dataNode name="dn1" dataHost="localhost1" database="db1" />
  22. <dataNode name="dn2" dataHost="localhost1" database="db2" />
  23. <dataNode name="dn3" dataHost="localhost1" database="db3" />
  24. <!-- host配置 -->
  25. <dataHost name="localhost1" maxCon="1000" minCon="10" balance="0"
  26. writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
  27. <heartbeat>select user()</heartbeat>
  28. <writeHost host="hostM1" url="localhost:3306" user="root"
  29. password="123456">
  30. </writeHost>
  31. </dataHost>
  32. </mycat:schema>

读写分离

  1. <?xml version="1.0"?>
  2. <!DOCTYPE mycat:schema SYSTEM "schema.dtd">
  3. <mycat:schema xmlns:mycat="http://io.mycat/">
  4. <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1">
  5. </schema>
  6. <!-- 数据节点 -->
  7. <dataNode name="dn1" dataHost="localhost1" database="db" />
  8. <!-- host配置 -->
  9. <dataHost name="localhost1" maxCon="1000" minCon="10" balance="0"
  10. writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
  11. <!-- 心跳检测 -->
  12. <heartbeat>show slave status</heartbeat>
  13. <writeHost host="hostM1" url="localhost:3306" user="root"
  14. password="123456">
  15. <readHost host="localhost1" url="localhost:3306" password="root" user="123456" />
  16. </writeHost>
  17. </dataHost>
  18. </mycat:schema>

数据库负载均衡:Haproxy

数据库高可用:keepalive