mycat安装步骤

本文详细介绍了Mycat的安装过程,包括下载安装包、理解目录结构、启动和停止服务,以及配置数据库分片。在配置过程中,强调了修改conf/wrapper.conf中的JDK路径和jvm参数,以及在服务器上创建分片数据库。通过schema.xml和server.xml文件设置数据源和用户信息,实现了水平分表和数据插入操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、下载mycat安装包

     mycat安装包下载路径:http://www.mycat.io/

2、安装包目录结构介绍

   解压后的目录结构如下图所示:

     

目录说明见下表所示:

目录名称

说明

bin

存放window版本和linux版本,除了提供封装成服务的版本之外,也提供nowrapshell脚本命令,方便大家选择和修改。

Windows  运行:mycat.bat console在控制台启动程序,也可以装载成服务,若此程序运行有问题,也可以运行startup_nowrap.bat,确保java命令可以在命令执行。

Warp方式的命令,可以安装成服务并启动或停止。

        mycat install (可选)

        mycat start

注意,wrap方式的程序,其JVM配置参数在conf/wrap.conf中,可以修改为合适的参数,参数调整参照http://wrapper.tanukisoftware.com/doc/english/properties.html

conf

存放配置文件:

        server.xml:是Mycat服务器参数调整和用户授权的配置文件。

        schema.xml:是逻辑库定义和表以及分片定义的配置文件。

        rule.xml:是分片规则的配置文件,分片规则的具体一些参数信息单独存放为文件,也在这个目录下,配置文件修改,需要重启MyCAT或者通过9066端口reload

        wrapper.confJVM配置参数等设置。

        log4j.xml:日志存放在logs/mycat.log中,每天一个文件,日志的配置是在conf/log4j.xml中,根据自己的需要,可以调整输出级别为debugdebug级别下,会输出更多的信息,方便排查问题。

lib

MyCAT自身的jar包或依赖的jar包的存放目录。

logs

MyCAT日志的存放目录。日志存放在logs/mycat.log中,每天一个文件

2、mycat启动和停止

    进入mycat/bin目录

            安装mycat服务 :mycate install 

            启动mycat服务 :mycate start

            停止mycat服务 :mycate stop

    注意:当修改配置文件后,需要重启mycat服务

    修改conf/wrapper.conf文件中jdk路径和jvm参数



3、创建数据库

         分别在服务器A、服务器B创建所用的分片数据库;

        CREATE database db1;

4、配置文件

    schema.xml配置文件,因为分库在不同的服务器,因此配置两个datahost;如果在一个datahost中配置多个writeHost,则为主从配置。type="global"时,为全局表,

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

    <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
        <!-- auto sharding by id (long) -->
        <table name="travelrecord" dataNode="dn1,dn2" rule="auto-sharding-long" />

        <!-- global table is auto cloned to all defined data nodes ,so can join 
            with any table whose sharding node is in the same data node -->
        <table name="company" primaryKey="ID" type="global" dataNode="dn1,dn2" />
        <table name="goods" primaryKey="ID" type="global" dataNode="dn1,dn2" />

        <!-- random sharding using mod sharind rule -->
        <table name="hotnews" primaryKey="ID" dataNode="dn1,dn2"
            rule="mod-long" />
        <!-- <table name="dual" primaryKey="ID" dataNode="dnx,dnoracle2" type="global"
            needAddLimit="false"/> <table name="worker" primaryKey="ID" dataNode="jdbc_dn1,jdbc_dn2,jdbc_dn3"
            rule="mod-long" /> -->
        <table name="employee" primaryKey="ID" dataNode="dn1,dn2"
            rule="sharding-by-intfile" />
        <table name="customer" primaryKey="ID" dataNode="dn1,dn2"
            rule="sharding-by-intfile">
            <childTable name="orders" primaryKey="ID" joinKey="customer_id"
                parentKey="id">
                <childTable name="order_items" joinKey="order_id"
                    parentKey="id" />
            </childTable>
            <childTable name="customer_addr" primaryKey="ID" joinKey="customer_id"
                parentKey="id" />
        </table>
    </schema>
    <dataNode name="dn1" dataHost="localhost1" database="db1" />
    <dataNode name="dn2" dataHost="localhost2" database="db1" />
    <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.1.201:3306" user="shopuser"
            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="hostM1" url="192.168.1.202:3306" user="shopuser"
            password="123456">
        </writeHost>
    </dataHost>
