Distributed database middleware MyCat do it!

About MyCat groundwork has written three articles on:

  1. MySQL can only do small projects? Song Ge to say a few words!
  2. The North Sea There Data, it called Kun, Kun great, a MySQL does not fit!
  3. What? Tomcat actually can be considered middleware?

Today finally we meet our big Boss played!

MyCat Profile

We mentioned in previous articles, if the large amount of data, we need the data sub-library sub-table after points finish, originally present data in a database, now there are multiple databases, like this:

So this time MyCat's role is distributed database middleware!

MyCat is an open source distributed database middleware that implements the MySQL protocol, the developer's eyes, he is a proxy database, we can even use MySQL client tools and command-line access MyCat.

MyCat now not only supports MySQL, and also supports MSSQL, Oracle, DB2, PostgreSQL and other mainstream database. Even such as MongoDB NoSQL is also supported.

Getting Started

Build separate read and write

Engage MyCat, generally must first build a good read and write separation of MySQL, MySQL to read and write separation can refer to this article before Song Ge:

  1. Improve performance, MySQL environment separate read and write structures (II)

MyCat installation

surroundings:

  • CentOS7
  • JDK1.8

MyCat developed in Java, therefore, run MyCat, must have a Java environment, configure the Java Runtime Environment that is relatively easy, there are many online information, I will not described in detail.

After installing the Java environment, first download MyCat:

wget http://dl.mycat.io/1.6.7.1/Mycat-server-1.6.7.1-release-20190213150257-linux.tar.gz

Once downloaded, the downloaded file to decompress it.

tar -zxvf Mycat-server-1.6.7.1-release-20190213150257-linux.tar.gz

After decompression is successful, there will be a mycatdirectory, enter the mycat/confdirectory, to mycatconfigure:

First configure the schema.xmlfile:

  1. 首先在 schema 中指定逻辑库的名字,逻辑库是指 MyCat 中的库,这个库不存储数据,数据存储在 MySQL 中的物理库中。
  2. 逻辑库中配置逻辑表,配置逻辑表时,需要指定 dataNode 节点, dataNode 就是指数据库存储的位置
  3. 配置 dataNodedataNode 指定 dataHost 和物理库的名字。
  4. dataHost 则配置 MySQL 的主机和从机的位置,登录密码等。主机和从机都可以配置多个。

配置完 schema.xml 后 ,接下来配置 server.xml。

server.xml 中主要配置 MyCat 的登录用户名和密码,以及需要操作的逻辑库。

配置完成后,接下来就可以启动 MyCat 了 。

执行 MyCat 解压目录下的 bin 目录下的 mycat 命令,可以启动 MyCat

./bin/mycat start

如果启动后,提示无法创建 mycat.pid 文件,就自己手动创建一个 mycat.pid 文件。启动成功之后,就可以在本地连接 MyCat 了,连接方式和 MySQL 一样,唯一的区别在于端口号不同。

在连接 MyCat 之前,先在 MySQL 物理库中创建 db1db2 以及 db3 三个数据库。

使用 SQLyog 连接:

也可以在 cmd 命令行登录 MyCat

登录成功后 ,在 MyCat 的窗口中,执行如下命令,创建表:

create table t_user (id integer primary key,username varchar(255))

执行成功后,我们会发现物理库中出现了相应的表。
接下来,手动往各个物理库的物理表中存储一条数据,然后在 MyCat 窗口中查询:

这样就可以查询到 三个库中的三个表中的数据。

问题分析

整个过程不难,但是有的小伙伴在第一次配置的过程中还是容易出错,因此我这里还是来说两句,出错了要如何定位。

一般来说,配置 MyCat 出错,问题可能发生在两个阶段。第一个阶段就是客户端连接 MyCat 出错,第二个阶段就是 MyCat 连接 MySQL 出错。

无论你是使用 SQLyog 还是 Navicat ,我们在连接数据库的过程中,都可以先测试连接,很多人卡在这一步。

如果在测试连接的时候就连接不通,说明是 MyCat 的问题,这个时候检查步骤如下:

  1. 首先当然是查看日志信息,看能不能找出端倪
  2. 通过 jps 命令查看 mycat 是否成功启动
  3. 检查 server.xml 中配置是否正确,用户名密码是否输入正确

这是第一种可能的问题,第二种问题就是测试连接没问题,但是测试完后,却连接不上。反映到 Navicat 上,就是测试连接没问题,测完之后,点击连接名要打开连接时,Navicat 就崩了,出现这个问题一般是 MyCat 在连接 MySQL 出问题了,这个时候就要去检查 schema.xml 文件中关于 MySQL 主机和从机的配置是否正确,数据库地址是否正确,用户名密码是否正确。

结语

好了,本文主要简单介绍了下 MyCat 的安装问题,下篇文章我们来看 MyCat 中的分片规则问题。

参考资料:

  1. MyCat 官方文档

关注公众号【江南一点雨】,专注于 Spring Boot+微服务以及前后端分离等全栈技术,定期视频教程分享,关注后回复 Java ,领取松哥为你精心准备的 Java 干货!

Guess you like

Origin www.cnblogs.com/lenve/p/11100766.html