Mycat Quick Start

What is Mycat

As for what is Mycat, there may be different interpretations in different roles. Had knowledge of MySQL architecture, then, to know that MySQL Server is actually composed of layers and layers of storage engine. So for the DBA , Mycat is the MySQL Server layer. The MySQL Server behind Mycat connected, as if MySQL storage engine. Therefore, Mycat itself does not store data, the data is stored on a MySQL backend, so the reliability of the data
and transactions such as MySQL is still guaranteed.

For developers , Mycat basically the same as MySQL. Because the connection can use to connect to the MySQL Mycat, in addition to a different port than the default, there is no difference on the connection. So Mycat is transparent to developers, but there are some restrictions on the use of SQL.

For architects , myCat is a middleware database can be used as separate read and write, sub-sub-table database backup and disaster recovery. For applications using the Mycat, its hidden storage logical database. Let us do not need to go up in the code to read and write separation, storage control logic to achieve without modifying the application code premise, expand the database schema.

Since Mycat is the open source project, there is a very sophisticated and rich Chinese documents. Therefore, this article will not go into details on the basis of the concept, the proposed reference Mycat the online version of The Definitive Guide or to Mycat official website Learn more about the Mycat.


What is a database middleware

Database middleware is interposed between the database and the application, intermediate interaction with data processing services. Since then fragmentation processing the data, from the original library, it is cut into a plurality of slices databases, all fragments of the entire database cluster configuration database stores complete.

Mycat Quick Start

As indicated by FIG., The data has been fragmented into multiple databases, if the application needs to read data, the data will need to handle a plurality of data sources.

如果没有数据库中间件,那么应用将直接面对分片集群。那么数据源切换、事务处理以及数据聚合等问题都需要应用直接处理。这样就会导致原本该是专注于业务的应用,将会花大量的工作来处理分片后的问题,最重要的是每个应用处理将是完全的重复造轮子。

因此有了数据库中间件,应用只需要集中于业务处理。而大量的通用的数据聚合,事务,数据源切换都由中间件来处理,中间件的性能与处理能力将直接决定应用的读写性能,所以一款好的数据库中间件至关重要。


Mycat的主要作用

1、作为分布式数据库中间件使用:

Mycat不仅可以连接mysql还可以连接其他诸如oracle、sql server等数据库,因为Mycat是使用Java编写的,理论上只要Java能连接的数据库,Mycat都可以连接。这就可以使得应用无需直连数据库,而是连接Mycat,此时后端数据库对应用就是透明的。这样就可以对应用屏蔽后端数据库的复杂性,并保持连接方式的统一。

2、实现数据库集群的读写分离和负载均衡:

当系统发展到后期,数据量上来后,数据库架构通常会向集群演进。其中Replication集群方案,节点存在主从角色,也就是常说的主从架构。在这种集群下通常由主节点负责写,从节点负责读。而Mycat可以作为中间层实现读写分离,无需每个应用都写一套读写分离逻辑。并且当存在多个从节点时,Mycat还可以将读请求负载均衡到各个从节点上。

3、实现数据库的高可用:

当数据库架构是一主一从时,可以利用Mycat实现数据库的高可用。当主节点挂掉后,Mycat可以切换到从节点上进行读写。但不适用于一主多从的情况,因为主节点挂掉后,Mycat只能切换到其中一个从节点上,而不能改变节点的角色。这样就会导致剩余的从节点无法对其进行数据同步。因此,一主多从需采用MMM或MHA等方式来实现高可用。

4、对业务数据库进行垂直切分:

当系统中的写负载上来后,单个主节点开始无法承受写请求的压力,这时就需要增加主节点来分担负载。拥有多个主节点后,通常会将一个库里没有依赖关系的表拆分到不同的库中,或将一些大表拆成小表,以分散负载。这也就是数据库的垂直切分。

存在多个主节点就意味着应用可能需要对多个数据库进行操作,而Mycat可以将多个数据库抽象成一个逻辑库。这样对于应用来说就无需改动代码去操作多个数据源,只需要操作Mycat中的逻辑库即可。

5、对业务数据库进行水平切分:

当数据库进行了垂直切分后,可能依旧会有一些库的写负载比较高,或者数据量达到单表极限。那么就需要考虑进行水平切分,所谓水平切分就是分库分表。垂直切分与水平切分的区别在于:水平切分后每个库里的表结构是一样的,切分的是数据。而垂直切分则是不同的库里表结构是不一样的,切分的是表。

