vlambda博客
学习文章列表

Mycat 分库教程,常见问题解答

Mycat 使用踩坑教程

一  .安装

可在官网上下载  Mycat-server-1.6.7.4-release版本  (http://www.mycat.io/)

解压文件,我们得到

D:.
├─.idea
├─bin --startup_nowrap.bat 启动脚本目录
├─catlet
├─conf 配置文件目录
│ ├─zkconf
│ └─zkdownload
├─lib
├─logs 日志文件
│ ├─2020-03
│ └─2020-04
└─tmlogs

二 .配置文件说明

schema.xml

用于配置逻辑库的配置文件

	<schema name="eval" checkSQLschema="true" sqlMaxLimit="100" >
<!-- auto sharding by id (long) -->
<!--splitTableNames 启用<table name 属性使用逗号分割配置多个表,即多个表使用这个配置-->
<table name="rep_detail,eval_exam,eval_exam_subject,rep_question_relationship" primaryKey="ID" rule="sharding-by-pattern" autoIncrement="true" ruleRequired="true"
dataNode="dn$1-5" splitTableNames ="true" />
<!-- <table name="oc_call" primaryKey="ID" dataNode="dn1$0-743" rule="latest-month-calldate"
/> -->
</schema>

schema 标签用于定义 MyCat 实例中的逻辑库,MyCat 可以有多个逻辑库,每个逻辑库都有自己的相关配 置。可以使用 schema 标签来划分这些不同的逻辑库。

上面的配置,采用了逻辑库eval,使用了分片规则 'sharding-by-pattern',分别对应了四张表 分别对应的节点(dataNode)为'dn1'到'dn5'

	<dataNode name="dn1" dataHost="localhost1" database="qd_eval_dev01" />
<dataNode name="dn2" dataHost="localhost2" database="qd_eval_dev02" />
<dataNode name="dn3" dataHost="localhost3" database="qd_eval_dev03" />
<dataNode name="dn4" dataHost="localhost4" database="qd_eval_dev04" />
<dataNode name="dn5" dataHost="localhost5" database="qd_eval_dev05" />

以下是五个对应节点,需要注意的是'database'是对应的mysql物理库,例如:

	<dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="jdbc" switchType="2" >
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="jdbc:mysql://localhost:3306" user="eval" password="123456"> </writeHost>
<writeHost host="hostM2" url="jdbc:mysql://localhost:3306" user="eval" password="123456"> </writeHost>
</dataHost>

<dataHost name="localhost2" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="jdbc" switchType="2" >
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="jdbc:mysql://localhost:3306" user="eval" password="123456"> </writeHost>
<writeHost host="hostM2" url="jdbc:mysql://localhost:3306" user="eval" password="123456"> </writeHost>
</dataHost>

<dataHost name="localhost3" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="jdbc" switchType="2" >
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="jdbc:mysql://localhost:3306" user="eval" password="123456"> </writeHost>
<writeHost host="hostM2" url="jdbc:mysql://localhost:3306" user="eval" password="123456"> </writeHost>
</dataHost>

<dataHost name="localhost4" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="jdbc" switchType="2" >
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="jdbc:mysql://localhost:3306" user="eval" password="123456"> </writeHost>
<writeHost host="hostM2" url="jdbc:mysql://localhost:3306" user="eval" password="123456"> </writeHost>
</dataHost>

<dataHost name="localhost5" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="jdbc" switchType="2" >
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="jdbc:mysql://localhost:3306" user="eval" password="123456"> </writeHost>
<writeHost host="hostM2" url="jdbc:mysql://localhost:3306" user="eval" password="123456"> </writeHost>
</dataHost>

'dataHost'该标签在 mycat 逻辑库中也是作为最底层的标签存在,直接定义了具 体的数据库实例、读写分离配置和心跳语句。完整配置:

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">

<schema name="eval" checkSQLschema="true" sqlMaxLimit="100" >
<!-- auto sharding by id (long) -->
<!--splitTableNames 启用<table name 属性使用逗号分割配置多个表,即多个表使用这个配置-->
<table name="rep_detail,eval_exam,eval_exam_subject,rep_question_relationship" primaryKey="ID" rule="sharding-by-pattern" autoIncrement="true" ruleRequired="true"
dataNode="dn$1-5" splitTableNames ="true" />
<!-- <table name="oc_call" primaryKey="ID" dataNode="dn1$0-743" rule="latest-month-calldate"
/> -->
</schema>

<!-- <dataNode name="dn1$0-743" dataHost="localhost1" database="db$0-743"/> -->
<dataNode name="dn1" dataHost="localhost1" database="qd_eval_dev01" />
<dataNode name="dn2" dataHost="localhost2" database="qd_eval_dev02" />
<dataNode name="dn3" dataHost="localhost3" database="qd_eval_dev03" />
<dataNode name="dn4" dataHost="localhost4" database="qd_eval_dev04" />
<dataNode name="dn5" dataHost="localhost5" database="qd_eval_dev05" />
<!--<dataNode name="dn4" dataHost="sequoiadb1" database="SAMPLE" />
<dataNode name="jdbc_dn1" dataHost="jdbchost" database="db1" />
<dataNode name="jdbc_dn2" dataHost="jdbchost" database="db2" />
<dataNode name="jdbc_dn3" dataHost="jdbchost" database="db3" /> -->
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="jdbc" switchType="2" >
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="jdbc:mysql://localhost:3306" user="eval" password="123456"> </writeHost>
<writeHost host="hostM2" url="jdbc:mysql://localhost:3306" user="eval" password="123456"> </writeHost>
</dataHost>

<dataHost name="localhost2" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="jdbc" switchType="2" >
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="jdbc:mysql://localhost:3306" user="eval" password="123456"> </writeHost>
<writeHost host="hostM2" url="jdbc:mysql://localhost:3306" user="eval" password="123456"> </writeHost>
</dataHost>

<dataHost name="localhost3" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="jdbc" switchType="2" >
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="jdbc:mysql://localhost:3306" user="eval" password="123456"> </writeHost>
<writeHost host="hostM2" url="jdbc:mysql://localhost:3306" user="eval" password="123456"> </writeHost>
</dataHost>

<dataHost name="localhost4" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="jdbc" switchType="2" >
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="jdbc:mysql://localhost:3306" user="eval" password="123456"> </writeHost>
<writeHost host="hostM2" url="jdbc:mysql://localhost:3306" user="eval" password="123456"> </writeHost>
</dataHost>

<dataHost name="localhost5" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="jdbc" switchType="2" >
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="jdbc:mysql://localhost:3306" user="eval" password="123456"> </writeHost>
<writeHost host="hostM2" url="jdbc:mysql://localhost:3306" user="eval" password="123456"> </writeHost>
</dataHost>




<!--
<dataHost name="sequoiadb1" maxCon="1000" minCon="1" balance="1" dbType="sequoiadb" dbDriver="jdbc">
<heartbeat> </heartbeat>
<writeHost host="hostM1" url="sequoiadb://1426587161.dbaas.sequoialab.net:11920/SAMPLE" user="jifeng" password="jifeng"></writeHost>
</dataHost>

<dataHost name="oracle1" maxCon="1000" minCon="1" balance="1" writeType="0" dbType="oracle" dbDriver="jdbc"> <heartbeat>select 1 from dual</heartbeat>
<connectionInitSql>alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss'</connectionInitSql>
<writeHost host="hostM1" url="jdbc:oracle:thin:@127.0.0.1:1521:nange" user="base" password="123456" > </writeHost> </dataHost>

<dataHost name="jdbchost" maxCon="1000" minCon="1" balance="1" writeType="0" dbType="mongodb" dbDriver="jdbc">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM" url="mongodb://192.168.0.99/test" user="admin" password="123456" ></writeHost> </dataHost>

<dataHost name="sparksql" maxCon="1000" minCon="1" balance="1" dbType="spark" dbDriver="jdbc">
<heartbeat> </heartbeat>
<writeHost host="hostM1" url="jdbc:hive2://feng01:10000" user="jifeng" password="jifeng"></writeHost> </dataHost> -->

<!-- <dataHost name="jdbchost" maxCon="1000" minCon="10" balance="1" dbType="mysql"
dbDriver="jdbc"> <heartbeat>select user()</heartbeat> <writeHost host="hostM1"
url="jdbc:mysql://localhost:3306" user="root" password="123456"> </writeHost>
</dataHost> -->
</mycat:schema>

server.xml

server.xml 几乎保存了所有 mycat 需要的系统配置信息。其在代码内直接的映射类为 SystemConfig 类。此配置文件除了白名单之外,还需要注意mycat默认使用的编码为utf8,完整配置

<?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="ignoreUnknownCommand">0</property><!-- 0遇上没有实现的报文(Unknown command:),就会报错、1为忽略该报文,返回ok报文。
在某些mysql客户端存在客户端已经登录的时候还会继续发送登录报文,mycat会报错,该设置可以绕过这个错误-->
<property name="useHandshakeV10">1</property>
<property name="charset">utf8mb4</property>
<property name="removeGraveAccent">1</property>
<property name="useSqlStat">0</property> <!-- 1为开启实时统计、0为关闭 -->
<property name="useGlobleTableCheck">0</property> <!-- 1为开启全加班一致性检测、0为关闭 -->
<property name="sqlExecuteTimeout">300</property> <!-- SQL 执行超时 单位:秒-->
<property name="sequnceHandlerType">2</property>
<!--<property name="sequnceHandlerPattern">(?:(\s*next\s+value\s+for\s*MYCATSEQ_(\w+))(,|\)|\s)*)+</property>
INSERT INTO `travelrecord` (`id`,user_id) VALUES ('next value for MYCATSEQ_GLOBAL',"xxx");
-->
<!--必须带有MYCATSEQ_或者 mycatseq_进入序列匹配流程 注意MYCATSEQ_有空格的情况-->
<property name="sequnceHandlerPattern">(?:(\s*next\s+value\s+for\s*MYCATSEQ_(\w+))(,|\)|\s)*)+</property>
<property name="subqueryRelationshipCheck">false</property> <!-- 子查询中存在关联查询的情况下,检查关联字段中是否有分片字段 .默认 false -->
<property name="sequenceHanlderClass">io.mycat.route.sequence.handler.HttpIncrSequenceHandler</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 | 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>
<!-- 5 * 60 * 1000L; //连接空闲检查-->
<property name="dataNodeIdleCheckPeriod">300000</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">0</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>
<!--如果为0的话,涉及多个DataNode的catlet任务不会跨线程执行-->
<property name="parallExecute">0</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="192.168.*.*" user="root"/>
<host host="192.168.*.*" user="eval"/>
<host host="127.0.0.1" user="root"/>
<host host="127.0.0.1" user="eval"/>
<host host="0:0:0:0:0:0:0:1" user="root"/>
<host host="0:0:0:0:0:0:0:1" user="eval"/>
</whitehost>
<blacklist check="false">
</blacklist>
</firewall>
<user name="root" defaultAccount="true">
<property name="password">li2wg3vA</property>
<property name="schemas">eval</property>
<property name="defaultSchema">eval</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">123456</property>
<property name="schemas">eval</property>
<property name="defaultSchema">eval</property>
</user>

