数据库垂直拆分,水平拆分利器,cobar升级版mycat(转)

原文:数据库垂直拆分,水平拆分利器,cobar升级版mycat

1,关于Mycat

Mycat情报 
基于阿里的开源cobar ,可以用于生产系统中,目前在做如下的一些改进: 
非阻塞IO的实现,相对于目前的cobar,并发性能大大提升,而且不会陷入假死状态 
优化线程池的分配,目前cobar的线程池分配效率不高 
修复cobar一些BUG 
参考impala中的impala front部分的Java代码,实现高效的Map-Reduce,能够处理上亿的大数据量 
实现自动分片特性,目前cobar需要手工分片,并有一定的编程限制 
官方网站: 
https://github.com/MyCATApache/ 
好多文档,大多数都是word的,写的非常详细。 
https://github.com/MyCATApache/Mycat-doc 
Mycat是cobar重新优化开发的版本,和cobar的很多配置都类似。 
可以参考之前写的cobar安装: 
http://blog.csdn.net/freewebsys/article/details/44022421

2,安装Mycat服务

下载二进制安装文件 
https://github.com/MyCATApache/Mycat-download 
解压缩到/usr/local/mycat 
修改配置文件:

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://org.opencloudb/">
    #数据库名称是TESTDB,sqlMaxLimit设置limit防止错误sql查询大量数据
    <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
        #自动分库规则
        <!-- auto sharding by id (long) -->
        <table name="travelrecord" dataNode="dn1,dn2,dn3" 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,dn3" />
        <table name="goods" primaryKey="ID" type="global" dataNode="dn1,dn2" />

        <!-- random sharding using mod sharind rule -->
        <table name="hotnews" primaryKey="ID" dataNode="dn1,dn2,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,制定dataHost和数据库名称。
    <dataNode name="dn1" dataHost="localhost1" database="db1" />
    <dataNode name="dn2" dataHost="localhost1" database="db2" />
    <dataNode name="dn3" dataHost="localhost1" database="db3" />
    #配置dataHost的读写分配。同时mysql也要配置好,Master-Slave或Master-Master
    <dataHost name="localhost1" maxCon="1000" minCon="10" balance="0"
        writeType="0" dbType="mysql" dbDriver="native">
        <heartbeat>select user()</heartbeat>
        <!-- can have multi write hosts -->
        <writeHost host="hostM1" url="localhost:3306" user="root"
            password="123456">
            <!-- can have multi read hosts -->
            <!-- <readHost host="hostS1" url="localhost:3306" user="root" password="123456"
                /> -->
        </writeHost>
        <!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->
    </dataHost>

</mycat:schema>

wrapper.conf是mycat的配置文件启动参数等 
rule.xml 是配置规则xml 
还有几个配置文件慢慢研究 
创建数据库

CREATE DATABASE db1 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE DATABASE db2 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE DATABASE db3 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

服务启动:/bin/mycat start 
/bin/mycat这个问题同时可以作为service,拷贝到/etc/init.d/目录下即可。

3,登陆mycat服务和管理端

登陆mycat服务:(端口8066)

# mysql -utest -ptest -h 127.0.0.1 -P8066
Warning: USING a password ON the command line interface can be insecure.
Welcome TO the MySQL monitor.  Commands END WITH ; OR \g.
Your MySQL connection id IS 1
Server version: 5.5.8-mycat-1.3 MyCat Server (OpenCloundDB)

Copyright (c) 2000, 2015, Oracle AND/OR its affiliates. ALL rights reserved.

Oracle IS a registered trademark OF Oracle Corporation AND/OR its
affiliates. Other names may be trademarks OF their respective
owners.

TYPE ‘help;‘ OR ‘\h‘ FOR help. TYPE ‘\c‘ TO clear the CURRENT INPUT statement.

mysql> SHOW DATABASES;
+----------+
| DATABASE |
+----------+
| TESTDB   |
+----------+
1 ROW IN SET (0.02 sec)

