「这是我参与11月更文挑战的第22天,活动详情查看:2021最后一次更文挑战」
MyCat的下载和安装
安装环境
- jdk:要求jdk必须是1.7及以上版本
- MySQL:推荐mysql5.5版本以上
- MyCat:Mycat的官方网站:www.mycat.org.cn/
第一步:搭建3台虚拟机
第二步:server01与server02安装MySQL数据库服务器,保证版本一致
第三步:创建数据库
MyCat安装
注意: 提前安装好JDK
- 第一步:下载MyCat
- 第二步:上传MyCat到server03服务器,并解压
启动命令:./mycat start
停止命令:./mycat stop
重启命令:./mycat restart
查看状态:./mycat status
- 带控制台启动
./mycat console
MyCat核心配置
schema.xml配置
schema标签
Schema.xml作为MyCat中重要的配置文件之一,管理着MyCat的逻辑库、表、分片规则、DataNode以及DataSource。弄懂这些配置,是正确使用MyCat的前提。这里就一层层对该文件进行解析。
<!-- 逻辑库 -->
<schema name="rgtest" checkSQLschema="true" sqlMaxLimit="100" >
</schema>
属性名 | 值 | 数量限制 | 说明 |
---|---|---|---|
dataNode | 任意String | (0..1) | 分片节点 |
sqlMaxLimit | Integer | (1) | 查询返回的记录数限制limit |
checkSQLschema | Boolean | (1) | 执行SQL时,是否去掉表所属的库名 |
table标签
table标签定义了Mycat中的逻辑表,所有需要拆分的表都需要在这个标签中定义
<schema name="rgtest" checkSQLschema="true" sqlMaxLimit="100" >
<table name="pay_order" dataNode="dn1,dn2,dn3" rule="auto-sharding-long" primaryKey="id" autoIncrement="true" ></table>
</schema>
属性 | 值 | 数量限制 | 说明 |
---|---|---|---|
name | String | (1) | 逻辑表名 |
dataNode | String | (1..*) | 分片节点 |
rule | String | (0..1) | 分片规则 |
ruleRequired | Boolean | (0..1) | 是否强制绑定分片规则 |
primaryKey | String | (1) | 主键 |
type | String | (0..1) | 逻辑表类型,全局表、普通表 |
autoIncrement | Boolean | (0..1) | 自增长主键 |
subTables | String | (1) | 分表 |
needAddLimit | Boolean | (0..1) | 是否为查询SQL自动加limit限制 |
dataNode标签
dataNode标签定义了MyCat中的分片节点,也就是我们通常说所的数据分片。
<dataNode name="dn1" dataHost="localhost1" database="rgtest1" />
<dataNode name="dn2" dataHost="localhost2" database="rgtest2" />
<dataNode name="dn3" dataHost="localhost2" database="rgtest3" />
- name:定义节点的名字,这个名字需要是唯一的,我们需要在table标签上应用这个名字,来建立表与分片对应的关系。
- dataHost:用于定义该分片属于哪个分片主机,属性值是引用dataHost标签上定义的name属性。
- database:用于定义该分片节点属于哪个具体的库。
dataHost标签
dataHost标签在Mycat逻辑库中也是作为最底层的标签存在,直接定义了具体的数据库实例、读写分离配置和心跳语句
<!-- 节点主机 -->
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="192.168.52.10:3306" user="root" password="123456">
</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="192.168.52.11:3306" user="root" password="123456">
</writeHost>
</dataHost>
属性 | 值 | 数量限制 | 说明 |
---|---|---|---|
name | String | (1) | 节点主机名 |
maxCon | Integer | (1) | 最大连接数 |
minCon | Integer | (1) | 最小连接数 |
balance | Integer | (1) | 读操作负载均衡类型 |
writeType | Integer | (1) | 写操作负载均衡类型 |
dbType | String | (1) | 数据库类型 |
dbDriver | String | (1) | 数据库驱动 |
switchType | String | (1) | 主从切换类型 |
heartbeat标签
heartbeat标签内指明用于和后端数据库进行心跳检查的语句。例如:MySQL可以使用select user()、Oracle可以使用select 1 from dual等
<heartbeat>select user()</heartbeat>
writeHost和readHost标签
- writeHost和readHost标签都指定后端数据库的相关配置给mycat,用于实例化后端连接池。
- writeHost指定写实例,readHost指定读实例。在一个dataHost内可以定义多个writeHost和readHost
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="192.168.52.10:3306" user="root" password="123456">
</writeHost>
</dataHost>
属性 | 值 | 数量限制 | 说明 |
---|---|---|---|
host | String | (1) | 主机名 |
url | String | (1) | 连接字符串 |
password | String | (1) | 密码 |
user | String | (1) | 用户名 |
weight | String | (1) | 权重 |
usingDecrypt | String | (1) | 是否对密码加密,默认0 |
server.xml配置
server.xml几乎保存了所有mycat需要的系统配置信息。
user标签
这个标签主要用于定义登录mycat的用户和权限。
<user name="root" defaultAccount="true">
<property name="password">123456</property>
<property name="schemas">rgtest</property>
<property name="defaultSchema">rgtest</property>
</user>
连接MyCat
- 重启myCat,查看状态
./mycat start
./mycat status
- 连接mycat
mysql -uroot -p123456 -h127.0.0.1 -P8066
rule.xml配置
- rule.xml里面就定义了我们对表进行拆分所涉及到的规则定义。我们可以灵活的对表使用不同的分片算法,或者对表使用相同的算法但具体的参数不同。
- 这个文件里面主要有tableRule和function这两个标 签。在具体使用过程中可以按照需求添加tableRule和function。
此配置文件可以不用修改,使用默认即可。
tableRule标签
<mycat:rule xmlns:mycat="http://io.mycat/">
<tableRule name="sharding-by-intfile">
<rule>
<columns>sharding_id</columns>
<algorithm>hash-int</algorithm>
</rule>
</tableRule>
</mycat:rule>
name:指定唯一的名字,用于标识不同的表规则。
rule: 指定对物理表中的哪一列进行拆分和使用什么路由算法
columns:指定要拆分的列名字。
algorithm:使用function标签中的name属性,连接表规则和具体路由算法。
function标签
<function name="hash-int" class="io.mycat.route.function.PartitionByFileMap">
<property name="mapFile">partition-hash-int.txt</property>
</function>
name:指定算法的名字。
class:制定路由算法具体的类名字。
property:为具体算法需要用到的一些属性。
常用的分片规则
Mycat常用分片配置示例:
- 自动分片
根据指定的列的范围进行分片。默认从0节点开始
<tableRule name="auto-sharding-long">
<rule>
<columns>id</columns>
<algorithm>rang-long</algorithm>
</rule>
</tableRule>
<function name="rang-long" class="io.mycat.route.function.AutoPartitionByLong">
<property name="mapFile">autopartition-long.txt</property>
</function>
autopartition-long.txt文件:
0-200000=0
200000-400000=1
0-200000范围分配给节点0
200000-400000范围分配给节点1
- 枚举分片
把数据分类存储,这种方法适用于取值固定的场合,例如性别和省份
<!-- 枚举分片 -->
<tableRule name="sharding-by-intfile">
<rule>
<columns>sharding_id</columns>
<algorithm>hash-int</algorithm>
</rule>
</tableRule>
<function name="hash-int" class="io.mycat.route.function.PartitionByFileMap">
<property name="mapFile">partition-hash-int.txt</property>
</function>
- mapFile中是自定义的分片策略文件,需要自己编写
- partition-hash-int.txt文件内容如下:
beijing=0
wuhan=1
shanghai=2
- 取模分片
根据配置中的count值进行分片,将数据分成配置的count份,然后将数据均匀的分布在各个节点上
<tableRule name="mod-long">
<rule>
<columns>id</columns>
<algorithm>mod-long</algorithm>
</rule>
</tableRule>
<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
<!-- how many data nodes -->
<property name="count">3</property>
</function>