<user name="eval">
<property name="password">123456</property>
<property name="schemas">eval</property>
<property name="defaultSchema">eval</property>
</user>


</mycat:server>

rule.xml

rule.xml 里面就定义了我们对表进行拆分所涉及到的规则定义。我们可以灵活的对表使用不同的分片算法, 或者对表使用相同的算法但具体的参数不同。这个文件里面主要有 tableRule 和 function 这两个标签。在具体使 用过程中可以按照需求添加 tableRule 和 function 在此次介绍中使用的规则'sharding-by-pattern'是按学校id%25600得到的值分库

	<tableRule name="sharding-by-pattern">
<rule>
<columns>school_id</columns>
<algorithm>sharding-by-pattern</algorithm>
</rule>
</tableRule>

<function name="sharding-by-pattern" class="io.mycat.route.function.PartitionByPattern">
<property name="mapFile">sharding-by-pattern.txt</property>
<property name="defaultNode">0</property>
<property name="patternValue">25600</property>
</function>

规则文件'sharding-by-pattern.txt'保存在conf目录下

1-10000=0
10001-15000=1
15001-20000=2
20001-25000=3
25001-30000=4

当取模在这边区间内,数据就会被路由到上面配置的库里

完整的配置

<?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="sharding-by-date">
<rule>
<columns>createTime</columns>
<algorithm>partbyday</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="sharding-by-exam-code">
<rule>
<columns>school_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>
<tableRule name="sharding-by-pattern">
<rule>
<columns>school_id</columns>
<algorithm>sharding-by-pattern</algorithm>
</rule>
</tableRule>

