Mycat安装
1. 下载
下载地址:http://dl.mycat.org.cn/1.6.7.4/Mycat-server-1.6.7.4-release/
我这里以linux部署为例
2. 上传安装包
scp Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz root@39.108.xxx.xxx:.usr/local
我这里是命令上传到linux服务器目录,大家也可以通过工具手工上传,或者直接打开linux用rz上传
3. 解压
tar -zxvf Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz
4. mycat目录所有权授权给mycat用户
添加用户:adduser mycat
授权:chown mycat:mycat mycat/
5. 配置环境变量
编辑profile系统文件
vim /etc/profile
文件最后加入这两行
MYCAT_HOME=/usr/local/mycat
export PATH= P A T H : PATH: PATH:MYCAT_HOME/bin
6. Mycat配置
cd /usr/local/mycat/conf
主要关注一下这三个配置:
rule.xml 主要是指定各个表的分片算法
schema.xml 配置多节点的信息
server.xml mycat服务配置,账号密码配置
schema.xml
配置文件如下,有详细说明
<mycat:schema xmlns:mycat="http://io.mycat/">
<!-- scchema名称就是DB名称,dataNode指定当前表的数据节点,和下面的dataNode name必须一致
rule是分片的规则,我是自己重新定义在rule配置中的crc32slot算法,大家可以使用rule.xml现有的分片算法,部分算法对节点的个数有要求,会导致报错,启动注意查看日志,或者提前看下算法函数-->
<schema name="et_activity_db" checkSQLschema="true" sqlMaxLimit="100">
<table name="et_app_t" dataNode="dn1-1,dn2-1" rule="fixedSlot_et_app_t"/>
<table name="et_handler_t" dataNode="dn1-1,dn2-1" rule="fixedSlot_et_handler_t" primaryKey="id" />
<table name="et_task_t" dataNode="dn1-1,dn2-1" rule="fixedSlot_et_task_t" primaryKey="id"/>
</schema>
<schema name="mysql" checkSQLschema="true" sqlMaxLimit="100">
<table name="user" dataNode="dn1-2,dn2-2" rule="auto-sharding-long"/>
</schema>
<!-- 多节点定义,dataHost指定数据源配置,和下面的dataHost name必须保持一致
database必须在数据源中存在,否则启动会报错-->
<dataNode name="dn1-1" dataHost="pzx" database="db1"/>
<dataNode name="dn1-2" dataHost="pzx" database="db2"/>
<dataNode name="dn2-1" dataHost="pzx" database="db1-1"/>
<dataNode name="dn2-2" dataHost="pzx" database="db2-1"/>
<!--数据源可以配置多个-->
<dataHost name="pzx" maxCon="1000" minCon="10" balance="0"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<!-- can have multi write hosts -->
<writeHost host="hostM1" url="47.110.80.98:3306" user="root"
password="sxl7216000>">
<!-- can have multi read hosts -->
<!-- <readHost host="hostS2" url="192.168.1.200:3306" user="guest" password="123456>" /> -->
</writeHost>
<!-- <writeHost host="hostS1" url="localhost:3316" user="root"
password="123456" /> -->
<!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->
</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="fixedSlot_et_app_t">
<rule>
<columns>app_id</columns>
<algorithm>crc32slot</algorithm>
</rule>
</tableRule>
<tableRule name="fixedSlot_et_handler_t">
<rule>
<!--指定用于计算分片的字段,这样子相同task_id的数据会分配到同一个分片-->
<columns>task_id</columns>
<algorithm>crc32slot</algorithm>
</rule>
</tableRule>
<tableRule name="fixedSlot_et_task_t">
<rule>
<columns>instance_id</columns>
<algorithm>crc32slot</algorithm>
</rule>
</tableRule>
<tableRule name="auto-sharding-long">
<rule>
<columns>id</columns>
<algorithm>rang-long</algorithm>
</rule>
</tableRule>
<function name="crc32slot"
class="io.mycat.route.function.PartitionByCRC32PreSlot">
<property name="count">2</property><!-- 要分片的数据库节点数量,必须指定,否则没法分片 -->
</function>
<function name="rang-long"
class="io.mycat.route.function.AutoPartitionByLong">
<property name="mapFile">autopartition-long.txt</property>
</function>
</mycat:rule>
server.xml
这个文件默认配置都可以直接使用,替换以下部分就行
<user name="root" defaultAccount="true">
<property name="password">123456</property>
<property name="schemas">et_activity_db,mysql</property>
<property name="defaultSchema">et_activity_db</property>
<!--No MyCAT Database selected 错误前会尝试使用该schema作为schema,不设置则为null,报错 -->
<!-- 表级 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">et_activity_db</property>
<property name="readOnly">true</property>
</user>
7. 启动Mycat
mycat start
查看启动日志
vim wrapper.log
如果出现此错误,证明没有java环境,安装jdk,配置环境变量即可解决
启动成功
ps -ef |grep ‘mycat’
接下来就可以像mysql一样使用了,用法几乎一样,有少部分mysql语法不支持,不过不影响使用
命令连接mycat
刚好有一台服务器没有装mycat,我就写了这篇博客帮助大家了解和配置mycat;那么看了之后,你学会了吗?
原创不易,转载请注明原文地址!