MyCat

什么是  Mycat

是一个开源的分布式数据库系统,但是因为数据库一般都有自己的数据库引擎,而Mycat并没有属于自己的独有数据库引擎,所有严格意义上说并不能算是一个完整的数据库系统,只能说是一个在应用和数据库之间起桥梁作用的中间件。

Mycat中间件出现之前,MySQL主从复制集群,如果要实现读写分离,一般是在程序段实现,这样就带来了一个问题,即数据段和程序的耦合度太高,如果数据库的地址发生了改变,那么我的程序也要进行相应的修改,如果数据库不小心挂掉了,则同时也意味着程序的不可用,而对于很多应用来说,并不能接受;

 引入Mycat中间件能很好地对程序和数据库进行解耦,这样,程序只需关注数据库中间件的地址,而无需知晓底层数据库是如何提供服务的,大量的通用数据聚合、事务、数据源切换等工作都由中间件来处理;

  Mycat中间件的原理是对数据进行分片处理,从原有的一个库,被切分为多个分片数据库,所有的分片数据库集群构成完成的数据库存储,有点类似磁盘阵列中的RAID0.

 

Mycat安装

创建表结构

CREATE DATABASE IF NOT EXISTS `weibo_simple`;

-- ------------------------------------
-- Table structure for `t_users` 用户表
-- ------------------------------------
DROPTABLEIFEXISTS`t_users`;
CREATETABLE`t_users` (
  `user_id`varchar(64) NOTNULLCOMMENT'注册用户ID',
  `user_email`varchar(64) NOTNULLCOMMENT'注册用户邮箱',
  `user_password`varchar(64) NOTNULLCOMMENT'注册用户密码',
  `user_nikename`varchar(64) NOTNULLCOMMENT'注册用户昵称',
  `user_creatime` datetime NOTNULLCOMMENT'注册时间',
  `user_status` tinyint(1) NOTNULLCOMMENT'验证状态  1:已验证  0:未验证',
  `user_deleteflag` tinyint(1) NOTNULLCOMMENT'删除标记  1:已删除 0:未删除',
  PRIMARY KEY (`user_id`)
) ENGINE=InnoDBDEFAULTCHARSET=utf8;
 
-- -------------------------------------
-- Table structure for `t_message`微博表
-- -------------------------------------
DROPTABLEIFEXISTS`t_message`;
CREATETABLE`t_message` (
  `messages_id`varchar(64) NOTNULLCOMMENT'微博ID',
  `user_id`varchar(64) NOTNULLCOMMENT'发表用户',
  `messages_info`varchar(255) DEFAULTNULLCOMMENT'微博内容',
  `messages_time` datetime DEFAULTNULLCOMMENT'发布时间',
  `messages_commentnum`int(12) DEFAULTNULLCOMMENT'评论次数',
  `message_deleteflag` tinyint(1) NOTNULLCOMMENT'删除标记 1:已删除 0:未删除',
  `message_viewnum`int(12) DEFAULTNULLCOMMENT'被浏览量',
  PRIMARY KEY (`messages_id`),
  KEY`user_id` (`user_id`),
  CONSTRAINT`t_message_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES`t_users` (`user_id`)
) ENGINE=InnoDBDEFAULTCHARSET=utf8;

 

 

配置server.xml

 

         <!-- 添加user -->

   <user name="mycat">

    <property name="password">mycat</property>

    <property name="schemas">mycat</property>

    </user>

        

         <!-- 添加user -->

   <user name="mycat_red">

    <property name="password">mycat_red</property>

    <property name="schemas">mycat</property>

         <property name="readOnly">true</property>

    </user>

 

配置schema.xml

<?xml version="1.0"?>

<!DOCTYPE mycat:schema SYSTEM "schema.dtd">

<mycat:schema xmlns:mycat="http://org.opencloudb/">

    <!-- server.xmluserschemas名一致 -->

    <schema name="mycat" checkSQLschema="true" sqlMaxLimit="100">

        <table name="t_users" primaryKey="user_id" dataNode="dn1" rule="rule1"/>

        <table name="t_message" type="global" primaryKey="messages_id" dataNode="dn1" />

    </schema>

<dataNode name="dn1" dataHost="jdbchost" database="weibo_simple

 

 

" />

  

    <dataHost name="jdbchost" maxCon="1000" minCon="10" balance="1"

                writeType="0" dbType="mysql" dbDriver="native" switchType="1"

                slaveThreshold="100">

         <heartbeat>select user()</heartbeat> 

        <writeHost host="hostMaster" url="172.27.185.1:3306" user="root" password="root">

        </writeHost>

        <writeHost host="hostSlave" url="172.27.185.2:3306" user="root" password="root"/>

    </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://org.opencloudb/">

          <tableRule name="rule1">

        <rule>

            <columns>user_id</columns>

            <algorithm>func1</algorithm>

        </rule>

    </tableRule>

    <function name="func1" class="org.opencloudb.route.function.AutoPartitionByLong">

       <property name="mapFile">autopartition-long.txt</property>

    </function>

</mycat:rule>

为了更好地定位错误,修改log4j.xml

<level value="debug" />

双击startup_nowrap.bat开始启动

 

常见问题

SHOW MASTER STATUS 如果为,则在my.ini文件中添加一行

log-bin=mysql-bin

 

给账号分配权限

grant all privileges on *.* to 'root'@'172.27.185.1' identified by 'root';

查询服务器server_id

SHOW VARIABLES LIKE 'server_id'