安装地址
linux 下载地址:http://dl.mycat.io/1.6.6.1/Mycat-server-1.6.6.1-release-20181031195535-linux.tar.gz
windows下载地址:http://dl.mycat.io/1.6.6.1/Mycat-server-1.6.6.1-release-20181031195535-win.tar.gz
设置mysql远程连接
授权法
mysql -u root -p
use mysql;
grant all privileges on *.* to root@'%' identified by "password";
flush privileges;
改表法
update user set host = ’%’ where user = ’root’;
这里演示的是安装windows版本的
首先解压项目到目录
安装前提是已经安装好jdk
我这里解压到F:\java\mysql目录下
然后添加环境变量
key= MYCAT_HOME
value = F\java\mysql\mycat
添加到path变量中
%MYCAT_HOME%\bin;
项目结构是:

其中简单配置只需要了解三个目录
bin目录下 启动的程序
conf目录下 配置文件
logs 打印日志
conf目录
我们需要配置的是rule.xml schema.xml server.xml(先拷贝一个副本留着)
schema.xml
用来配置真实数据库的一些配置 与逻辑数据库(虚拟数据库)的映射关系
这里会配置三个部分
第一个是 <schema> 用来配置逻辑数据库 名称 与包含的表 需要用到第二个配置
第二个配置<dataNode> 用来装载第三个配置 , 并且添加映射的实际数据库名 (database='db1' )
第三个配置<dataHost > 配置实际数据库节点 中主从节点 读写节点
heartbeat 配置心跳连接
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<!-- 定义一个MyCat的模式,逻辑数据库名称JGDB-->
<!-- “checkSQLschema”:描述的是当前的连接是否需要检测数据库的模式 -->
<!-- “sqlMaxLimit”:表示返回的最大的数据量的行数 -->
<!-- “dataNode="dn1"”:该操作使用的数据节点是dn1的逻辑名称 -->
<!-- 设置表的存储方式.schema name="JGDB" 与 server.xml中的 JGDB 设置一致 -->
<schema name="JGDB" checkSQLschema="false" sqlMaxLimit="100">
<table name="users" primaryKey="id" dataNode="db1" />
<table name="orders" primaryKey="id" dataNode="db2,db3,db4" rule="order_rule" />
</schema>
<!-- 定义数据的操作节点 -->
<!-- “dataHost="localhost1"”:定义数据节点的逻辑名称 -->
<!-- “database="mldn"”:定义数据节点要使用的数据库名称 -->
<!-- 设置dataNode 对应的数据库,及 mycat 连接的地址dataHost -->
<dataNode name="db1" dataHost="host1" database="db1" />
<dataNode name="db2" dataHost="host1" database="db2" />
<dataNode name="db3" dataHost="host1" database="db3" />
<dataNode name="db4" dataHost="host2" database="db4" />
<!-- 定义数据节点,包括了各种逻辑项的配置 -->
<!-- mycat 逻辑主机dataHost对应的物理主机.其中也设置对应的mysql登陆信息 -->
<dataHost name="host1" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native">
<!-- 配置真实MySQL与MyCat的心跳 -->
<heartbeat></heartbeat>
<!-- 配置真实的MySQL的连接路径 -->
<writeHost host="hostM1" url="192.168.0.102:3306" user="root" password="root">
<!-- can have multi read hosts -->
<readHost host="hostS2" url="192.168.178.128:3306" user="root" password="root" />
</writeHost>
</dataHost>
<dataHost name="host2" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native">
<heartbeat></heartbeat>
<writeHost host="hostM2" url="192.168.0.197:3306" user="root" password="123">
<!-- can have multi read hosts -->
<readHost host="host2S2" url="192.168.178.128:3306" user="root" password="root" />
</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="order_rule">
<rule>
<!--根据字段id-->
<columns>id</columns>
<!--根据分库规则 取模-->
<algorithm>mod-long</algorithm>
</rule>
</tableRule>
<!-- 定义方法 mod-long 根据io.mycat.route.function.PartitionByMod类-->
<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
<!-- how many data nodes -->
<!-- 配置节点数量 % count-->
<property name="count">3</property>
</function>
</mycat:rule>
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>
<!--
defaultSqlParser:指定默认的解析器(如解析sql),目前的可用的取值有:druidparser和 fdbparser。
使用的时候可以选择其中的一种,目前一般都使用druidparser
-->
<property name="defaultSqlParser">druidparser</property>
<!-- 0:开启小数量级(默认) ;1:开启亿级数据排序-->
<property name="mutiNodeLimitType">1</property>
<property name="serverPort">8066</property>
<property name="managerPort">9066</property>
</system>
<!-- mycat 的用户名,密码,数据库 -->
<user name="test">
<property name="password">123456</property>
<property name="schemas">JGDB</property>
</user>
<user name="user">
<property name="password">123456</property>
<property name="schemas">JGDB</property>
<property name="readOnly">true</property>
</user>
</mycat:server>
启动
对表操作或点击表时,报1184 错误
如果是提示charsetIndex:XXX 那么你需要打开index_to_charset.properties
在其中添加键值对
key等于报错的id
value等于你设置的编码格式
多表查询
/*!mycat:catlet=io.mycat.catlets.ShareJoin */ select * ....
网友评论