先点赞,后观看,伸手才有好习惯
理解:Mycat做为一个逻辑数据库,是须要依赖下面的真实数据库vue
坑点及建议:
一、由于关注点在主从,别花太多时间在一台单机安装mysql两个端口,可尝试虚拟机两台
二、mysql5.7跟以前的版本不同,windows上面测试的朋友my.ini能够本身新建
三、对于my.ini的各个参数的解释请随机去百度,这里有个人转载node
实操
假设如今两台数据库的mysql均已正常安装,配置以下mysql
服务器 | IP | 帐号 | 密码 |
---|---|---|---|
主 | A | AUSER | APWD |
从 | B | BUSER | BPWD |
通常进my.ini只须要更新如下所给信息(有则改之无则加):web
[mysqld]
#binlog格式,分三种:statement level,rowlevel,mixed
#三种模式的差异介绍能够看:https://juejin.im/post/5df32b2a6fb9a01628012fb3
binlog_format=mixed
#为服务器标识,主从必定不要同样
server-id = 1
#清理二进制日志的时间间隔
expire_logs_days = 10
#是须要同步的数据库
binlog-do-db
#不须要同步的数据库
binlog-ignore-db = mysql
binlog-ignore-db = test
#设置gtid同步方式
gtid_executed_compression_period = 1000 #1000默认
gtid_mode = on #默认off
enforce_gtid_consistency = on #默认off
复制代码
一、链接主库,并进入mysqlsql
>mysql -u数据库用户名 -p数据库密码
复制代码
二、为从库建立受权用户slave,密码slave ,B为对应的IPmongodb
grant replication slave on *.* to 'slave'@'B' identified by 'slave' ;
复制代码
三、刷新权限信息数据库
flush privileges;
复制代码
四、自行建立数据库和数据表并插入相应数据,mysql默认InnoDB,主库能够不用修改引擎express
my.ini文件apache
[mysqld]
#binlog格式,分三种:statement level,rowlevel,mixed
#三种模式的差异介绍能够看:
binlog_format=mixed
#为服务器标识,主从必定不要同样
server-id = 13
#清理二进制日志的时间间隔
expire_logs_days = 10
#是须要同步的数据库
binlog-do-db
#设置gtid同步方式
gtid_executed_compression_period = 1000 #1000默认
gtid_mode = on #默认off
enforce_gtid_consistency = on #默认off
复制代码
一、链接从库,并进入mysqlwindows
>mysql -u数据库用户名 -p数据库密码
复制代码
二、复制一份主库的数据库到从库,并赋予slave权限
grant all privileges on *.* to 'slave'@'%' identified by 'slave' with grant option;
复制代码
三、刷新权限或者退出mysql命令行重启mysql服务
四、master创建数据同步
change master to master_host='A',master_user='slave',master_password='slave',master_auto_position=1;
复制代码
重要说明:部分教程用的master_log_file & master_log_pos 参数来指定,可是slave一旦出现问题,没法确认断节点,数据容易形成不一致,因此才引入gtid(即global transaction ID全局事务ID),想看相应介绍的能够去mysql官网里面搜寻相应的版本而后看看.
五、mysql命令查看从库数据库状态
mysql>show slave status \G;
复制代码
当显示的数据内有:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
就说明能够了
六、自行验证主库更新数据对从库的影响
简介个毛线,[MyCat官网](http://mycat.io/)那么详细的介绍不看,非要听我在这摘录?
复制代码
环境安装
因为Mycat是基于Java开发的,因此JDK环境先安装好,再去安装Mycat,安装教程一大把,我就不赘述了
实现过程
一、为简单明了的看清配置信息,会删除不影响结果的注释,且本记录只针对一个主库和一个从库,分库分表只须要加对应的配置便可,配置文件中有相应的详细说明
二、能够进入mysql把从库的引擎改为MyISAM,若是不想从库有写的功能,也能够将mysql设置成只读数据库
schema.xml 配置信息(完整的配置信息及解释在文末):
主要是配置读写数据库信息和对应表信息
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<!-- 每个schema表明一个逻辑数据库
这里的name是程序端使用的数据库名称,对应的数据库帐号密码在server.xml中
-->
<schema name="MycatDemo" checkSQLschema="true" sqlMaxLimit="100">
<!-- auto sharding by id (long)
每一个逻辑数据库下面对应的是全部的表,下方RDB表明真实数据库
name:对应RDB中的表名
primaryKey:RDB中该表的主键
dataNode:这些表对应的数据库
rule:对应的分片规则
-->
<table name="article" primaryKey="article_id" autoIncrement="true" dataNode="dn1"
rule="mod-long" />
</schema>
<!-- 各个数据结点的信息,便于上方schema使用
若是是多个数据结点和多个host,那就同步复制一份dataNode和dataHost数据,而后写上对应的配置信息
name:结点的名称
dataHost:结点的主机地址
database:RDB数据库名称
-->
<dataNode name="dn1" dataHost="localhost1" database="demo" />
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<!-- 心跳语句检测,检测对应的mysql是否正常运行 -->
<heartbeat>select user()</heartbeat>
<!-- can have multi write hosts -->
<writeHost host="hostM1" url="11.11.11.11:3306" user="root"
password="root">
<!-- can have multi read hosts -->
<readHost host="hostS2" url="22.22.22.22:3306" user="root" password="root" />
</writeHost>
<!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->
</dataHost>
</mycat:schema>
复制代码
rule.xml 配置信息(完整的配置信息及解释在文末): 主要定义一些分片规则和生成规则之类的
这里尤为要注意一点,有个叫mod-long的function在schema.xml中引用到了,可是我只作了一分数据库,因此这里我把默认的3改为了1,若是你有对应的多个数据库分表,则改为相应的数量。
<?xml version="1.0" encoding="UTF-8"?>
<!-- - - Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License. - You
may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0
- - Unless required by applicable law or agreed to in writing, software -
distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the
License for the specific language governing permissions and - limitations
under the License. -->
<!DOCTYPE mycat:rule SYSTEM "rule.dtd">
<mycat:rule xmlns:mycat="http://io.mycat/">
<tableRule name="rule1">
<rule>
<columns>id</columns>
<algorithm>func1</algorithm>
</rule>
</tableRule>
<tableRule name="rule2">
<rule>
<columns>user_id</columns>
<algorithm>func1</algorithm>
</rule>
</tableRule>
<tableRule name="sharding-by-intfile">
<rule>
<columns>sharding_id</columns>
<algorithm>hash-int</algorithm>
</rule>
</tableRule>
<tableRule name="auto-sharding-long">
<rule>
<columns>id</columns>
<algorithm>rang-long</algorithm>
</rule>
</tableRule>
<tableRule name="mod-long">
<rule>
<columns>article_id</columns>
<algorithm>mod-long</algorithm>
</rule>
</tableRule>
<tableRule name="sharding-by-murmur">
<rule>
<columns>id</columns>
<algorithm>murmur</algorithm>
</rule>
</tableRule>
<tableRule name="crc32slot">
<rule>
<columns>id</columns>
<algorithm>crc32slot</algorithm>
</rule>
</tableRule>
<tableRule name="sharding-by-month">
<rule>
<columns>create_time</columns>
<algorithm>partbymonth</algorithm>
</rule>
</tableRule>
<tableRule name="latest-month-calldate">
<rule>
<columns>calldate</columns>
<algorithm>latestMonth</algorithm>
</rule>
</tableRule>
<tableRule name="auto-sharding-rang-mod">
<rule>
<columns>id</columns>
<algorithm>rang-mod</algorithm>
</rule>
</tableRule>
<tableRule name="jch">
<rule>
<columns>id</columns>
<algorithm>jump-consistent-hash</algorithm>
</rule>
</tableRule>
<function name="murmur"
class="io.mycat.route.function.PartitionByMurmurHash">
<property name="seed">0</property><!-- 默认是0 -->
<property name="count">1</property><!-- 要分片的数据库节点数量,必须指定,不然无法分片 -->
<property name="virtualBucketTimes">160</property><!-- 一个实际的数据库节点被映射为这么多虚拟节点,默认是160倍,也就是虚拟节点数是物理节点数的160倍 -->
<!-- <property name="weightMapFile">weightMapFile</property> 节点的权重,没有指定权重的节点默认是1。以properties文件的格式填写,以从0开始到count-1的整数值也就是节点索引为key,以节点权重值为值。全部权重值必须是正整数,不然以1代替 -->
<!-- <property name="bucketMapPath">/etc/mycat/bucketMapPath</property>
用于测试时观察各物理节点与虚拟节点的分布状况,若是指定了这个属性,会把虚拟节点的murmur hash值与物理节点的映射按行输出到这个文件,没有默认值,若是不指定,就不会输出任何东西 -->
</function>
<function name="crc32slot"
class="io.mycat.route.function.PartitionByCRC32PreSlot">
<property name="count">2</property><!-- 要分片的数据库节点数量,必须指定,不然无法分片 -->
</function>
<function name="hash-int"
class="io.mycat.route.function.PartitionByFileMap">
<property name="mapFile">partition-hash-int.txt</property>
</function>
<function name="rang-long"
class="io.mycat.route.function.AutoPartitionByLong">
<property name="mapFile">autopartition-long.txt</property>
</function>
<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
<!-- how many data nodes -->
<property name="count">1</property><!-- 只有一个数据库我改为了1,默认3 -->
</function>
<function name="func1" class="io.mycat.route.function.PartitionByLong">
<property name="partitionCount">8</property>
<property name="partitionLength">128</property>
</function>
<function name="latestMonth"
class="io.mycat.route.function.LatestMonthPartion">
<property name="splitOneDay">24</property>
</function>
<function name="partbymonth"
class="io.mycat.route.function.PartitionByMonth">
<property name="dateFormat">yyyy-MM-dd</property>
<property name="sBeginDate">2015-01-01</property>
</function>
<function name="rang-mod" class="io.mycat.route.function.PartitionByRangeMod">
<property name="mapFile">partition-range-mod.txt</property>
</function>
<function name="jump-consistent-hash" class="io.mycat.route.function.PartitionByJumpConsistentHash">
<property name="totalBuckets">3</property>
</function>
</mycat:rule>
复制代码
server.xml 配置信息(完整的配置信息及解释在文末):
主要定义sql服务相关的参数,例如帐号密码,sql事务及最大文本
<?xml version="1.0" encoding="UTF-8"?>
<!-- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License. - You
may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0
- - Unless required by applicable law or agreed to in writing, software -
distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the
License for the specific language governing permissions and - limitations
under the License. -->
<!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">2</property>
<!-- <property name="useCompression">1</property> --> <!--1为开启mysql压缩协议-->
<!-- <property name="fakeMySQLVersion">5.6.20</property> --> <!--设置模拟的MySQL版本号-->
<!-- <property name="processorBufferChunk">40960</property> -->
<!--
<property name="processors">1</property>
<property name="processorExecutor">32</property>
-->
<!--默认为type 0: DirectByteBufferPool | type 1 ByteBufferArena-->
<property name="processorBufferPoolType">0</property>
<!-- 默认是65535 64K 用于sql解析时最大文本长度 -->
<property name="maxStringLiteralLength">65535</property>
<property name="sequnceHandlerType">0</property>
<property name="backSocketNoDelay">1</property>
<property name="frontSocketNoDelay">1</property>
<property name="processorExecutor">16</property>
<property name="serverPort">8066</property> <property name="managerPort">9066</property>
<property name="idleTimeout">300000</property> <property name="bindIp">0.0.0.0</property>
<property name="frontWriteQueueSize">4096</property> <property name="processors">32</property>
<!--分布式事务开关,0为不过滤分布式事务,1为过滤分布式事务(若是分布式事务内只涉及全局表,则不过滤),2为不过滤分布式事务,可是记录分布式事务日志-->
<property name="handleDistributedTransactions">0</property>
<!--
off heap for merge/order/group/limit 1开启 0关闭
-->
<property name="useOffHeapForMerge">1</property>
<!--
单位为m
-->
<property name="memoryPageSize">1m</property>
<!--
单位为k
-->
<property name="spillsFileBufferSize">1k</property>
<property name="useStreamOutput">0</property>
<!--
单位为m
-->
<property name="systemReserveMemorySize">384m</property>
<!--是否采用zookeeper协调切换 -->
<property name="useZKSwitch">true</property>
</system>
<!-- 全局SQL防火墙设置 -->
<!--
<firewall>
<whitehost>
<host host="127.0.0.1" user="mycat"/>
<host host="127.0.0.2" user="mycat"/>
</whitehost>
<blacklist check="false">
</blacklist>
</firewall>
-->
<!-- 至关于Mycat逻辑数据库的管理员帐号 数据库名跟schema.xml中定义的保持一致-->
<user name="mycatuser">
<property name="password">mycatpwd</property>
<property name="schemas">MycatDemo</property>
<property name="defaultAccount">true</property>
<!-- 表级 DML 权限设置 -->
<!--
<privileges check="false">
<schema name="TESTDB" dml="0110" >
<table name="tb01" dml="0000"></table>
<table name="tb02" dml="1111"></table>
</schema>
</privileges>
-->
</user>
<!-- 至关于Mycat逻辑数据库的只读帐号 数据库名跟schema.xml中定义的保持一致-->
<user name="user">
<property name="password">user</property>
<property name="schemas">MycatDemo</property>
<property name="readOnly">true</property>
</user>
</mycat:server>
复制代码
程序中链接数据库的时候就再也不是单纯的mysql的链接和帐号密码了,而是上面定义的:
数据库名:MycatDemo(schema.xml中)
帐号:mycatuser(server.xml中)
密码:mycatpwd(server.xml中)
验证读写分离
进入mysql命令行,开启主从两个数据库的sql日志,经过sql日志执行的状况就能判断读写使用的哪一个库
#查看日期状况
mysql>show variables like '%general%';
复制代码
#开启日志
mysql>SET GLOBAL general_log = 'On';
复制代码
#指定日志文件
mysql>SET GLOBAL general_log_file = '/var/lib/mysql/mysql.log';
复制代码
Mycat性能监控
mycat有性能监控的WEB管理端软件,有须要的自行下载安装
server.xml文件说明
<?xml version="1.0" encoding="UTF-8"?>
<!-- - - Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License. - You
may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0
- - Unless required by applicable law or agreed to in writing, software -
distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the
License for the specific language governing permissions and - limitations
under the License. -->
<!DOCTYPE mycat:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://io.mycat/">
<system>
<property name="nonePasswordLogin">0</property> <!-- 0为须要密码登录、1为不须要密码登录 ,默认为0,设置为1则须要指定默认帐户-->
<property name="useHandshakeV10">1</property>
<property name="useSqlStat">0</property> <!-- 1为开启实时统计、0为关闭 -->
<property name="useGlobleTableCheck">0</property> <!-- 1为开启全加班一致性检测、0为关闭 -->
<property name="sequnceHandlerType">2</property> <!--0 本地文件方式 1 数据库方式 2 时间戳方式-->
<property name="subqueryRelationshipCheck">false</property> <!-- 子查询中存在关联查询的状况下,检查关联字段中是否有分片字段 .默认 false -->
<!-- <property name="useCompression">1</property>--> <!--1为开启mysql压缩协议-->
<!-- <property name="fakeMySQLVersion">5.6.20</property>--> <!--设置模拟的MySQL版本号-->
<!-- <property name="processorBufferChunk">40960</property> -->
<!--
<property name="processors">1</property>
<property name="processorExecutor">32</property>
-->
<!--默认为type 0: DirectByteBufferPool | type 1 ByteBufferArena | type 2 NettyBufferPool -->
<property name="processorBufferPoolType">0</property>
<!--默认是65535 64K 用于sql解析时最大文本长度 -->
<!--<property name="maxStringLiteralLength">65535</property>-->
<!--<property name="sequnceHandlerType">0</property>-->
<!--<property name="backSocketNoDelay">1</property>-->
<!--<property name="frontSocketNoDelay">1</property>-->
<!--<property name="processorExecutor">16</property>-->
<!--
<property name="serverPort">8066</property> <property name="managerPort">9066</property>
<property name="idleTimeout">300000</property> <property name="bindIp">0.0.0.0</property>
<property name="frontWriteQueueSize">4096</property> <property name="processors">32</property> -->
<!--分布式事务开关,0为不过滤分布式事务,1为过滤分布式事务(若是分布式事务内只涉及全局表,则不过滤),2为不过滤分布式事务,可是记录分布式事务日志-->
<property name="handleDistributedTransactions">0</property>
<!--
off heap for merge/order/group/limit 1开启 0关闭
-->
<property name="useOffHeapForMerge">1</property>
<!--
单位为m
-->
<property name="memoryPageSize">64k</property>
<!--
单位为k
-->
<property name="spillsFileBufferSize">1k</property>
<property name="useStreamOutput">0</property>
<!--
单位为m
-->
<property name="systemReserveMemorySize">384m</property>
<!--是否采用zookeeper协调切换 -->
<property name="useZKSwitch">false</property>
<!-- XA Recovery Log日志路径 -->
<!--<property name="XARecoveryLogBaseDir">./</property>-->
<!-- XA Recovery Log日志名称 -->
<!--<property name="XARecoveryLogBaseName">tmlog</property>-->
<!--若是为 true的话 严格遵照隔离级别,不会在仅仅只有select语句的时候在事务中切换链接-->
<property name="strictTxIsolation">false</property>
<property name="useZKSwitch">true</property>
</system>
<!-- 全局SQL防火墙设置 -->
<!--白名单可使用通配符%或着*-->
<!--例如<host host="127.0.0.*" user="root"/>-->
<!--例如<host host="127.0.*" user="root"/>-->
<!--例如<host host="127.*" user="root"/>-->
<!--例如<host host="1*7.*" user="root"/>-->
<!--这些配置状况下对于127.0.0.1都能以root帐户登陆-->
<!--
<firewall>
<whitehost>
<host host="1*7.0.0.*" user="root"/>
</whitehost>
<blacklist check="false">
</blacklist>
</firewall>
-->
<!--用户名是mycat-->
<user name="mycat" defaultAccount="true">
<!--密码是123456-->
<property name="password">123456</property>
<!---可访问的逻辑库有mycatdb-->
<property name="schemas">mycatdb</property>
<!-- 表级 DML 权限设置 -->
<!--
<privileges check="false">
<schema name="TESTDB" dml="0110" >
<table name="tb01" dml="0000"></table>
<table name="tb02" dml="1111"></table>
</schema>
</privileges>
-->
</user>
<user name="user">
<property name="password">user</property>
<property name="schemas">mycatdb</property>
<!--是否只读-->
<property name="readOnly">true</property>
</user>
</mycat:server>
复制代码
schema.xml文件说明
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<!-- checkSQLschema这个属性默认就是false,官方文档的意思就是是否去掉表前面的数据库的名称,
”select * from db1.testtable” ,设置为true就会去掉db1。可是若是db1的名称不是schema的名称,那么也不会被去掉,
所以官方建议不要使用这种语法。同时默认设置为false。-->
<!-- sqlMaxLimit当该值设置为某个数值时。每条执行的 SQL 语句,若是没有加上 limit 语句,MyCat 也会自动的加上所对应的值。
例如设置值为 100,执行”select * from test_table”,则效果为“selelct * from test_table limit 100”.
注意:若是运行的 schema 为非拆分库的,那么该属性不会生效。-->
<schema name="mycatdb" checkSQLschema="false" sqlMaxLimit="100">
<!-- name 该属性定义逻辑表的表名 -->
<!-- dataNode 该属性定义这个逻辑表所属的 dataNode, 该属性的值须要和 dataNode 标签中 name 属性的值相互对应。 -->
<!-- rule 该属性用于指定逻辑表要使用的规则名字,规则名字在 rule.xml 中定义,必须与 tableRule 标签中 name 属性属性值一一对应 -->
<!-- ruleRequired 该属性用于指定表是否绑定分片规则,若是配置为 true,但没有配置具体 rule 的话 ,程序会报错。 -->
<!-- primaryKey 该逻辑表对应真实表的主键, -->
<!-- type 该属性定义了逻辑表的类型,目前逻辑表只有“全局表”和”普通表”两种类型。全局表定义type=”global”,不定义的就是普通表。 -->
<!-- autoIncrement 主键是否自增加。 -->
<!-- subTables 分表,分表目前不支持Join。 -->
<!-- needAddLimit是否自动添加limit,默认是开启状态。关闭请谨慎。 -->
<!--rule="mod-long" 指定规则在rule中配置-->
<table name="users" primaryKey="id" dataNode="dn1"/>
<table name="orders" primaryKey="id" dataNode="dn1"/>
<table name="product" primaryKey="id" dataNode="dn2"/>
<table name="news" primaryKey="id" dataNode="dn2"/>
</schema>
<!-- Name 定义数据节点的名字,这个名字须要是惟一的-->
<!-- dataHost 该属性用于定义该分片属于哪一个数据库实例 -->
<!-- Database 该属性用于定义该分片属性哪一个具体数据库实例上的具体库 -->
<dataNode name="dn1" dataHost="localhost1" database="sharding1" />
<dataNode name="dn2" dataHost="localhost2" database="sharding2" />
<!-- name 惟一标识 dataHost 标签,供上层的标签使用-->
<!-- maxCon 指定每一个读写实例链接池的最大链接。 -->
<!-- minCon 指定每一个读写实例链接池的最小链接,初始化链接池的大小。 -->
<!-- balance 负载均衡类型,目前的取值有4 种:
“0”, 不开启读写分离机制,全部读操做都发送到当前可用的 writeHost 上。
“1”,所有的 readHost 与 stand by writeHost(非主非从) 参与 select 语句的负载均衡,简单的说,当双主双从模式(M1->S1,M2->S2,而且 M1 与 M2 互为主备),正常状况下,M2,S1,S2 都参与 select 语句的负载均衡。
”2”,全部读操做都随机的在 writeHost、readhost 上分发。
”3”,全部读请求随机的分发到 wiriterHost 对应的 readhost 执行,writerHost 不负担读压writeType 1. writeType=”0”, 全部写操做发送到配置的第一个 writeHost,第一个挂了切到还生存的第二个writeHost,从新启动后已切换后的为准,切换记录在配置文件中:dnindex.properties .2. writeType=”1”,全部写操做都随机的发送到配置的 writeHost,1.5 之后废弃不推荐。默认0就行了!-->
<!-- dbType 指定后端链接的数据库类型,目前支持二进制的 mysql 协议,还有其余使用 JDBC 链接的数据库。例如:mongodb、oracle、spark 等. -->
<!-- dbDriver 指定链接后端数据库使用的 Driver,目前可选的值有 native 和 JDBC。使用 native 的话,由于这个值执行的是二进制的 mysql 协议,因此可使用 mysql 和 maridb。其余类型的数据库则须要使用 JDBC 驱动来支持。 -->
<!-- switchType “-1” 表示不自动切换; “1” 默认值,自动切换;
“2” 基于 MySQL 主从同步的状态决定是否切换心跳语句为 show slave status;
“3” 基于 MySQL galary cluster 的切换机制(适合集群)(1.4.1)心跳语句为 show status like ‘wsrep%’.
-->
<!--slaveThreshold:指定从节点的最大个数-->
<dataHost name="localhost1"
maxCon="1000"
minCon="10"
balance="0"
writeType="0"
dbType="mysql"
dbDriver="native"
switchType="1"
slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<!--host 用于标识不一样实例,通常 writeHost 咱们使用*M1,readHost 咱们用*S1。-->
<!--url 后端实例链接地址。Native:地址:端口 JDBC:jdbc的url-->
<!--password 后端存储实例须要的密码-->
<!--user 后端存储实例须要的用户名字-->
<!--weight 权重 配置在 readhost 中做为读节点的权重-->
<!--usingDecrypt 是否对密码加密,默认0。具体加密方法看官方文档。-->
<writeHost host="hostM1" url="47.94.158.155:3306" user="root"
password="TJXtjx_19991007">
</writeHost>
</dataHost>
<dataHost name="localhost2"
maxCon="1000"
minCon="10"
balance="0"
writeType="0"
dbType="mysql"
dbDriver="native"
switchType="1"
slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM2" url="47.94.158.155:3306" user="root"
password="TJXtjx_19991007">
</writeHost>
</dataHost>
</mycat:schema>
复制代码
rule.xml文件说明
<?xml version="1.0" encoding="UTF-8"?>
<!-- - - Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License. - You
may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0
- - Unless required by applicable law or agreed to in writing, software -
distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the
License for the specific language governing permissions and - limitations
under the License. -->
<!DOCTYPE mycat:rule SYSTEM "rule.dtd">
<mycat:rule xmlns:mycat="http://io.mycat/">
<tableRule name="rule1">
<rule>
<columns>id</columns>
<algorithm>func1</algorithm>
</rule>
</tableRule>
<tableRule name="rule2">
<rule>
<columns>user_id</columns>
<algorithm>func1</algorithm>
</rule>
</tableRule>
<tableRule name="sharding-by-intfile">
<rule>
<columns>sharding_id</columns>
<algorithm>hash-int</algorithm>
</rule>
</tableRule>
<tableRule name="auto-sharding-long">
<rule>
<columns>id</columns>
<algorithm>rang-long</algorithm>
</rule>
</tableRule>
<tableRule name="mod-long">
<rule>
<columns>id</columns>
<algorithm>mod-long</algorithm>
</rule>
</tableRule>
<tableRule name="sharding-by-murmur">
<rule>
<columns>id</columns>
<algorithm>murmur</algorithm>
</rule>
</tableRule>
<tableRule name="crc32slot">
<rule>
<columns>id</columns>
<algorithm>crc32slot</algorithm>
</rule>
</tableRule>
<tableRule name="sharding-by-month">
<rule>
<columns>create_time</columns>
<algorithm>partbymonth</algorithm>
</rule>
</tableRule>
<tableRule name="latest-month-calldate">
<rule>
<columns>calldate</columns>
<algorithm>latestMonth</algorithm>
</rule>
</tableRule>
<tableRule name="auto-sharding-rang-mod">
<rule>
<columns>id</columns>
<algorithm>rang-mod</algorithm>
</rule>
</tableRule>
<tableRule name="jch">
<rule>
<columns>id</columns>
<algorithm>jump-consistent-hash</algorithm>
</rule>
</tableRule>
<function name="murmur"
class="io.mycat.route.function.PartitionByMurmurHash">
<property name="seed">0</property><!-- 默认是0 -->
<property name="count">2</property><!-- 要分片的数据库节点数量,必须指定,不然无法分片 -->
<property name="virtualBucketTimes">160</property><!-- 一个实际的数据库节点被映射为这么多虚拟节点,默认是160倍,也就是虚拟节点数是物理节点数的160倍 -->
<!-- <property name="weightMapFile">weightMapFile</property> 节点的权重,没有指定权重的节点默认是1。以properties文件的格式填写,以从0开始到count-1的整数值也就是节点索引为key,以节点权重值为值。全部权重值必须是正整数,不然以1代替 -->
<!-- <property name="bucketMapPath">/etc/mycat/bucketMapPath</property>
用于测试时观察各物理节点与虚拟节点的分布状况,若是指定了这个属性,会把虚拟节点的murmur hash值与物理节点的映射按行输出到这个文件,没有默认值,若是不指定,就不会输出任何东西 -->
</function>
<function name="crc32slot"
class="io.mycat.route.function.PartitionByCRC32PreSlot">
</function>
<function name="hash-int"
class="io.mycat.route.function.PartitionByFileMap">
<property name="mapFile">partition-hash-int.txt</property>
</function>
<function name="rang-long"
class="io.mycat.route.function.AutoPartitionByLong">
<property name="mapFile">autopartition-long.txt</property>
</function>
<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
<!-- how many data nodes -->
<property name="count">3</property>
</function>
<function name="func1" class="io.mycat.route.function.PartitionByLong">
<property name="partitionCount">8</property>
<property name="partitionLength">128</property>
</function>
<function name="latestMonth"
class="io.mycat.route.function.LatestMonthPartion">
<property name="splitOneDay">24</property>
</function>
<function name="partbymonth"
class="io.mycat.route.function.PartitionByMonth">
<property name="dateFormat">yyyy-MM-dd</property>
<property name="sBeginDate">2015-01-01</property>
</function>
<function name="rang-mod" class="io.mycat.route.function.PartitionByRangeMod">
<property name="mapFile">partition-range-mod.txt</property>
</function>
<function name="jump-consistent-hash" class="io.mycat.route.function.PartitionByJumpConsistentHash">
<property name="totalBuckets">3</property>
</function>
</mycat:rule>
复制代码