使用mycat 对数据库做分库
- 首先这边是有四个测试库环境 两台在3.10 两台在3.125
- mycat 分片常用的规则 https://blog.csdn.net/tototuzuoquan/article/details/80362892
- 关于mycat的核心配置有三个xml 文件分别是rule 、server、schema 下面分别是我这边三个xml的配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mycat:rule SYSTEM "rule.dtd"> <mycat:rule xmlns:mycat="http://io.mycat/"> <tableRule name="sharding-by-mod"> <rule> <!--属性指定对应表中用于分片的列名--> <columns>uid</columns> <!--属性对应function 中指定的算法的名称--> <algorithm>sharding-by-mod</algorithm> </rule> </tableRule> <tableRule name="sharding-by-murmur-userid"> <rule> <columns>Id</columns> <algorithm>sharding-by-murmur-userid</algorithm> </rule> </tableRule> <tableRule name="sharding-by-murmur-fzid"> <rule> <columns>FzId</columns> <algorithm>sharding-by-murmur-userid</algorithm> </rule> </tableRule> <function name="sharding-by-mod" class="io.mycat.route.function.PartitionByMod"> <!-- 共有多少个节点 --> <property name="count">4</property> </function> <function name="sharding-by-murmur-userid" class="io.mycat.route.function.PartitionByMurmurHash"> <property name="seed">0</property><!-- 默认是0 --> <property name="count">4</property><!-- 要分片的数据库节点数量,必须指定,否则没法分片 --> <property name="virtualBucketTimes">160</property><!-- 一个实际的数据库节点被映射为这么多虚拟节点,默认是160倍,也就是虚拟节点数是物理节点数的160倍 --> <!-- <property name="weightMapFile">weightMapFile</property> 节点的权重,没有指定权重的节点默认是1。以properties文件的格式填写,以从0开始到count-1的整数值也就是节点索引为key,以节点权重值为值。>所有权重值必须是正整数,否则以1代替 --> <!-- <property name="bucketMapPath">/usr/local/mycat/logs/bucketMapPath-murmur-userid</property> 用于测试时观察各物理节点与虚拟节点的分布情况,如果指定了这个属性,会把虚拟节点的murmur hash值与物理节点的映射按行输出到这个文件,没有默认值,如果不指定,就不会输出任何东西 --> </function> </mycat:rule>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mycat:server SYSTEM "server.dtd"> <mycat:server xmlns:mycat="http://io.mycat/"> <system> <property name="useSqlStat">0</property> <!-- 1为开启实时统计、0为关闭 --> <property name="useGlobleTableCheck">0</property> <!-- 1为开启全加班一致性检测、0为关闭 --> <property name="sequnceHandlerType">1</property> <property name="processorBufferPoolType">0</property> <property name="serverPort">8066</property> <!-- 8066 为连接mycat的端口--> <property name="managerPort">9066</property> <!-- 9066 为连接mycat管理地址的端口--> <property name="idleTimeout">300000</property> <property name="bindIp">0.0.0.0</property> <property name="frontWriteQueueSize">4096</property> <property name="processors">32</property> <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">384m</property> <property name="useZKSwitch">true</property> </system> <!-- 配置连接mycat的账号密码, 和逻辑库名称, 可以设置多个--> <user name="root"> <property name="password">123456</property> <!-- 多个数据库用英文逗号隔开--> <property name="schemas">test,fzuser</property> </user> <user name="user"> <property name="password">123456</property> <property name="schemas">test</property> <property name="readOnly">true</property> </user> </mycat:server>
<?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/"> <!-- name="TESTDB"是在server.xml中配置<property name="schemas">TESTDB</property>--> <schema name="test" checkSQLschema="false" sqlMaxLimit="100" > <table name="tb_user" dataNode="dn1,dn2,dn3,dn4" rule="sharding-by-mod" primaryKey="uid"/> <table name="tb_user_area" dataNode="dn1,dn2,dn3,dn4" rule="sharding-by-mod" primaryKey="aid"/> </schema> <schema name="fzuser" checkSQLschema="false" sqlMaxLimit="100" > <table name="tb_account" dataNode="user1,user2,user3,user4" rule="sharding-by-murmur-fzid" primaryKey="FzId"/> <table name="tb_parent_student_relation" dataNode="user1,user2,user3,user4" rule="sharding-by-murmur-userid" primaryKey="Id"/> <table name="tb_student_class" dataNode="user1,user2,user3,user4" rule="sharding-by-murmur-userid" primaryKey="Id"/> <table name="tb_teacher_class" dataNode="user1,user2,user3,user4" rule="sharding-by-murmur-userid" primaryKey="Id"/> <table name="tb_third_account" dataNode="user1,user2,user3,user4" rule="sharding-by-murmur-userid" primaryKey="Id"/> <table name="tb_user" dataNode="user1,user2,user3,user4" rule="sharding-by-murmur-userid" primaryKey="Id"/> </schema> <!--配置mysql数据库testdb2,cespimp_hazard mycat逻辑库名为 dn1,dn2,dn3--> <dataNode name="dn1" dataHost="dataHost01" database="mycat1" /> <dataNode name="dn2" dataHost="dataHost01" database="mycat2" /> <dataNode name="dn3" dataHost="dataHost02" database="mycat3" /> <dataNode name="dn4" dataHost="dataHost02" database="mycat4" /> <dataNode name="user1" dataHost="dataHost01" database="fz_platform_userdata" /> <dataNode name="user2" dataHost="dataHost01" database="fz_platform_userdata2" /> <dataNode name="user3" dataHost="dataHost02" database="fz_platform_userdata3" /> <dataNode name="user4" dataHost="dataHost02" database="fz_platform_userdata4" /> <!--配置数据类型 balance="0"读写不分离 --> <dataHost name="dataHost01" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <!--心跳包 --> <heartbeat>select user()</heartbeat> <!--配置mysql数据 账户密码 --> <writeHost host="hostM1" url="192.168.3.125:3306" user="root" password="k8-sgM&W" /> </dataHost> <!--配置数据类型 balance="0"读写不分离 --> <dataHost name="dataHost02" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <!--心跳包 --> <heartbeat>select user()</heartbeat> <!--配置mysql数据 账户密码 --> <writeHost host="hostM2" url="192.168.3.10:3306" user="root" password="123456" /> </dataHost> </mycat:schema>
如果是需要使用到join 建议使用E R 分片
-
<?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/"> <!-- name="TESTDB"是在server.xml中配置<property name="schemas">TESTDB</property>--> <schema name="test" checkSQLschema="false" sqlMaxLimit="100" > <table name="tb_user" dataNode="dn1,dn2,dn3,dn4" rule="sharding-by-mod" primaryKey="uid"/> <table name="tb_user_area" dataNode="dn1,dn2,dn3,dn4" rule="sharding-by-mod" primaryKey="aid"/> </schema> <schema name="fzuser" checkSQLschema="false" sqlMaxLimit="100" > <table name="tb_account" dataNode="user1,user2,user3,user4" rule="sharding-by-murmur-fzid" primaryKey="FzId"/> <table name="tb_user" dataNode="user1,user2,user3,user4" rule="sharding-by-murmur-userid" primaryKey="Id"> <childTable name="tb_student_class" joinKey="StudentId" parentKey="Id" primaryKey="Id"/> <childTable name="tb_teacher_class" joinKey="TeacherId" parentKey="Id" primaryKey="Id"/> <childTable name="tb_parent_student_relation" joinKey="StudentId" parentKey="Id" primaryKey="Id"/> <childTable name="tb_third_account" joinKey="UserId" parentKey="Id" primaryKey="Id"/> <childTable name="tb_use_product" joinKey="UserId" parentKey="Id" primaryKey="Id"/> <childTable name="tb_user_mergelog" joinKey="UserId" parentKey="Id" primaryKey="Id"/> </table> </schema> <!--配置mysql数据库testdb2,cespimp_hazard mycat逻辑库名为 dn1,dn2,dn3--> <dataNode name="dn1" dataHost="dataHost01" database="mycat1" /> <dataNode name="dn2" dataHost="dataHost01" database="mycat2" /> <dataNode name="dn3" dataHost="dataHost02" database="mycat3" /> <dataNode name="dn4" dataHost="dataHost02" database="mycat4" /> <dataNode name="user1" dataHost="dataHost01" database="fz_platform_userdata" /> <dataNode name="user2" dataHost="dataHost01" database="fz_platform_userdata2" /> <dataNode name="user3" dataHost="dataHost02" database="fz_platform_userdata3" /> <dataNode name="user4" dataHost="dataHost02" database="fz_platform_userdata4" /> <!--配置数据类型 balance="0"读写不分离 --> <dataHost name="dataHost01" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <!--心跳包 --> <heartbeat>select user()</heartbeat> <!--配置mysql数据 账户密码 --> <writeHost host="hostM1" url="192.168.3.125:3306" user="root" password="k8-sgM&W" /> </dataHost> <!--配置数据类型 balance="0"读写不分离 --> <dataHost name="dataHost02" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <!--心跳包 --> <heartbeat>select user()</heartbeat> <!--配置mysql数据 账户密码 --> <writeHost host="hostM2" url="192.168.3.10:3306" user="root" password="123456" /> </dataHost> </mycat:schema> ~
- 然后使用mysql客户端链接工具使用 server.xml 配置好的账号密码进行登录 。mycat端口默认是8066
stay hungry stay foolish!