MyCAT的优势
基于阿里开源的Cobar产品而研发,Cobar的稳定性、可靠性、优秀的架构和性能以及众多成熟的使用案例使得MYCAT一开始就拥有一个很好的起点,站在巨人的肩膀上,我们能看到更远。业界优秀的开源项目和创新思路被广泛融入到MYCAT的基因中,使得MYCAT在很多方面都领先于目前其他一些同类的开源项目,甚至超越某些商业产品。MYCAT背后有一支强大的技术团队,其参与者都是5年以上资深软件工程师、架构师、DBA等,优秀的技术团队保证了MYCAT的产品质量。MYCAT并不依托于任何一个商业公司,因此不像某些开源项目,将一些重要的特性封闭在其商业产品中,使得开源项目成了一个摆设。高可用方案和架构图如下:
MyCAT 可以视为“MySQL”集群的企业级数据库,用来替代昂贵的Oracle集群,其背后是阿里曾经开源的知名产品Cobar。MyCAT的目标是:低成本的将现有的单机数据库和应用平滑迁移到“云”端,解决数据存储和业务规模迅速增长情况下的数据瓶颈问题。
下面就适合订单的业务场景做介绍
1 应用场景
Mycat 有很多数据分库规则,接下来几篇就相关觉得常用的规则进行试用与总结。
一般来说,按自然月份或按日期来进行数据分片的规则比较适用于商城订单查询,类似最近1周、2周、3个月内的数据。或是报表类应用。
这样的数据放在一个片区内省去了数据合并的时间。
当然按月数据量不要过大就OK。
一、部署步骤详解
(1) 用命令行工具或图形化客户端,连接MYSQL,创建DEMO所用三个分片数据库;CREATE SCHEMA `range_db_01` DEFAULT CHARACTER SET utf8 ;CREATE SCHEMA `range_db_02` DEFAULT CHARACTER SET utf8 ;CREATE SCHEMA `range_db_03` DEFAULT CHARACTER SET utf8 ;CREATE SCHEMA `range_db_04` DEFAULT CHARACTER SET utf8 ;
(2) 修改配置my.cnf新增以下语句, 一般会放在/etc/my.cnf 或 /etc/mysql/my.cnf,设置为Mysql表名大小写不敏感,否则可能会发生表找不到的问题。
lower_case_table_names = 1
(3) 解压Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz 到安装目录下,会生成mycat目录
(4) 安装jdk-7u79-linux-x64
(5) 修改mycat/conf/wrap.conf 修改wrapper.Java.command=java为上一步存放路径
wrapper.java.command=/usr/java/jdk1.7.0_79/
(6) 创建mycat 用户,改变目录权限为mycat
useradd mycatchown -R mycat.mycat mycat
(7) 修改用户密码
passwd mycat输入:
(8) 修改mycat/conf/schema.xml,URL、用户名、密码修改,其余不变
二、运行步骤详解
(1) 进入 mycat/bin (默认数据端口为8066,管理端口为9066)执行./mycat start(2) 进入logs目录,查看日志,如果wrapper.log 报错 java.NET.BindException: Address already in use 杀掉正在执行的相关java进程ps -ef|grep javakill -9 xxx
三、使用步骤详解
(1) 进入mysql bin目录mysql/bin/(2) 登录mysql 执行以下命令./mysql -utest -ptest -h192.168.184.56 -P8066 -DTESTDB(mycat的用户账号和授权信息是在conf/server.xml文件中配置)
(3) 表创建测试:mysql> create table employee (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 employee (id int not null primary key,name varchar(100),sharding_id int not null);+-----------+------------------------------------------------------------------------------------------------+| DATA_NODE | SQL |+-----------+------------------------------------------------------------------------------------------------+| dn1 | create table employee (id int not null primary key,name varchar(100),sharding_id int not null) | | dn2 | create table employee (id int not null primary key,name varchar(100),sharding_id int not null) | +-----------+------------------------------------------------------------------------------------------------+2 rows in set (0.04 sec)
(4) 客户端软件使用:navicat
创建mycat新连接:ip:192.168.184.56,用户名:test,密码:test,端口:8066可看到TESTDB数据库下已创建表:employee打开db1,db2 数据库也可看到已创建表employee(5) 插入数据测试
mysql> insert into employee(id,name,sharding_id) values(1,'leader us',10000);ERROR 2006 (HY000): MySQL server has gone awayNo connection. Trying to reconnect...Connection id: 6Current database: TESTDBQuery OK, 1 row affected (0.03 sec)mysql> explain insert into employee(id,name,sharding_id) values(1,'leader us',10000);+-----------+-----------------------------------------------------------------------+| DATA_NODE | SQL |+-----------+-----------------------------------------------------------------------+| dn1 | insert into employee(id,name,sharding_id) values(1,'leader us',10000) | +-----------+-----------------------------------------------------------------------+1 row in set (0.00 sec)
(6) 根据规则auto-sharding-long(主键范围)进行分片测试
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)
(7) 三个分片上都插入了3条数据
mysql> explain insert into company(id,name) values(1,'hp');+-----------+---------------------------------------------+| DATA_NODE | SQL |+-----------+---------------------------------------------+| dn1 | insert into company(id,name) values(1,'hp') | | dn2 | insert into company(id,name) values(1,'hp') | | dn3 | insert into company(id,name) values(1,'hp') | +-----------+---------------------------------------------+3 rows in set (0.00 sec)
(8) 确认是分片存储
mysql> select * from employee;+----+-----------+-------------+| id | name | sharding_id |+----+-----------+-------------+| 2 | me | 10010 | | 4 | mydog | 10010 | | 1 | leader us | 10000 | | 3 | mycat | 10000 | +----+-----------+-------------+4 rows in set (0.01 sec)mysql> explain select * from employee;+-----------+----------------------------------+| DATA_NODE | SQL |+-----------+----------------------------------+| dn1 | SELECT * FROM employee LIMIT 100 | | dn2 | SELECT * FROM employee LIMIT 100 | +-----------+----------------------------------+2 rows in set (0.00 sec) mycat更详细阅读 -----------------------------------------------------------------重要的TIPs----------------------------------------------------------------------- MyCAT的团队已经发布了1.4Alpha版本,这其中修复了不少的bug,也添加了新功能, 博主这边测试用的是1.3的版本,所以和最新版本的测试结果可能出现不一致! -------------------------------------------------------------------背景介绍------------------------------------------------------------------ MyCAT的背景介绍直接略过,没啥用,当然,这是一个由JAVA开发的东东,这一点需要了解~。 -----------------------------------------------------------------MyCAT的前身---------------------------------------------------------------- MyCAT的前身,是阿里巴巴于2012年6月19日,正式对外开源的数据库中间件Cobar,Cobar的前身是早已经开源的Amoeba,不过其作者陈思儒离职去盛大之后,阿里巴巴内部考虑到Amoeba的稳定性、性能和功能支持,以及其他因素,重新设立了一个项目组并且更换名称为Cobar。Cobar是由 Alibaba 开源的 MySQL 分布式处理中间件,它可以在分布式的环境下看上去像传统数据库一样提供海量数据服务。
Cobar自诞生之日起, 就受到广大程序员的追捧,但是自2013年后,几乎没有后续更新。在此情况下,MyCAT应运而生,它基于阿里开源的Cobar产品而研发,Cobar的稳定性、可靠性、优秀的架构和性能,以及众多成熟的使用案例使得MyCAT一开始就拥有一个很好的起点,站在巨人的肩膀上,MyCAT能看到更远。
---------------------------------------------------------MyCAT的下载方式--------------------------------------------------------------------MyCAT的SVN地址为:---------------------------------------------------------MyCAT的重要特性--------------------------------------------------------------------支持 SQL 92标准;支持MySQL集群,可以作为Proxy使用;支持JDBC连接ORACLE、DB2、SQL Server,将其模拟为MySQL Server使用;支持galera for mysql集群,percona-cluster或者mariadb cluster,提供高可用性数据分片集群;自动故障切换,高可用性;支持读写分离,支持MySQL双主多从,以及一主多从的模式;支持全局表,数据自动分片到多个节点,用于高效表关联查询;支持独有的基于E-R 关系的分片策略,实现了高效的表关联查询;多平台支持,部署和实施简单。------------------------------------------------------------MyCAT的体系结构----------------------------------------------------------------总体上分成三个部分,最前端的是连接器,线程管理使用了资源池,并且默认采用了AIO的方式(这些基本信息可以再启动日志里面看到);中间层在图中已经描述的很清楚了,SQL解析器+SQL路由,SQL Executor需要具体看源码才能了解,因为通过这段时间对MyCAT的测试,没有感觉到SQL Executor的存在,更多的感觉是一个SQL process的东西,DataNode和心跳检测算是中间层实现的两个组件,一个是和MySQL的库(注意,不是实例)相关,一个是常见的监测机制的功能模块;最下层的存储就是是MySQL的集群了~怎么玩MySQL的集群,由我们自己决定╰(?? ▽ ??)╯。点击(此处)折叠或打开
- <?xml version="1.0"?>
- <!DOCTYPE mycat:schema SYSTEM "schema.dtd">
- <mycat:schema xmlns:mycat="http://org.opencloudb/">
- <schema name="weixin" checkSQLschema="false" sqlMaxLimit="100" dataNode="weixin" >
- <schema name="yixin" checkSQLschema="false" sqlMaxLimit="100" dataNode="yixin" />
- <dataNode name="dn1" dataHost="localhost0" database="weixin" />
- <dataNode name="dn2" dataHost="localhost0" database="yixin" />
- <dataHost name="localhost0" maxCon="450" minCon="10" balance="1"
- 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" />
- <readHost host="hostS1" url="localhost:3307" user="test" password="123456" />
- </dataHost>
- </mycat:schema>
点击(此处)折叠或打开
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE mycat:rule SYSTEM "rule.dtd">
- <mycat:rule xmlns:mycat="http://org.opencloudb/">
- <tableRule name="rule">
- <rule>
- <columns>user_id</columns>
- <algorithm>func1</algorithm>
- </rule>
- </tableRule>
- <function name="func1" class="org.opencloudb.route.function.PartitionByLong">
- <property name="partitionCount">2</property>
- <property name="partitionLength">512</property>
- </function>
- </mycat:rule>
点击(此处)折叠或打开
- <!DOCTYPE mycat:server SYSTEM "server.dtd">
- <mycat:server xmlns:mycat="http://org.opencloudb/">
- <system>
- <property name="sequnceHandlerType">0</property>
- </system>
- <user name="test">
- <property name="password">test</property>
- <property name="schemas">weixin,yixin</property>
- </user>
- </mycat:server>
点击(此处)折叠或打开
- <?xml version=\"1.0\"?>
- <!DOCTYPE mycat:schema SYSTEM \"schema.dtd\">
- <mycat:schema xmlns:mycat=\"http://org.opencloudb/\">
- <schema name=\"mycat\" checkSQLschema=\"false\" sqlMaxLimit=\"100\">
- <!-- auto sharding by id (long) -->
- <table name=\"students\" dataNode=\"dn1,dn2,dn3,dn4\" rule=\"rule1\" />
- <table name=\"log_test\" dataNode=\"dn1,dn2,dn3,dn4\" rule=\"rule2\" />
- <!-- 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\" />
- -->
- <table name=\"item_test\" primaryKey=\"ID\" type=\"global\" dataNode=\"dn1,dn2,dn3,dn4\" />
- <!-- random sharding using mod sharind rule -->
- <!-- <table name=\"hotnews\" primaryKey=\"ID\" dataNode=\"dn1,dn2,dn3\"
- rule=\"mod-long\" /> -->
- <!--
- <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\" />
- <ildTable>
- <childTable name=\"customer_addr\" primaryKey=\"ID\" joinKey=\"customer_id\"
- parentKey=\"id\" /> -->
- </schema>
- <!-- <dataNode name=\"dn\" dataHost=\"localhost\" database=\"test\" /> -->
- <dataNode name=\"dn1\" dataHost=\"localhost\" database=\"test1\" />
- <dataNode name=\"dn2\" dataHost=\"localhost\" database=\"test2\" />
- <dataNode name=\"dn3\" dataHost=\"localhost\" database=\"test3\" />
- <dataNode name=\"dn4\" dataHost=\"localhost\" database=\"test4\" />
- <!--
- <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=\"localhost\" maxCon=\"100\" minCon=\"10\" balance=\"1\"
- writeType=\"1\" dbType=\"mysql\" dbDriver=\"native\">
- <heartbeat>select user()<beat>
- <!-- can have multi write hosts -->
- <writeHost host=\"localhost\" url=\"localhost:3306\" user=\"root\" password=\"wangwenan\">
- <!-- can have multi read hosts -->
- <readHost host=\"hostS1\" url=\"localhost:3307\" user=\"root\" password=\"wangwenan\"/>
- </writeHost>
- <writeHost host=\"localhost1\" url=\"localhost:3308\" user=\"root\" password=\"wangwenan\">
- <!-- can have multi read hosts -->
- <readHost host=\"hostS11\" url=\"localhost:3309\" user=\"root\" password=\"wangwenan\"/>
- </writeHost>
- </dataHost>
- <!-- <writeHost host=\"hostM2\" url=\"localhost:3316\" user=\"root\" password=\"123456\"/> -->
- <!--
- <dataHost name=\"jdbchost\" maxCon=\"1000\" minCon=\"1\" balance=\"0\" writeType=\"0\" dbType=\"mongodb\" dbDriver=\"jdbc\">
- <heartbeat>select user()<beat>
- <writeHost host=\"hostM\" url=\"mongodb://192.168.0.99/test\" user=\"admin\" password=\"123456\" ></writeHost>
- </dataHost>
- -->
- <!--
- <dataHost name=\"jdbchost\" maxCon=\"1000\" minCon=\"10\" balance=\"0\"
- dbType=\"mysql\" dbDriver=\"jdbc\">
- <heartbeat>select user()<beat>
- <writeHost host=\"hostM1\" url=\"jdbc:mysql://localhost:3306\"
- user=\"root\" password=\"123456\">
- </writeHost>
- </dataHost>
- -->
- </mycat:schema>
点击(此处)折叠或打开
- <?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="processors">32</property>
- <property name="processorExecutor">256</property>
- <property name="processorBufferPool">204800000</property>
- <property name="processorBufferChunk">40960</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> -->
- <property name="defaultSqlParser">druidparser</property>
- </system>
- <!--
- <user name="root">
- <property name="password">root</property>
- <property name="schemas">test</property>
- </user>
- <user name="root_read">
- <property name="password">root_read</property>
- <property name="schemas">test</property>
- <property name="readOnly">true</property>
- </user>
- -->
- <user name="test">
- <property name="password">test</property>
- <property name="schemas">test</property>
- </user>
- <!-- <cluster> <node name="cobar1"> <property name="host">127.0.0.1</property>
- <property name="weight">1</property> </node> </cluster> -->
- <!-- <quarantine> <host name="1.2.3.4"> <property name="user">test</property>
- </host> </quarantine> -->
- </mycat:server>
点击(此处)折叠或打开
- #used for mycat cache service conf
- factory.encache=org.opencloudb.cache.impl.EnchachePooFactory
- #key is pool name ,value is type,max size, expire seconds
- pool.SQLRouteCache=encache,1500000,60
- pool.ER_SQL2PARENTID=encache,2000,180
- layedpool.TableID2DataNodeCache=encache,3000,18000
- layedpool.TableID2DataNodeCache.TESTDB_ORDERS=10000,18000