<function name="sharding-by-pattern" class="io.mycat.route.function.PartitionByPattern">
<property name="mapFile">sharding-by-pattern.txt</property>
<property name="defaultNode">0</property>
<property name="patternValue">25600</property>
</function>


<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">
<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">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="partbyday"
class="io.mycat.route.function.PartitionByDate">
<property name="dateFormat">yyyy-MM-dd</property>
<property name="sNaturalDay">0</property>
<property name="sBeginDate">2014-01-01</property>
<property name="sEndDate">2014-01-31</property>
<property name="sPartionDay">10</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>

三  .常见问题

上诉的三个配置文件,已经是一个按学校id切分并且用五个库来不同存储的配置文件.

Mycat 如何配置字符集?

在配置文件 server.xml 配置,默认配置为 utf8。

<system>
<property name="charset">utf8</property>
</system>

Mycat 主键插入后应用如何获取?

获得自增主键,插入记录后执行 select last_insert_id()获取

Mycat 中,旧系统数据如何迁移到 Mycat 中?

旧数据迁移目前可以手工导入,在 mycat 中提取配置好分配规则及后端分片数据库,然后通过 dump 或 loaddata 方式导入,后续 Mycat 就做旧数据自动数据迁移工具。

Mycat 支持多表 Join 吗?