当数据库进行水平切分后,应用在写数据时就需要考虑应该将数据写入到哪一个库中。而Mycat可以通过特定的策略将写请求分发到某个数据库上,这样应用就只需要将写请求发给Mycat即可,无需考虑应该写哪个库。

6、控制数据库连接数量:

系统中存在比较多的应用,而每个应用都有自己的一个连接池,那么当访问量增大就有可能超出数据库的最大连接数量。此时就可以通过Mycat建立一个公共的连接池,所有的应用都统一通过这个连接池来获取数据库的连接。从而控制同时连接数据库的应用数量,起到一个限流的作用,可以保证系统更加的稳定。


Mycat基本元素

逻辑库:

通常对实际应用来说,并不需要知道Mycat的存在,业务开发人员只需要知道数据库的概念,所以数据库中间件可以被看做是一个或多个数据库集群构成的逻辑库,需要注意的是逻辑库不存储数据。所以不管中间件背后是怎么样的数据库架构,对于应用来说都是在操作一个数据库,而这个数据库就是Mycat抽象出来的逻辑库。

逻辑表:

Since there is a logical library, there will be a logical table, distributed databases, applications for reading and writing data table is a logical table. Logic table, the data may be segmented, or distributed in a plurality of slices libraries. Segmentation data may not, not fragment, only one table configuration. Likewise, the logic table does not store data.

Logical table Category:

  • Fragmented tables: split into a plurality of data tables in the database
  • Non-fragmented table: table data is not split off
  • The global table: dictionary-like structure and is present in all slice tables, object data is fragmented across the join solution
  • ER table: a table fragmentation by ER relations, the purpose is to make records in the child table and the parent table records the associated data stored on the same slice, to ensure that data will not join cross-database

Mycat installation

surroundings:

  • System: CentOS 7
  • JDK:1.8
  • Mycat:1.6.7.4

First, the official Download get the download link, and then to the linux use the following command to download and extract:

[root@txy-server ~]# cd /usr/local/src
[root@txy-server /usr/local/src]# wget http://dl.mycat.io/1.6.7.4/Mycat-server-1.6.7.4-release/Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz
[root@txy-server /usr/local/src]# tar -zxvf Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz -C /usr/local/

After decompression, the directory structure Mycat follows:

[root@txy-server /usr/local/mycat]# ll -rh
total 24K
-rwxrwxrwx 1 root root  227 Jan  5 16:41 version.txt
drwxrwxrwx 2 root root 4.0K Jan  5 16:40 logs # 日志文件目录
drwxr-xr-x 2 root root 4.0K Jan  6 20:40 lib  # 库文件目录
drwxrwxrwx 4 root root 4.0K Jan  6 20:40 conf # 配置文件目录
drwxrwxrwx 2 root root 4.0K Oct 22 21:26 catlet
drwxr-xr-x 2 root root 4.0K Jan  6 20:40 bin  # 可执行文件目录
[root@txy-server /usr/local/mycat]#

Mycat create a system account, and change mycatyour user directory, because it is impossible for the program to have root account permissions in the actual environment. Command is as follows:

[root@txy-server /usr/local]# useradd mycat -s /sbin/nologin
[root@txy-server /usr/local]# chown -R mycat:mycat mycat/

Configuration environment variable:

[root@txy-server ~]# vim /etc/profile
export MYCAT_HOME=/usr/local/mycat
export PATH=$PATH:$MYCAT_HOME/bin
[root@txy-server ~]# source /etc/profile

I'm only here because the machine's memory 2G, so the need to modify Mycat startup parameters, the maximum memory instead 1G, the default parameter is 2G. Of course, if your machine's memory enough, then there is no need to change:

[root@txy-server /usr/local]# vim mycat/conf/wrapper.conf
wrapper.java.additional.4=-XX:MaxDirectMemorySize=1G

Start Mycat services:

[root@txy-server ~]# mycat start

View log output successfullyrepresents a successful start:

[root@txy-server ~]# more /usr/local/mycat/logs/wrapper.log |grep successfully
INFO   | jvm 1    | 2020/01/06 21:16:51 | MyCAT Server startup successfully. see logs in logs/mycat.log
[root@txy-server ~]#

After Mycat start listening 8066,9066 and other ports by default, if a firewall is turned on, the port also needs to be released. Open ports command is as follows:

[root@txy-server ~]# firewalld-cmd --zone=public --add-port=${端口号}/tcp --permanent
[root@txy-server ~]# firewalld-cmd --reload

So far, we have successfully built up Mycat service.

Guess you like

Origin blog.51cto.com/zero01/2464817