mysql> USE TESTDB;
Reading TABLE information FOR completion OF TABLE AND COLUMN names
You can turn off this feature TO GET a quicker startup WITH -A

DATABASE changed
mysql> SHOW TABLES;
+------------------+
| TABLES IN TESTDB |
+------------------+
| company          |
| customer         |
| customer_addr    |
| employee         |
| goods            |
| hotnews          |
| orders           |
| order_items      |
| travelrecord     |
+------------------+
9 ROWS IN SET (0.00 sec)

mysql> CREATE TABLE company(id INT NOT NULL PRIMARY KEY,name VARCHAR(100));
Query OK, 0 ROWS affected (0.05 sec)

mysql> EXPLAIN CREATE TABLE company(id INT NOT NULL PRIMARY KEY,name VARCHAR(100));
+-----------+---------------------------------------------------------------------+
| DATA_NODE | SQL                                                                 |
+-----------+---------------------------------------------------------------------+
| dn1       | CREATE TABLE company(id INT NOT NULL PRIMARY KEY,name VARCHAR(100)) |
| dn2       | CREATE TABLE company(id INT NOT NULL PRIMARY KEY,name VARCHAR(100)) |
| dn3       | CREATE TABLE company(id INT NOT NULL PRIMARY KEY,name VARCHAR(100)) |
+-----------+---------------------------------------------------------------------+
3 ROWS IN SET (0.01 sec)

mysql> INSERT INTO company(id,name) VALUES(1,‘hp‘); INSERT INTO company(id,name) VALUES(2,‘ibm‘); INSERT INTO company(id,name) VALUES(3,‘oracle‘);
Query OK, 3 ROWS affected (0.37 sec)

Query OK, 3 ROWS affected (0.01 sec)

Query OK, 3 ROWS affected (0.00 sec)

mysql> SELECT * FROM company ;
+----+--------+
| id | name   |
+----+--------+
|  1 | hp     |
|  2 | ibm    |
|  3 | oracle |
+----+--------+
3 ROWS IN SET (0.01 sec)

使用管理端登陆:(端口9066)

#  mysql -utest -ptest -h 127.0.0.1 -P9066
Warning: USING a password ON the command line interface can be insecure.
Welcome TO the MySQL monitor.  Commands END WITH ; OR \g.
Your MySQL connection id IS 2
Server version: 5.5.8-mycat-1.3 [email protected]

Copyright (c) 2000, 2015, Oracle AND/OR its affiliates. ALL rights reserved.

Oracle IS a registered trademark OF Oracle Corporation AND/OR its
affiliates. Other names may be trademarks OF their respective
owners.

TYPE ‘help;‘ OR ‘\h‘ FOR help. TYPE ‘\c‘ TO clear the CURRENT INPUT statement.

