Mycat概述

it2024-12-25  8

一、概述

是什么

Mycat是数据库中间件

数据库中间件

中间件:是一类连接软件组件和应用的计算机软件,以便于软件各部件之间的沟通。例子:Tomcat,web中间件。

数据库中间件:连接java应用程序和数据库

为什么要用Mycat?

Java与数据库紧耦合高访问量高并发对数据库的压力读写请求数据不一致

数据库中间件对比

中间件名称描述CobarCobar属于阿里B2B事业群,始于2008年,在阿里服役3年多,接管3000+个MySQL数据库的schema,集群日处理在线SQL请求50亿次以上。由于Cobar发起人的离职,Cobar停止维护MycatMycat是开源社区在阿里cobar基础上进行二次开发,解决了cobar存在的问题,并且加入了许多新的功能在其中。青出于蓝而胜于蓝OneProxyOneProxy基于MySQL官方的proxy思想利用c进行开发的,OneProxy是一款商业收费的中间件。舍弃了一些功能,专注在性能和稳定性上kingshardkingshard由小团队用go语言开发,还需要发展,需要不断完善VitessVitess是Youtube生产在使用,架构很复杂。不支持MySQL原生协议,使用需要大量改造成本AtlasAtlas是360团队基于mysql proxy改写,功能还需完善,高并发下不稳定MaxScaleMaxScale是mariadb(MySQL原作者维护的一个版本) 研发的中间MySQLRouteMySQLRoute是MySQL官方Oracle公司发布的中间件

能干什么

一、读写分离

二、数据分片

垂直拆分(分库)水平拆分(分表)垂直+水平拆分(分库分表)

三、多数据源整合

Mycat原理

Mycat 的原理中最重要的一个动词是拦截,它拦截了用户发送过来的 SQL 语句,首先对SQL语句做了一些特定的分析:如分片分析、路由分析、读写分离分析、缓存分析等,然后将此SQL发往后端的真实数据库,并将返回的结果做适当的处理,最终再返回给用户。

这种方式把数据库的分布式从代码中解耦出来,程序员察觉不出来后台使用Mycat还是MySQL。

二、安装部署

安装

1、解压后即可使用

解压缩文件拷贝到centos下 /mycat/

2、三个配置文件

文件名称文件描述schema.xml定义逻辑库、表、分片节点等内容rule.xml定义分片规则server.xml定义用户以及系统相关变量,如端口等

启动

1、修改配置文件server.xml

修改用户信息,与MySQL区分,如下:

<user name="mycat" defaultAccount="true"> <property name="password">123456</property> <property name="schemas">TESTDB</property> </user>

2、修改配置文件 schema.xml

删除标签间的表信息,标签只留一个,标签只留一个,只留一对

<?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/"> <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1"> </schema> <dataNode name="dn1" dataHost="host1" database="cloudDB01" /> <dataHost name="host1" maxCon="1000" minCon="10" balance="2" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <!-- can have multi write hosts --> <writeHost host="hostM1" url="192.168.67.140:3306" user="root" password="123456"> <!-- can have multi read hosts --> <readHost host="hostS1" url="192.168.67.130:3306" user="root" password="123456" /> </writeHost> </dataHost> </mycat:schema>

3、验证数据库访问情况

Mycat 作为数据库中间件要和数据库部署在不同机器上,所以要验证远程访问情况。

mysql -uroot -h 192.168.67.130 -P 3306 -p123456 mysql -uroot -h 192.168.67.140 -P 3306 -p123456

#如远程访问报错,请建对应用户 grant all privileges on . to root@‘缺少的host’ identified by ‘123456’;

4、启动程序

控制台启动 :去 mycat/bin 目录下执行 ./mycat console后台启动 :去 mycat/bin 目录下 ./mycat start

为了能第一时间看到启动日志,方便定位问题,我们选择控制台启动。

启动Mycat命令

./mycat start

查看启动状态

./mycat status

停止Mycat命令

./mycat stop

重启Mycat命令

./mycat restart

登录

方式1:登录后台管理窗口(端口:9066)

此登录方式用于管理维护Mycat

mysql -umycat -p123456 -P9066 -h 192.168.67.140

#常用命令如下:

