{"id":1787,"date":"2023-03-27T22:20:53","date_gmt":"2023-03-27T14:20:53","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1787"},"modified":"2023-04-23T21:15:33","modified_gmt":"2023-04-23T13:15:33","slug":"mycat-sharding-implementation","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/27\/mycat-sharding-implementation\/","title":{"rendered":"MyCat\u5206\u7247\u5b9e\u73b0"},"content":{"rendered":"<h2>MyCat\u4ecb\u7ecd<\/h2>\n<p>\u5b98\u65b9\u7f51\u7ad9\uff1a\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/www.mycat.org.cn\/\">http:\/\/www.mycat.org.cn\/<\/a><\/p>\n<h3>MyCat\u6838\u5fc3\u6982\u5ff5<\/h3>\n<p><!-- more --><\/p>\n<ul>\n<li><code>Schema<\/code>\uff1a\u7531\u5b83\u6307\u5b9a\u903b\u8f91\u6570\u636e\u5e93\uff08\u76f8\u5f53\u4e8eMySQL\u7684database\u6570\u636e\u5e93\uff09<\/li>\n<li><code>Table<\/code>\uff1a\u903b\u8f91\u8868\uff08\u76f8\u5f53\u4e8eMySQL\u7684table\u8868\uff09<\/li>\n<li><code>DataNode<\/code>\uff1a\u771f\u6b63\u5b58\u50a8\u6570\u636e\u7684\u7269\u7406\u8282\u70b9<\/li>\n<li><code>DataHost<\/code>\uff1a\u5b58\u50a8\u8282\u70b9\u6240\u5728\u7684\u6570\u636e\u5e93\u4e3b\u673a\uff08\u6307\u5b9aMySQL\u6570\u636e\u5e93\u7684\u8fde\u63a5\u4fe1\u606f\uff09<\/li>\n<li><code>User<\/code>\uff1aMyCat\u7684\u7528\u6237\uff08\u7c7b\u4f3c\u4e8eMySQL\u7684\u7528\u6237\uff0c\u652f\u6301\u591a\u7528\u6237\uff09<\/li>\n<\/ul>\n<h3>MyCat\u4e3b\u8981\u89e3\u51b3\u95ee\u9898<\/h3>\n<ul>\n<li>\u6d77\u91cf\u6570\u636e\u4fdd\u5b58<\/li>\n<li>\u67e5\u8be2\u4f18\u5316<\/li>\n<\/ul>\n<h3>MyCat\u5bf9\u591a\u6570\u636e\u5e93\u7684\u652f\u6301<\/h3>\n<ul>\n<li>MySQL<\/li>\n<li>Oracle<\/li>\n<li>PostgreSQL<\/li>\n<li>SQL Server<\/li>\n<li>MongoDB<\/li>\n<\/ul>\n<h3>MyCat\u5206\u7247\u7b56\u7565<\/h3>\n<p>MyCAT\u652f\u6301\u6c34\u5e73\u5206\u7247\u4e0e\u5782\u76f4\u5206\u7247\uff1a<\/p>\n<ul>\n<li>\u6c34\u5e73\u5206\u7247\uff1a\u4e00\u4e2a\u8868\u683c\u7684\u6570\u636e\u5206\u5272\u5230\u591a\u4e2a\u8282\u70b9\u4e0a\uff0c\u6309\u7167\u884c\u5206\u9694<\/li>\n<li>\u5782\u76f4\u5206\u7247\uff1a\u4e00\u4e2a\u6570\u636e\u5e93\u4e2d\u591a\u4e2a\u8868\u683cA\uff0cB\uff0cC\uff0cA\u5b58\u50a8\u5230\u8282\u70b91\u4e0a\uff0cB\u5b58\u50a8\u5230\u8282\u70b92\u4e0a\uff0cC\u5b58\u50a8\u5230\u8282\u70b93 \u4e0a<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Java\/MyCat\u5206\u7247\u7b56\u7565.png\" alt=\"MyCat\u5206\u7247\u7b56\u7565\" \/><\/p>\n<p>MyCAT\u901a\u8fc7\u5b9a\u4e49\u8868\u7684\u5206\u7247\u89c4\u5219\u6765\u5b9e\u73b0\u5206\u7247\uff0c\u6bcf\u4e2a\u8868\u683c\u53ef\u4ee5\u6346\u7ed1\u4e00\u4e2a\u5206\u7247\u89c4\u5219\uff0c\u6bcf\u4e2a\u5206\u7247\u89c4\u5219\u6307\u5b9a\u4e00\u4e2a\u5206\u7247\u5b57\u6bb5\u5e76\u7ed1\u5b9a\u4e00\u4e2a\u51fd\u6570\uff0c\u6765\u5b9e\u73b0\u52a8\u6001\u5206\u7247\u7b97\u6cd5\u3002<\/p>\n<ul>\n<li><code>Schema<\/code>\uff1a\u903b\u8f91\u5e93\uff0c\u4e0eMySQL\u4e2d\u7684Database\uff08\u6570\u636e\u5e93\uff09\u5bf9\u5e94\uff0c\u4e00\u4e2a\u903b\u8f91\u5e93\u4e2d\u5b9a\u4e49\u4e86\u6240\u5305\u62ec\u7684Table<\/li>\n<li><code>Table<\/code>\uff1a\u8868\uff0c\u5373\u7269\u7406\u6570\u636e\u5e93\u4e2d\u5b58\u50a8\u7684\u67d0\u4e00\u5f20\u8868\uff0c\u4e0e\u4f20\u7edf\u6570\u636e\u5e93\u4e0d\u540c\uff0c\u8fd9\u91cc\u7684\u8868\u683c\u9700\u8981\u58f0\u660e\u5176\u6240\u5b58\u50a8\u7684\u903b\u8f91\u6570\u636e\u8282\u70b9DataNode\u3002\u5728\u6b64\u53ef\u4ee5\u6307\u5b9a\u8868\u7684\u5206\u7247\u89c4\u5219<\/li>\n<li><code>DataNode<\/code>\uff1aMyCAT\u7684\u903b\u8f91\u6570\u636e\u8282\u70b9\uff0c\u662f\u5b58\u653etable\u7684\u5177\u4f53\u7269\u7406\u8282\u70b9\uff0c\u4e5f\u79f0\u4e4b\u4e3a\u5206\u7247\u8282\u70b9\uff0c\u901a\u8fc7 DataHost\u6765\u5173\u8054\u5230\u540e\u7aef\u67d0\u4e2a\u5177\u4f53\u6570\u636e\u5e93\u4e0a<\/li>\n<li><code>DataHost<\/code>\uff1a\u5b9a\u4e49\u67d0\u4e2a\u7269\u7406\u5e93\u7684\u8bbf\u95ee\u5730\u5740\uff0c\u7528\u4e8e\u6346\u7ed1\u5230Datanode\u4e0a<\/li>\n<\/ul>\n<h2>MyCat\u5b89\u88c5<\/h2>\n<h3>\u5b89\u88c5\u5e76\u542f\u52a8MyCat<\/h3>\n<pre><code class=\"language-bash\">wget http:\/\/dl.mycat.org.cn\/1.6.7.3\/20190927161129\/Mycat-server-1.6.7.3-release-20190927161129-linux.tar.gz\ntar -zxf Mycat-server-1.6.7.3-release-20190927161129-linux.tar.gz<\/code><\/pre>\n<p>\u8fdb\u5165<code>mycat\/bin<\/code>\uff0c\u542f\u52a8MyCat<\/p>\n<ul>\n<li>\u542f\u52a8\u547d\u4ee4\uff1a<code>.\/mycat start<\/code><\/li>\n<li>\u505c\u6b62\u547d\u4ee4\uff1a<code>.\/mycat stop<\/code><\/li>\n<li>\u91cd\u542f\u547d\u4ee4\uff1a<code>.\/mycat restart<\/code><\/li>\n<li>\u67e5\u770b\u72b6\u6001\uff1a<code>.\/mycat status<\/code><\/li>\n<\/ul>\n<h3>\u8bbf\u95eeMyCat<\/h3>\n<p>\u4f7f\u7528MySQL\u7684\u5ba2\u6237\u7aef\u76f4\u63a5\u8fde\u63a5mycat\u670d\u52a1\u3002\u9ed8\u8ba4\u670d\u52a1\u7aef\u53e3\u4e3a<code>8066<\/code><\/p>\n<pre><code>mysql -uroot -p123456 -h127.0.0.1 -P8066<\/code><\/pre>\n<h2>MyCat\u5206\u7247<\/h2>\n<h3>\u914d\u7f6eschema.xml<\/h3>\n<h4>\u4ec0\u4e48\u662fschema.xml<\/h4>\n<p><code>schema.xml<\/code>\u4f5c\u4e3aMyCat\u4e2d\u91cd\u8981\u7684\u914d\u7f6e\u6587\u4ef6\u4e4b\u4e00\uff0c\u7ba1\u7406\u7740MyCat\u7684\u903b\u8f91\u5e93\u3001\u8868\u3001\u5206\u7247\u89c4\u5219\u3001DataNode \u4ee5\u53caDataHost\u4e4b\u95f4\u7684\u6620\u5c04\u5173\u7cfb\u3002\u5f04\u61c2\u8fd9\u4e9b\u914d\u7f6e\uff0c\u662f\u6b63\u786e\u4f7f\u7528MyCat\u7684\u524d\u63d0\u3002<\/p>\n<ul>\n<li><code>schema<\/code> \u6807\u7b7e\u7528\u4e8e\u5b9a\u4e49MyCat\u5b9e\u4f8b\u4e2d\u7684\u903b\u8f91\u5e93<\/li>\n<li><code>Table<\/code> \u6807\u7b7e\u5b9a\u4e49\u4e86MyCat\u4e2d\u7684\u903b\u8f91\u8868<\/li>\n<li><code>dataNode<\/code> \u6807\u7b7e\u5b9a\u4e49\u4e86MyCat\u4e2d\u7684\u6570\u636e\u8282\u70b9\uff0c\u4e5f\u5c31\u662f\u6211\u4eec\u901a\u5e38\u8bf4\u6240\u7684\u6570\u636e\u5206\u7247<\/li>\n<li><code>dataHost<\/code> \u6807\u7b7e\u5728MyCat\u903b\u8f91\u5e93\u4e2d\u4e5f\u662f\u4f5c\u4e3a\u6700\u5e95\u5c42\u7684\u6807\u7b7e\u5b58\u5728\uff0c\u76f4\u63a5\u5b9a\u4e49\u4e86\u5177\u4f53\u7684\u6570\u636e\u5e93\u5b9e\u4f8b\u3001\u8bfb\u5199\u5206\u79bb\u914d\u7f6e\u548c\u5fc3\u8df3\u8bed\u53e5<\/li>\n<\/ul>\n<h4>Schema.xml \u914d\u7f6e<\/h4>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot;?&gt;\n&lt;!DOCTYPE mycat:schema SYSTEM &quot;schema.dtd&quot;&gt;\n&lt;mycat:schema xmlns:mycat=&quot;http:\/\/io.mycat\/&quot;&gt;\n    &lt;!--\n    schema: \u903b\u8f91\u5e93 name:\u903b\u8f91\u5e93\u540d\u79f0\n    sqlMaxLimit\uff1a\u4e00\u6b21\u53d6\u591a\u5c11\u6761\u6570\u636e \u8981\u8d85\u8fc7\u7528limit xxx\n    table:\u903b\u8f91\u8868\n    dataNode: \u6570\u636e\u8282\u70b9 \u5bf9\u5e94datanode\u6807\u7b7e\n    rule\uff1a\u5206\u7247\u89c4\u5219\uff0c\u5bf9\u5e94rule.xml\n    subTables: \u5b50\u8868\n    primaryKey\uff1a\u5206\u7247\u4e3b\u952e \u53ef\u7f13\u5b58\n    --&gt;\n    &lt;schema name=&quot;TESTDB&quot; checkSQLschema=&quot;false&quot; sqlMaxLimit=&quot;100&quot;&gt;\n        &lt;!-- auto sharding by id (long) --&gt;\n        &lt;table name=&quot;item&quot; dataNode=&quot;dn1,dn2,dn3&quot; rule=&quot;mod-long&quot;\n               primaryKey=&quot;ID&quot;\/&gt;\n    &lt;\/schema&gt;\n    &lt;!-- &lt;dataNode name=&quot;dn1$0-743&quot; dataHost=&quot;localhost1&quot; database=&quot;db$0-743&quot;\/&gt; --&gt;\n    &lt;dataNode name=&quot;dn1&quot; dataHost=&quot;localhost1&quot; database=&quot;db1&quot; \/&gt;\n    &lt;dataNode name=&quot;dn2&quot; dataHost=&quot;localhost1&quot; database=&quot;db2&quot; \/&gt;\n    &lt;dataNode name=&quot;dn3&quot; dataHost=&quot;localhost1&quot; database=&quot;db3&quot; \/&gt;\n    &lt;!--\n    dataHost: \u6570\u636e\u4e3b\u673a\uff08\u8282\u70b9\u4e3b\u673a\uff09\n    balance\uff1a1\uff1a\u8bfb\u5199\u5206\u79bb 0\uff1a\u8bfb\u5199\u4e0d\u5206\u79bb\n    writeType\uff1a0 \u7b2c\u4e00\u4e2awriteHost\u5199\uff0c 1 \u968f\u673awriteHost\u5199\n    dbDriver\uff1a\u6570\u636e\u5e93\u9a71\u52a8 native\uff1aMySQL JDBC\uff1aOracle\u3001SQLServer\n    switchType\uff1a\u662f\u5426\u4e3b\u52a8\u8bfb\n    1 \u4e3b\u4ece\u81ea\u52a8\u5207\u6362 -1 \u4e0d\u5207\u6362 2 \u5f53\u4ece\u673a\u5ef6\u65f6\u8d85\u8fc7slaveThreshold\u503c\u65f6\u5207\u6362\u4e3a\u4e3b\u8bfb\n    --&gt;\n    &lt;dataHost name=&quot;localhost1&quot; maxCon=&quot;1000&quot; minCon=&quot;10&quot; balance=&quot;0&quot;\n              writeType=&quot;0&quot; dbType=&quot;mysql&quot; dbDriver=&quot;native&quot; switchType=&quot;1&quot;\n              slaveThreshold=&quot;100&quot;&gt;\n        &lt;heartbeat&gt;select user()&lt;\/heartbeat&gt;\n        &lt;writeHost host=&quot;hostM1&quot; url=&quot;192.168.24.129:3306&quot; user=&quot;root&quot;\n                   password=&quot;root&quot; &gt;\n        &lt;\/writeHost&gt;\n    &lt;\/dataHost&gt;\n&lt;\/mycat:schema&gt;<\/code><\/pre>\n<h2>\u914d\u7f6eserver.xml<\/h2>\n<h3>server.xml \u4ecb\u7ecd<\/h3>\n<p><code>server.xml<\/code>\u51e0\u4e4e\u4fdd\u5b58\u4e86\u6240\u6709MyCat\u9700\u8981\u7684\u7cfb\u7edf\u914d\u7f6e\u4fe1\u606f\u3002\u6700\u5e38\u7528\u7684\u662f\u5728\u6b64\u914d\u7f6e\u7528\u6237\u540d\u3001\u5bc6\u7801\u53ca\u6743\u9650\u3002<\/p>\n<h3>server.xml \u914d\u7f6e<\/h3>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\n&lt;!DOCTYPE mycat:server SYSTEM &quot;server.dtd&quot;&gt;\n&lt;mycat:server xmlns:mycat=&quot;http:\/\/io.mycat\/&quot;&gt;\n    &lt;system&gt;\n        &lt;property name=&quot;defaultSqlParser&quot;&gt;druidparser&lt;\/property&gt;\n    &lt;\/system&gt;\n    &lt;user name=&quot;mycat&quot;&gt;\n        &lt;property name=&quot;password&quot;&gt;mycat&lt;\/property&gt;\n        &lt;property name=&quot;schemas&quot;&gt;TESTDB&lt;\/property&gt;\n    &lt;\/user&gt;\n&lt;\/mycat:server&gt;<\/code><\/pre>\n<h2>\u914d\u7f6erule.xml<\/h2>\n<p><code>rule.xml<\/code>\u91cc\u9762\u5c31\u5b9a\u4e49\u4e86\u6211\u4eec\u5bf9\u8868\u8fdb\u884c\u62c6\u5206\u6240\u6d89\u53ca\u5230\u7684\u89c4\u5219\u5b9a\u4e49\u3002\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u7684\u5bf9\u8868\u4f7f\u7528\u4e0d\u540c\u7684\u5206\u7247\u7b97\u6cd5\uff0c\u6216\u8005\u5bf9\u8868\u4f7f\u7528\u76f8\u540c\u7684\u7b97\u6cd5\u4f46\u5177\u4f53\u7684\u53c2\u6570\u4e0d\u540c\u3002\u8fd9\u4e2a\u6587\u4ef6\u91cc\u9762\u4e3b\u8981\u6709<code>tableRule<\/code>\u548c<code>function<\/code>\u8fd9\u4e24\u4e2a\u6807\u7b7e\u3002\u5728\u5177\u4f53\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u53ef\u4ee5\u6309\u7167\u9700\u6c42\u6dfb\u52a0<code>tableRule<\/code>\u548c<code>function<\/code>\u3002\u6b64\u914d\u7f6e\u6587\u4ef6\u53ef\u4ee5\u4e0d\u7528\u4fee\u6539\uff0c\u4f7f\u7528\u9ed8\u8ba4\u5373\u53ef<\/p>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\n&lt;!DOCTYPE mycat:rule SYSTEM &quot;rule.dtd&quot;&gt;\n&lt;mycat:rule xmlns:mycat=\u201dhttp:\/\/io.mycat\/\u201c &gt;\n&lt;tableRule name=&quot;sharding-by-intfile&quot;&gt;\n&lt;rule&gt;\n    &lt;columns&gt;sharding_id&lt;\/columns&gt;\n    &lt;algorithm&gt;hash-int&lt;\/algorithm&gt;\n&lt;\/rule&gt;\n&lt;\/tableRule&gt;\n&lt;function name=&quot;hash-int&quot;\n          class=&quot;io.mycat.route.function.PartitionByFileMap&quot;&gt;\n    &lt;property name=&quot;mapFile&quot;&gt;partition-hash-int.txt&lt;\/property&gt;\n&lt;\/function&gt;\n&lt;\/mycat:rule&gt;<\/code><\/pre>\n<h3>tableRule \u6807\u7b7e\u914d\u7f6e\u8bf4\u660e<\/h3>\n<ul>\n<li>name \u5c5e\u6027\u6307\u5b9a\u552f\u4e00\u7684\u540d\u5b57\uff0c\u7528\u4e8e\u6807\u8bc6\u4e0d\u540c\u7684\u8868\u89c4\u5219<\/li>\n<li>rule \u6807\u7b7e\u5219\u6307\u5b9a\u5bf9\u7269\u7406\u8868\u4e2d\u7684\u54ea\u4e00\u5217\u8fdb\u884c\u62c6\u5206\u548c\u4f7f\u7528\u4ec0\u4e48\u8def\u7531\u7b97\u6cd5<\/li>\n<li>columns \u5185\u6307\u5b9a\u8981\u62c6\u5206\u7684\u5217\u540d\u5b57<\/li>\n<li>algorithm \u4f7f\u7528 function \u6807\u7b7e\u4e2d\u7684 name \u5c5e\u6027\uff0c\u8fde\u63a5\u8868\u89c4\u5219\u548c\u5177\u4f53\u8def\u7531\u7b97\u6cd5\u3002\u5f53\u7136\uff0c\u591a\u4e2a\u8868\u89c4\u5219\u53ef\u4ee5\u8fde\u63a5\u5230\u540c\u4e00\u4e2a\u8def\u7531\u7b97\u6cd5\u4e0a<\/li>\n<li>table \u6807\u7b7e\u5185\u4f7f\u7528\uff0c\u8ba9\u903b\u8f91\u8868\u4f7f\u7528\u8fd9\u4e2a\u89c4\u5219\u8fdb\u884c\u5206\u7247<\/li>\n<\/ul>\n<h3>function \u6807\u7b7e\u914d\u7f6e\u8bf4\u660e<\/h3>\n<ul>\n<li>name \u6307\u5b9a\u7b97\u6cd5\u7684\u540d\u5b57<\/li>\n<li>class \u6307\u5b9a\u8def\u7531\u7b97\u6cd5\u5177\u4f53\u7684\u7c7b\u540d\u5b57<\/li>\n<li>property \u4e3a\u5177\u4f53\u7b97\u6cd5\u9700\u8981\u7528\u5230\u7684\u4e00\u4e9b\u5c5e\u6027<\/li>\n<\/ul>\n<h2>\u51e0\u4e2a\u5e38\u7528\u7684\u5206\u7247\u89c4\u5219<\/h2>\n<h3>\u8fde\u7eed\u5206\u7247<\/h3>\n<h4>\u65e5\u671f\u5217\u5206\u533a\u6cd5<\/h4>\n<pre><code class=\"language-xml\">&lt;!--\u6309\u56fa\u5b9a\u65f6\u95f4\u5206\u7247--&gt;\n&lt;tableRule name=&quot;sharding-by-date&quot;&gt;\n    &lt;rule&gt;\n        &lt;columns&gt;create_time&lt;\/columns&gt;\n        &lt;algorithm&gt;sharding-by-date&lt;\/algorithm&gt;\n    &lt;\/rule&gt;\n&lt;\/tableRule&gt;\n&lt;function name=&quot;sharding-by-date&quot;\n          class=&quot;io.mycat.route.function..PartitionByDate&quot;&gt;\n    &lt;property name=&quot;dateFormat&quot;&gt;yyyy-MM-dd&lt;\/property&gt;\n    &lt;property name=&quot;sBeginDate&quot;&gt;2014-01-01&lt;\/property&gt;\n    &lt;property name=&quot;sPartionDay&quot;&gt;10&lt;\/property&gt;\n&lt;\/function&gt;<\/code><\/pre>\n<pre><code class=\"language-xml\">&lt;!--\u6309\u81ea\u7136\u6708\u5206\u7247--&gt;\n&lt;tableRule name=&quot;sharding-by-month&quot;&gt;\n    &lt;rule&gt;\n        &lt;columns&gt;create_time&lt;\/columns&gt;\n        &lt;algorithm&gt;sharding-by-month&lt;\/algorithm&gt;\n    &lt;\/rule&gt;\n&lt;\/tableRule&gt;\n&lt;function name=&quot;sharding-by-month&quot;\n          class=&quot;io.mycat.route.function..PartitionByMonth&quot;&gt;\n    &lt;property name=&quot;dateFormat&quot;&gt;yyyy-MM-dd&lt;\/property&gt;\n    &lt;property name=&quot;sBeginDate&quot;&gt;2014-01-01&lt;\/property&gt;\n&lt;\/function&gt;<\/code><\/pre>\n<pre><code class=\"language-xml\">&lt;!--\n\u6309\u5355\u6708\u5c0f\u65f6\u5206\u7247\n\u9002\u5408\u505a\u65e5\u5fd7\uff0c\u6bcf\u6708\u672b\uff0c\u624b\u5de5\u6e05\u7406\n--&gt;\n&lt;tableRule name=&quot;sharding-by-hour&quot;&gt;\n&lt;rule&gt;\n    &lt;columns&gt;create_time&lt;\/columns&gt;\n    &lt;algorithm&gt;sharding-by-hour&lt;\/algorithm&gt;\n&lt;\/rule&gt;\n&lt;\/tableRule&gt;\n&lt;function name=&quot;sharding-by-hour&quot;\n          class=&quot;io.mycat.route.function..LastestMonthPartition&quot;&gt;\n    &lt;property name=&quot;splitOneDay&quot;&gt;24&lt;\/property&gt;\n&lt;\/function&gt;<\/code><\/pre>\n<p>\u914d\u7f6e\u8bf4\u660e<\/p>\n<p>\uff081\uff09<code>tableRule<\/code>\u6807\u7b7e<\/p>\n<ul>\n<li><code>columns<\/code>\uff1a\u6807\u8bc6\u5c06\u8981\u5206\u7247\u7684\u8868\u5b57\u6bb5<\/li>\n<li><code>algorithm<\/code>\uff1a\u6307\u5b9a\u5206\u7247\u51fd\u6570<\/li>\n<\/ul>\n<p>\uff082\uff09<code>function<\/code>\u6807\u7b7e<\/p>\n<ul>\n<li><code>dateFormat<\/code>\uff1a\u65e5\u671f\u683c\u5f0f<\/li>\n<li><code>sBeginDate<\/code>\uff1a\u5f00\u59cb\u65e5\u671f<\/li>\n<li><code>sPartionDay<\/code>\uff1a\u5206\u533a\u5929\u6570\uff0c\u5373\u9ed8\u8ba4\u4ece\u5f00\u59cb\u65e5\u671f\u7b97\u8d77\uff0c\u5206\u969410\u5929\u4e00\u4e2a\u5206\u533a<\/li>\n<\/ul>\n<h3>\u8303\u56f4\u7ea6\u5b9a<\/h3>\n<h4>ID\u8303\u56f4\u5206\u533a\u6cd5<\/h4>\n<pre><code class=\"language-xml\">&lt;tableRule name=&quot;auto-sharding-long&quot;&gt;\n    &lt;rule&gt;\n        &lt;columns&gt;user_id&lt;\/columns&gt;\n        &lt;algorithm&gt;rang-long&lt;\/algorithm&gt;\n    &lt;\/rule&gt;\n&lt;\/tableRule&gt;\n&lt;function name=&quot;rang-long&quot;\n          class=&quot;io.mycat.route.function.AutoPartitionByLong&quot;&gt;\n    &lt;property name=&quot;mapFile&quot;&gt;autopartition-long.txt&lt;\/property&gt;\n&lt;\/function&gt;<\/code><\/pre>\n<p>\u914d\u7f6e\u8bf4\u660e<\/p>\n<p>\uff081\uff09<code>tableRule<\/code>\u6807\u7b7e<\/p>\n<p><code>columns<\/code>\uff1a\u6807\u8bc6\u5c06\u8981\u5206\u7247\u7684\u8868\u5b57\u6bb5<br \/>\n<code>algorithm<\/code>\uff1a\u6307\u5b9a\u5206\u7247\u51fd\u6570 <\/p>\n<p>\uff082\uff09<code>function<\/code>\u6807\u7b7e<\/p>\n<p><code>mapFile<\/code>\uff1a\u6307\u5b9a\u5206\u7247\u51fd\u6570\u9700\u8981\u7684\u914d\u7f6e\u6587\u4ef6\u540d\u79f0<br \/>\n<code>autopartition-long.txt<\/code>\u6587\u4ef6\u5185\u5bb9\uff1a\u6240\u6709\u7684\u8282\u70b9\u914d\u7f6e\u90fd\u662f\u4ece0\u5f00\u59cb\uff0c\u53730\u4ee3\u8868\u8282\u70b91\uff0c\u6b64\u914d\u7f6e\u975e\u5e38\u7b80\u5355\uff0c\u5373\u9884\u5148\u5236\u5b9a\u53ef\u80fd\u7684id\u8303\u56f4\u5bf9\u5e94\u67d0\u4e2a\u5206\u7247<\/p>\n<pre><code># range start-end, data node index\n# K=1000,M=10000.\n0-500M=0 0-100 0\n500M-1000M=1 101-200 1\n201-300 2\n1000M-1500M=2\ndefault=0\n# \u6216\u4ee5\u4e0b\u5199\u6cd5\n# 0-10000000=0\n# 10000001-20000000=1<\/code><\/pre>\n<blockquote>\n<p>\u4f18\u52bf\uff1a\u6269\u5bb9\u65e0\u9700\u8fc1\u79fb\u6570\u636e<br \/>\n\u7f3a\u70b9\uff1a\u70ed\u70b9\u6570\u636e\uff0c\u5e76\u53d1\u53d7\u9650<\/p>\n<\/blockquote>\n<h3>\u79bb\u6563\u5206\u7247<\/h3>\n<h4>\u679a\u4e3e\u6cd5<\/h4>\n<pre><code class=\"language-xml\">&lt;tableRule name=&quot;sharding-by-intfile&quot;&gt;\n    &lt;rule&gt;\n        &lt;columns&gt;user_id&lt;\/columns&gt;\n        &lt;algorithm&gt;hash-int&lt;\/algorithm&gt;\n    &lt;\/rule&gt;\n&lt;\/tableRule&gt;\n&lt;function name=&quot;hash-int&quot;\n          class=&quot;io.mycat.route.function.PartitionByFileMap&quot;&gt;\n    &lt;property name=&quot;mapFile&quot;&gt;partition-hash-int.txt&lt;\/property&gt;\n    &lt;property name=&quot;type&quot;&gt;0&lt;\/property&gt;\n    &lt;property name=&quot;defaultNode&quot;&gt;0&lt;\/property&gt;\n&lt;\/function&gt;<\/code><\/pre>\n<p>\u914d\u7f6e\u8bf4\u660e<\/p>\n<p>\uff081\uff09<code>tableRule<\/code>\u6807\u7b7e<\/p>\n<ul>\n<li><code>columns<\/code>\uff1a\u6807\u8bc6\u5c06\u8981\u5206\u7247\u7684\u8868\u5b57\u6bb5<\/li>\n<li><code>algorithm<\/code>\uff1a\u6307\u5b9a\u5206\u7247\u51fd\u6570<\/li>\n<\/ul>\n<p>\uff082\uff09function\u6807\u7b7e<\/p>\n<ul>\n<li><code>mapFile<\/code>\uff1a\u6307\u5b9a\u5206\u7247\u51fd\u6570\u9700\u8981\u7684\u914d\u7f6e\u6587\u4ef6\u540d\u79f0<\/li>\n<li><code>type<\/code>\uff1a\u9ed8\u8ba4\u503c\u4e3a0\uff0c0\u8868\u793aInteger\uff0c\u975e\u96f6\u8868\u793aString<\/li>\n<li><code>defaultNode<\/code>\uff1a\u6307\u5b9a\u9ed8\u8ba4\u8282\u70b9\uff0c\u5c0f\u4e8e0\u8868\u793a\u4e0d\u8bbe\u7f6e\u9ed8\u8ba4\u8282\u70b9\uff0c\u5927\u4e8e\u7b49\u4e8e0\u8868\u793a\u8bbe\u7f6e\u9ed8\u8ba4\u8282\u70b9\uff0c0\u4ee3\u8868\u8282\u70b91<\/li>\n<\/ul>\n<p>\u9ed8\u8ba4\u8282\u70b9\u7684\u4f5c\u7528\uff1a\u679a\u4e3e\u5206\u7247\u65f6\uff0c\u5982\u679c\u78b0\u5230\u4e0d\u8bc6\u522b\u7684\u679a\u4e3e\u503c\uff0c\u5c31\u8ba9\u5b83\u8def\u7531\u5230\u9ed8\u8ba4\u8282\u70b9\u3002\u5982\u679c\u4e0d\u914d\u7f6e\u9ed8\u8ba4\u8282\u70b9\uff08defaultNode\u503c\u5c0f\u4e8e0\u8868\u793a\u4e0d\u914d\u7f6e\u9ed8\u8ba4\u8282\u70b9\uff09\uff0c\u78b0\u5230\u4e0d\u8bc6\u522b\u7684\u679a\u4e3e\u503c \u5c31\u4f1a\u62a5\u9519<\/p>\n<p><code>partition-hash-int.txt<\/code>\u914d\u7f6e\uff1a<\/p>\n<pre><code>10000=0 \u5217\u7b49\u4e8e10000 \u653e\u7b2c\u4e00\u4e2a\u5206\u7247\n10010=1\n\u7537=0\n\u5973=1\nbeijing=0\ntianjin=1\nzhanghai=2<\/code><\/pre>\n<h4>\u6c42\u6a21\u6cd5<\/h4>\n<pre><code class=\"language-xml\">&lt;tableRule name=&quot;mod-long&quot;&gt;\n&lt;rule&gt;\n    &lt;columns&gt;user_id&lt;\/columns&gt;\n    &lt;algorithm&gt;mod-long&lt;\/algorithm&gt;\n&lt;\/rule&gt;\n&lt;\/tableRule&gt;\n&lt;function name=&quot;mod-long&quot;\n          class=&quot;io.mycat.route.function.PartitionByMod&quot;&gt;\n    &lt;!-- how many data nodes --&gt;\n    &lt;property name=&quot;count&quot;&gt;3&lt;\/property&gt;\n&lt;\/function&gt;<\/code><\/pre>\n<p>\u914d\u7f6e\u8bf4\u660e<\/p>\n<p>\uff081\uff09<code>tableRule<\/code>\u6807\u7b7e<\/p>\n<ul>\n<li><code>columns<\/code>\uff1a\u6807\u8bc6\u5c06\u8981\u5206\u7247\u7684\u8868\u5b57\u6bb5<\/li>\n<li><code>algorithm<\/code>\uff1a\u6307\u5b9a\u5206\u7247\u51fd\u6570<\/li>\n<\/ul>\n<p>\uff082\uff09<code>function<\/code>\u6807\u7b7e<\/p>\n<ul>\n<li><code>count<\/code>\uff1a\u8282\u70b9\u6570\u91cf<\/li>\n<\/ul>\n<h4>\u4e00\u81f4\u6027hash<\/h4>\n<pre><code class=\"language-xml\">&lt;tableRule name=&quot;sharding-by-murmur&quot;&gt;\n    &lt;rule&gt;\n        &lt;columns&gt;user_id&lt;\/columns&gt;\n        &lt;algorithm&gt;murmur&lt;\/algorithm&gt;\n    &lt;\/rule&gt;\n&lt;\/tableRule&gt;\n&lt;function name=&quot;murmur&quot;\n          class=&quot;io.mycat.route.function.PartitionByMurmurHash&quot;&gt;\n    &lt;!-- \u9ed8\u8ba4\u662f0 --&gt;\n    &lt;property name=&quot;seed&quot;&gt;0&lt;\/property&gt;\n    &lt;!-- \u8981\u5206\u7247\u7684\u6570\u636e\u5e93\u8282\u70b9\u6570\u91cf\uff0c\u5fc5\u987b\u6307\u5b9a\uff0c\u5426\u5219\u6ca1\u6cd5\u5206\u7247 --&gt;\n    &lt;property name=&quot;count&quot;&gt;2&lt;\/property&gt;\n    &lt;!-- \u4e00\u4e2a\u5b9e\u9645\u7684\u6570\u636e\u5e93\u8282\u70b9\u88ab\u6620\u5c04\u4e3a\u8fd9\u4e48\u591a\u865a\u62df\u8282\u70b9\uff0c\u9ed8\u8ba4\u662f160\u500d\uff0c\u4e5f\u5c31\u662f\u865a\u62df\u8282\u70b9\u6570\u662f\u7269\u7406\u8282\u70b9\u6570\u7684160\u500d --&gt;\n    &lt;property name=&quot;virtualBucketTimes&quot;&gt;160&lt;\/property&gt;\n    &lt;!-- \u8282\u70b9\u7684\u6743\u91cd\uff0c\u6ca1\u6709\u6307\u5b9a\u6743\u91cd\u7684\u8282\u70b9\u9ed8\u8ba4\u662f1 --&gt;\n    &lt;!-- \u4ee5properties\u6587\u4ef6\u7684\u683c\u5f0f\u586b\u5199\uff0c\u4ee5\u4ece0\u5f00\u59cb\u5230count-1\u7684\u6574\u6570\u503c\u4e5f\u5c31\u662f\u8282\u70b9\u7d22\u5f15\u4e3akey\uff0c\u4ee5\u8282\u70b9\u6743\u91cd\u503c\u4e3a\u503c --&gt;\n    &lt;!-- \u6240\u6709\u6743\u91cd\u503c\u5fc5\u987b\u662f\u6b63\u6574\u6570\uff0c\u5426\u5219\u4ee51\u4ee3\u66ff --&gt;\n    &lt;!-- &lt;property name=&quot;weightMapFile&quot;&gt;weightMapFile&lt;\/property&gt; --&gt;\n    &lt;!-- \u7528\u4e8e\u6d4b\u8bd5\u65f6\u89c2\u5bdf\u5404\u7269\u7406\u8282\u70b9\u4e0e\u865a\u62df\u8282\u70b9\u7684\u5206\u5e03\u60c5\u51b5 --&gt;\n    &lt;!-- \u5982\u679c\u6307\u5b9a\u4e86\u8fd9\u4e2a\u5c5e\u6027\uff0c\u4f1a\u628a\u865a\u62df\u8282\u70b9\u7684murmur\uff0chash\u503c\u4e0e\u7269\u7406\u8282\u70b9\u7684\u6620\u5c04\u6309\u884c\u8f93\u51fa\u5230\u8fd9\u4e2a\u6587\u4ef6\uff0c\u6ca1\u6709\u9ed8\u8ba4\u503c --&gt;\n    &lt;!-- \u5982\u679c\u4e0d\u6307\u5b9a\uff0c\u5c31\u4e0d\u4f1a\u8f93\u51fa\u4efb\u4f55\u4e1c\u897f --&gt;\n    &lt;!-- &lt;property name=&quot;bucketMapPath&quot;&gt;\/etc\/mycat\/bucketMapPath&lt;\/property&gt; --&gt;\n&lt;\/function&gt;<\/code><\/pre>\n<h2>\u6d4b\u8bd5\u5206\u7247<\/h2>\n<p>\u628a\u5546\u54c1\u8868\u5206\u7247\u5b58\u50a8\u5230\u4e09\u4e2a\u6570\u636e\u8282\u70b9\u4e0a\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Java\/MyCat\u5206\u7247\u6d4b\u8bd5.png\" alt=\"MyCat\u5206\u7247\u6d4b\u8bd5\" \/><\/p>\n<h3>\u521b\u5efa\u8868<\/h3>\n<p>\u914d\u7f6e\u5b8c\u6bd5\u540e\uff0c\u91cd\u65b0\u542f\u52a8MyCat\u3002\u4f7f\u7528MySQL\u5ba2\u6237\u7aef\u8fde\u63a5MyCat\uff0c\u521b\u5efa\u8868\u3002<\/p>\n<pre><code class=\"language-sql\">CREATE TABLE item (\nid int(11) NOT NULL,\nname varchar(20) DEFAULT NULL,\nPRIMARY KEY (id)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8<\/code><\/pre>\n<h3>\u5206\u7247\u6d4b\u8bd5<\/h3>\n<p>\u5206\u7247\u7b56\u7565\u6307\u5b9a\u4e3a<code>auto-sharding-long<\/code>\uff0c\u5206\u7247\u89c4\u5219\u6307\u5b9a\u4e3a<code>mod-long<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>MyCat\u4ecb\u7ecd \u5b98\u65b9\u7f51\u7ad9\uff1a\uff1ahttp:\/\/www.mycat.org.cn\/ MyCat\u6838\u5fc3\u6982\u5ff5 Schema [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[297],"class_list":["post-1787","post","type-post","status-publish","format-standard","hentry","category-mysql","tag-mycat"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1787","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1787"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1787\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1787"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1787"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1787"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}