mysql> SHOW @@help;
+--------------------------------------+-----------------------------------+
| STATEMENT                            | DESCRIPTION                       |
+--------------------------------------+-----------------------------------+
| clear @@slow WHERE datanode = ?      | Clear slow SQL BY datanode        |
| clear @@slow WHERE schema = ?        | Clear slow SQL BY schema          |
| KILL @@connection id1,id2,...        | KILL the specified connections    |
| offline                              | CHANGE MyCat STATUS TO OFF        |
| online                               | CHANGE MyCat STATUS TO ON         |
| reload @@config                      | Reload ALL config FROM file       |
| reload @@route                       | Reload route config FROM file     |
| reload @@USER                        | Reload USER config FROM file      |
| ROLLBACK @@config                    | ROLLBACK ALL config FROM memory   |
| ROLLBACK @@route                     | ROLLBACK route config FROM memory |
| ROLLBACK @@USER                      | ROLLBACK USER config FROM memory  |
| SHOW @@backend                       | Report backend connection STATUS  |
| SHOW @@cache                         | Report system cache usage         |
| SHOW @@command                       | Report commands STATUS            |
| SHOW @@connection                    | Report connection STATUS          |
| SHOW @@connection.SQL                | Report connection SQL             |
| SHOW @@DATABASE                      | Report DATABASES                  |
| SHOW @@datanode                      | Report dataNodes                  |
| SHOW @@datanode WHERE schema = ?     | Report dataNodes                  |
| SHOW @@datasource                    | Report dataSources                |
| SHOW @@datasource WHERE dataNode = ? | Report dataSources                |
| SHOW @@heartbeat                     | Report heartbeat STATUS           |
| SHOW @@parser                        | Report parser STATUS              |
| SHOW @@processor                     | Report processor STATUS           |
| SHOW @@router                        | Report router STATUS              |
| SHOW @@server                        | Report server STATUS              |
| SHOW @@SESSION                       | Report front SESSION details      |
| SHOW @@slow WHERE datanode = ?       | Report datanode slow SQL          |
| SHOW @@slow WHERE schema = ?         | Report schema slow SQL            |
| SHOW @@SQL WHERE id = ?              | Report specify SQL                |
| SHOW @@SQL.detail WHERE id = ?       | Report EXECUTE detail STATUS      |
| SHOW @@SQL.EXECUTE                   | Report EXECUTE STATUS             |
| SHOW @@SQL.slow                      | Report slow SQL                   |
| SHOW @@threadpool                    | Report threadPool STATUS          |
| SHOW @@TIME.CURRENT                  | Report CURRENT TIMESTAMP          |
| SHOW @@TIME.startup                  | Report startup TIMESTAMP          |
| SHOW @@version                       | Report Mycat Server version       |
| stop @@heartbeat name:TIME           | Pause dataNode heartbeat          |
| switch @@datasource name:INDEX       | Switch dataSource                 |
+--------------------------------------+-----------------------------------+
39 ROWS IN SET (0.03 sec)

mysql> SHOW @@backend;
+------------+------+---------+-----------------+------+--------+--------+---------+------+--------+----------+------------+--------+---------+------------+
| processor  | id   | mysqlId | host            | port | l_port | net_in | net_out | life | closed | borrowed | SEND_QUEUE | schema | txlevel | autocommit |
+------------+------+---------+-----------------+------+--------+--------+---------+------+--------+----------+------------+--------+---------+------------+
| Processor0 |   12 |      33 | 127.0.0.1 | 3306 |  42505 |    596 |     501 | 3384 | FALSE  | FALSE    |          0
.......

17 ROWS IN SET (0.03 sec)

mysql> SHOW @@connection;
+------------+------+-----------+------+------------+--------+---------+--------+---------+---------------+-------------+------------+---------+------------+
| PROCESSOR  | ID   | HOST      | PORT | LOCAL_PORT | SCHEMA | CHARSET | NET_IN | NET_OUT | ALIVE_TIME(S) | RECV_BUFFER | SEND_QUEUE | txlevel | autocommit |
+------------+------+-----------+------+------------+--------+---------+--------+---------+---------------+-------------+------------+---------+------------+
| Processor0 |    2 | 127.0.0.1 | 9066 |      35952 | NULL   | utf8    |    156 |    4107 |            40 |        4096 |          0 | NULL    | NULL       |
| Processor0 |    1 | 127.0.0.1 | 8066 |      16525 | TESTDB | utf8    |   2005 |    5132 |          3461 |        4096 |          0 | 3       | TRUE       |
+------------+------+-----------+------+------------+--------+---------+--------+---------+---------------+-------------+------------+---------+------------+
2 ROWS IN SET (0.02 sec)

mysql> SHOW @@heartbeat;
+--------+-------+-----------------+------+---------+-------+--------+---------+--------------+---------------------+-------+
| NAME   | TYPE  | HOST            | PORT | RS_CODE | RETRY | STATUS | TIMEOUT | EXECUTE_TIME | LAST_ACTIVE_TIME    | STOP  |
+--------+-------+-----------------+------+---------+-------+--------+---------+--------------+---------------------+-------+
| hostM1 | mysql | 127.0.0.1 | 3306 |       1 |     0 | idle   |   30000 | 0,0,0        | 2015-03-03 14:45:00 | FALSE |
+--------+-------+-----------------+------+---------+-------+--------+---------+--------------+---------------------+-------+
1 ROW IN SET (0.01 sec)