[root@CentOS001 conf]# mysql -umycat -p123456 -P9066 -h 192.168.67.140 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 13 Server version: 5.6.29-mycat-1.6.7.1-release-20190627191042 MyCat Server (monitor) Copyright (c) 2000, 2016, 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.00 sec) mysql> use TESTDB; Database changed mysql> show @@help; +--------------------------------------------------------------+--------------------------------------------+ | STATEMENT | DESCRIPTION | +--------------------------------------------------------------+--------------------------------------------+ | show @@time.current | Report current timestamp | | show @@time.startup | Report startup timestamp | | show @@version | Report Mycat Server version | | show @@server | Report server status | | show @@threadpool | Report threadPool status | | 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 @@datasource.synstatus | Report datasource data synchronous | | show @@datasource.syndetail where name=? | Report datasource data synchronous detail | | show @@datasource.cluster | Report datasource galary cluster variables | | show @@processor | Report processor status | | show @@command | Report commands status | | show @@connection | Report connection status | | show @@cache | Report system cache usage | | show @@backend | Report backend connection status | | show @@session | Report front session details | | show @@connection.sql | Report connection sql | | show @@sql.execute | Report execute status | | show @@sql.detail where id = ? | Report execute detail status | | show @@sql | Report SQL list | | show @@sql.high | Report Hight Frequency SQL | | show @@sql.slow | Report slow SQL | | show @@sql.resultset | Report BIG RESULTSET SQL | | show @@sql.sum | Report User RW Stat | | show @@sql.sum.user | Report User RW Stat | | show @@sql.sum.table | Report Table RW Stat | | show @@parser | Report parser status | | show @@router | Report router status | | show @@heartbeat | Report heartbeat status | | show @@heartbeat.detail where name=? | Report heartbeat current detail | | show @@slow where schema = ? | Report schema slow sql | | show @@slow where datanode = ? | Report datanode slow sql | | show @@sysparam | Report system param | | show @@syslog limit=? | Report system mycat.log | | show @@white | show mycat white host | | show @@white.set=?,? | set mycat white host,[ip,user] | | show @@directmemory=1 or 2 | show mycat direct memory usage | | show @@check_global -SCHEMA= ? -TABLE=? -retry=? -interval=? | check mycat global table consistency | | switch @@datasource name:index | Switch dataSource | | kill @@connection id1,id2,... | Kill the specified connections | | stop @@heartbeat name:time | Pause dataNode heartbeat | | reload @@config | Reload basic config from file | | reload @@config_all | Reload all config from file | | reload @@route | Reload route config from file | | reload @@user | Reload user config from file | | reload @@sqlslow= | Set Slow SQL Time(ms) | | reload @@user_stat | Reset show @@sql @@sql.sum @@sql.slow | | rollback @@config | Rollback all config from memory | | rollback @@route | Rollback route config from memory | | rollback @@user | Rollback user config from memory | | reload @@sqlstat=open | Open real-time sql stat analyzer | | reload @@sqlstat=close | Close real-time sql stat analyzer | | offline | Change MyCat status to OFF | | online | Change MyCat status to ON | | clear @@slow where schema = ? | Clear slow sql by schema | | clear @@slow where datanode = ? | Clear slow sql by datanode | +--------------------------------------------------------------+--------------------------------------------+ 59 rows in set (0.00 sec) mysql> show @@version; +---------------------------------------------+ | VERSION | +---------------------------------------------+ | 5.6.29-mycat-1.6.7.1-release-20190627191042 | +---------------------------------------------+ 1 row in set (0.00 sec) mysql>

方式2:登录数据窗口(端口:8066)

此登录方式用于通过 Mycat 查询数据,我们选择这种方式访问 Mycat

mysql -umycat -p123456 -P8066 -h 192.168.67.140 [root@CentOS001 conf]# mysql -umycat -p123456 -P8066 -h 192.168.67.140 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 14 Server version: 5.6.29-mycat-1.6.7.1-release-20190627191042 MyCat Server (OpenCloudDB) Copyright (c) 2000, 2016, 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.00 sec) mysql> use TESTDB; Database changed mysql> show tables; +---------------------+ | Tables_in_cloudDB01 | +---------------------+ | dept | | user | +---------------------+ 2 rows in set (0.00 sec) mysql> select * from dept; +--------+--------------+-----------+ | deptno | dname | db_source | +--------+--------------+-----------+ | 1 | 开发部 | cloudDB01 | | 2 | 人事部 | cloudDB01 | | 3 | 财务部 | cloudDB01 | | 4 | 市场部 | cloudDB01 | | 5 | 运维部 | cloudDB01 | | 6 | AI | cloudDB01 | | 7 | 大数据部 | cloudDB01 | | 8 | 营销部 | cloudDB01 | +--------+--------------+-----------+ 8 rows in set (0.00 sec) mysql>
最新回复(0)