</mycat:schema>

      server.xml配置文件,本实例很简单,就只定义user, 

      name:用户名

      password:密码

      schemas:实例名,和schema.xml定义的schema对应,这里的实例名是虚拟名,也就是对mycat服务的一种别名,是 应用程序以及客户端连接的入口。

<?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://org.opencloudb/">
    <system>
    <property name="defaultSqlParser">druidparser</property>
      <!--  <property name="useCompression">1</property>--> <!--1为开启mysql压缩协议-->
    <!-- <property name="processorBufferChunk">40960</property> -->
    <!-- 
    <property name="processors">1</property> 
    <property name="processorExecutor">32</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="mutiNodeLimitType">1</property> 0:开启小数量级(默认) ;1:开启亿级数据排序
            <property name="mutiNodePatchSize">100</property> 亿级数量排序批量
            <property name="processors">32</property> <property name="processorExecutor">32</property>
            <property name="serverPort">8066</property> <property name="managerPort">9066</property>
            <property name="idleTimeout">300000</property> <property name="bindIp">0.0.0.0</property>
            <property name="frontWriteQueueSize">4096</property> <property name="processors">32</property> -->
    </system>
    <user name="test">
        <property name="password">test</property>
        <property name="schemas">TESTDB</property>
    </user>
</mycat:server>

  

 5、测试

全局表:company
mysql> create table company(id int not null primary key,name varchar(100),sharding_id int not null);
      Query OK, 0 rows affected (0.30 sec)
      mysql> explain create table company(id int not null primary key,name varchar(100),sharding_id int not null);
     +-----------+------------------------------------------------------------------------------------------------+
     | DATA_NODE | SQL                                                                                            |
     +-----------+------------------------------------------------------------------------------------------------+
     | dn1       | create table company(id int not null primary key,name varchar(100),sharding_id int not null) |
     | dn2       | create table company(id int not null primary key,name varchar(100),sharding_id int not null) |
     +-----------+------------------------------------------------------------------------------------------------+
     2 rows in set (0.04 sec)

mysql> insert into company(id,name,sharding_id) values(1,'leader us',10000);
      ERROR 2006 (HY000): MySQL server has gone away
      No connection. Trying to reconnect...
      Connection id:    6
      Current database: TESTDB

 Query OK, 1 row affected (0.03 sec)
       mysql> explain insert into company(id,name,sharding_id) values(1,'leader us',10000);
       +-----------+-----------------------------------------------------------------------+
       | DATA_NODE | SQL                                                                   |
       +-----------+-----------------------------------------------------------------------+
      | dn1       | insert into company(id,name,sharding_id) values(1,'leader us',10000) |
      +-----------+-----------------------------------------------------------------------+
      1 row in set (0.00 sec)


水平分表:travelrecord

mysql> explain create table travelrecord(id int not null primary key,name varchar(100));
      +-----------+---------------------------------------------------------------------+
      | DATA_NODE | SQL                                                                 |
     +-----------+---------------------------------------------------------------------+
     | dn1       | create table travelrecord(id int not null primary key,name varchar(100)) |
     | dn2       | create table travelrecord(id int not null primary key,name varchar(100)) |
     | dn3       | create table travelrecord(id int not null primary key,name varchar(100)) |
     +-----------+---------------------------------------------------------------------+
     3 rows in set (0.01 sec)

  (7) 三个分片上都插入了3条数据
     mysql> explain insert into travelrecord(id,name) values(1,'hp');
     +-----------+---------------------------------------------+
     | DATA_NODE | SQL                                         |
     +-----------+---------------------------------------------+
     | dn1       | insert into travelrecord(id,name) values(1,'hp') | 
     | dn2       | insert into travelrecord(id,name) values(1,'hp') | 
     | dn3       | insert into travelrecord(id,name) values(1,'hp') | 
     +-----------+---------------------------------------------+
     3 rows in set (0.00 sec)




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值