mysql> SHOW @@datanode;
+------+----------------+-------+-------+--------+------+------+---------+------------+----------+---------+---------------+
| NAME | DATHOST        | INDEX | TYPE  | ACTIVE | IDLE | SIZE | EXECUTE | TOTAL_TIME | MAX_TIME | MAX_SQL | RECOVERY_TIME |
+------+----------------+-------+-------+--------+------+------+---------+------------+----------+---------+---------------+
| dn1  | localhost1/db1 |     0 | mysql |      0 |    9 |   50 |      27 |          0 |        0 |       0 |            -1 |
| dn2  | localhost1/db2 |     0 | mysql |      0 |    4 |   50 |      26 |          0 |        0 |       0 |            -1 |
| dn3  | localhost1/db3 |     0 | mysql |      0 |    3 |   50 |      17 |          0 |        0 |       0 |            -1 |
+------+----------------+-------+-------+--------+------+------+---------+------------+----------+---------+---------------+
3 ROWS IN SET (0.01 sec)

mysql> SHOW @@cache;
+-------------------------------------+-------+------+--------+------+------+---------------+---------------+
| CACHE                               | MAX   | CUR  | ACCESS | HIT  | PUT  | LAST_ACCESS   | LAST_PUT      |
+-------------------------------------+-------+------+--------+------+------+---------------+---------------+
| SQLRouteCache                       | 10000 |    1 |      8 |    0 |    1 | 1425364473193 | 1425363891040 |
| TableID2DataNodeCache.TESTDB_ORDERS | 50000 |    0 |      0 |    0 |    0 |             0 |             0 |
| ER_SQL2PARENTID                     |  1000 |    0 |      0 |    0 |    0 |             0 |             0 |
+-------------------------------------+-------+------+--------+------+------+---------------+---------------+
3 ROWS IN SET (0.05 sec)

4,总结

初步安装测试了下mycat组件,还是非常不错的,确实比起cobar有不少的进步。 
同时他们开发团队也说了,解决了cobar之前存在的问题。 
有了全局表,这样可以使用join了,同时增加读写分离。 
规则增加了不少,能满足大部分需求。 
和cobar一样,mycat可以直接伪装成一个mysql服务器,对业务进行垂直拆分,水平拆分。平滑的进行数据扩展。 
保证在原有系统上进行优化。接下来继续研究mycat,非常好的解决方案。

时间: 2024-10-06 00:07:21

数据库垂直拆分,水平拆分利器,cobar升级版mycat(转)的相关文章

数据库垂直拆分,水平拆分利器,cobar升级版mycat - freewebsys的专栏 - 博客频道 - CSDN.NET

Using Lua/WSAPI with uWSGI - uWSGI 2.0 documentation 数据库垂直拆分,水平拆分利器,cobar升级版mycat - freewebsys的专栏 - 博客频道 - CSDN.NET

关于数据库表的水平拆分和垂直拆分

垂直拆分 垂直拆分是指数据表列的拆分,把一张列比较多的表拆分为多张表 通常我们按以下原则进行垂直拆分: 把不常用的字段单独放在一张表; 把text,blob等大字段拆分出来放在附表中; 经常组合查询的列放在一张表中; 垂直拆分更多时候就应该在数据表设计之初就执行的步骤,然后查询的时候用jion关键起来即可; 水平拆分 水平拆分是指数据表行的拆分,表的行数超过200万行时,就会变慢,这时可以把一张的表的数据拆成多张表来存放. 水平拆分的一些技巧 1. 拆分原则 通常情况下,我们使用取模的方式来进行

MySQL 数据库表的水平拆分