Mycat 目前支持 2 个表 Join

Mycat 连接会报无效数据源(Invalid datasource)

这类错误最常见是一些配置问题例如 schema.xml 中的 dataNode 的配置和实际不符合,请先仔细检查配置 项,确保配置没有问题。如果不是配置问题,分析具体日志看出错原因,常见的有:如果是应用连:在某些版本的 Mysql 驱动下连接 Mycat 会报错,可升级最新的驱动包试下。如果是服务端控制台连,确认 mysql 是否开启远程连接权限,或防火墙是否设置正确,或者数据库 database 是否配置,或用户名密码是否正确。

Mycat 如何建表与创建存储过程?

注意注解中语句是节点的表请替换成自己表如 select 1 from 表 ,查出来的数据在那个节点往哪个节点 建 存储过程

/*!mycat: sql=select 1 from 表 */ CREATE DEFINER=`root`@`%` PROCEDURE `proc_test`() BEGIN
END ;
表:
/*!mycat: sql=select 1 from 表 */create table ttt(id int);

Mycat 支持的或者不支持的语句有哪些?

insert into,复杂子查询,3 表及其以上跨库 join 等不支持

Mycat 中文乱码的问题

如果在使用 mycat 出现中文插入或者查询出现乱码,请检查三个环节的字符集设置:1)客户端环节
(应用程序、mysql 命令或图形终端工具)连接 mycat 字符集
2)mycat 连接数据库的字符集
3)数据库
(mysql,oracle)字符集。这三个环节的字符集如果配置一致,则不会出现中文乱码,其中尤其需要注意的是客 户端连接 mycat 时使用的连接字符集,通常的中文乱码问题一般都由此处设置不当引出。其中 mycat 内部默认使 用 utf8 字符集,在最初启动连接数据库时,mycat 会默认使用 utf8 去连接数据库,当客户端真正连接 mycat 访 问数据库时,mycat 会使用客户端连接使用的字符集修改它连接数据库的字符集,在 mycat 环境的管理 9066 端 口,可以通过 show @@backend 命令查看后端数据库的连接字符集,通过 show @@connection 命令查看前 端客户端的连接字符集。客户端的连接可以通过指定字符集编码或者发送 SET 命令指定连接 mycat 时 connection 使用的字符集,常见客户端连接指定字符集写法如下:

1) jdbcUrl=jdbc:mysql://localhost:8066/databaseName? characterEncoding=iso_1
2) SET character_set_client = utf8;用来指定解析客户端传递数据的编码
SET character_set_results = utf8;用来指定数据库内部处理时使用的编码
SET character_set_connection = utf8;用来指定数据返回给客户端的编码方式
3) mysql –utest –ptest –P8066 --default-character-set=gbk