优化数据库结构之数据库表的水平拆分 水平拆分后的每个表结构是相同的,有利于解决单表记录数量过多的问题. 拆分方法: 1 id执行hash运算 如果拆分成5个表,则使用mod(id, 5)取出0-4. 2 根据散列值把数据存到不同的表中. 问题: 1 跨分区表进行数据查询 2 统计及后台报表操作 解决方法: 前后端业务分离,前端查询分区表,后端统计时使用汇总表. 参考资料 数据库表的水平拆分 原文地址:https://www.cnblogs.com/WJQ2017/p/8505535.html

数据库垂直拆分,水平拆分利器,cobar升级版mycat

1,关于Mycat Mycat情报 基于阿里的开源cobar ,可以用于生产系统中,目前在做如下的一些改进: 非阻塞IO的实现,相对于目前的cobar,并发性能大大提升,而且不会陷入假死状态 优化线程池的分配,目前cobar的线程池分配效率不高 修复cobar一些BUG 参考impala中的impala front部分的Java代码,实现高效的Map-Reduce,能够处理上亿的大数据量 实现自动分片特性,目前cobar需要手工分片,并有一定的编程限制 官方网站: https://github.

mysql关于数据库表的水平拆分和垂直拆分

最初知道水平垂直分表的时候是刚参加工作不久的时候,知道了这个概念,但是公司用户量和数据量始终没上来,所以也没用到过,知道有一天到了一家新公司后,这些才被应用到实际开发中,这里我就大概说说关于水平和垂直的拆分. 分表的概念还是比较好理解的,就拿本网站的评论表展开讲讲,源于数据量较大,当评论表有CURD操作时,单张表表现的可能有些力不从心,当然这里还能引申出关于读写速度的其他好多概念:数据库读写分离,NoSql等等. 垂直拆分:顾名思义是将表垂直着给拆掉,即:(下面是省略掉字段的一个表) +----

数据库表的水平拆分和垂直拆分

垂直拆分:顾名思义是将表垂直着给拆掉,即:(下面是省略掉字段的一个表)+--------+---------+--------+--------+-------+---------+---------+--------+-----+-------------+--------+-----------+------+--------+ | userid | groupid | areaid | amount | point | modelid | message | islock | vip | 

分布式数据库选型——数据水平拆分方案

概述 水平拆分的概念随着分布式数据库的推广已为大部分人熟知.分库分表.异构索引.小表广播.这些功能几乎是产品功能需求标配.然而有些客户使用分布式数据库后的体验不尽如意.本文尝试从数据的角度总结分布式数据的复制(replication)和分区(partition)技术原理和方案,其中分区也有称为分片(sharding),希望能引起读者一些思考,在分布式数据库选型中能注意这些细节的区别,选择适合业务的数据水平拆分方案. 分布式数据库架构 分布式数据库以集群形式存在,有多个节点.集群架构有共享磁盘架构

redis和memcached在应用中的架构:水平拆分和垂直拆分

redis和memcached,这两个都是当今非常火的内存数据库,要论流行程度,目前redis用的最多,性能上来讲,如果你把redis的aof和rdb功能全部禁用,只是把redis当做一个缓存来用的话,redis和mencached性能上差不了多少,我相信用redis的不大可能只是用它的缓存功能吧? 这篇文章主要的目的是讲讲redis和memcached的垂直拆分和水平拆分,这里我以redis进行举例,写起来方便点. 一:垂直拆分 redis的垂直拆分实际上和mysql的垂直拆分思路差不多,我们

数据库优化-水平拆分 垂直拆分

过某种特定的条件,将存放在同一个数据库中的数据分散存放到多个数据库上,实现分布存储,通过路由规则路由访问特定的数据库,这样一来每次访问面对的就不是单台服务器了,而是N台服务器,这样就可以降低单台机器的负载压力.提示:sqlserver 2005版本之后,可以友好的支持“表分区”. 垂直(纵向)拆分:是指按功能模块拆分,比如分为订单库.商品库.用户库...这种方式多个数据库之间的表结构不同. 水平(横向)拆分:将同一个表的数据进行分块保存到不同的数据库中,这些数据库中的表结构完全相同. ▲(纵向拆