当前位置: 移动技术网 > IT编程>数据库>Mysql > 数据库中间件DBLE学习(一) 基础介绍和快速搭建

数据库中间件DBLE学习(一) 基础介绍和快速搭建

2019年12月26日  | 移动技术网IT编程  | 我要评论

dble基本架构简介

dble是上海爱可生信息技术股份有限公司基于mysql的高可用扩展性的分布式中间件。江湖人送外号mycat plus

dble架构
我们首先来看架构图,外部应用通过nio/aio进行连接操作。这里首先我们得介绍一下nio/aio是什么概念。

  • bio 即传统的blocking i/o,是jdk1.4之前的唯一选择。同步阻塞i/o模式,并发处理能力低。
  • nio 也叫non-blocking i/o,在jdk1.4版本之后发布的新功能,同步非阻塞模式
  • aio 也叫asynchronous i/o,在jdk1.7版本之后才支持,是异步非阻塞i/o模型

可以看到应用发起之后,首先经过nio操作来到sql parse层进行解析。sql解析生产执行计划,然后路由下发到各个底层mysql sharding数据库中执行,这个后台执行的过程也是通过nio/aio来实现的。底层各个数据库执行完成之后再返回到中间层进行合并、过滤、分组、排序等操作,最终在返回给客户端。

对基本架构有所了解后,我们来做快速的搭建。首先进行下载:dble下载地址,最新版本是2.19.09.0,这里选择下载actiontech-dble-2.19.09.0.tar.gz。我们快速搭建的环境和ip如下:

服务器 ip地址 描述
dble服务器 192.168.56.185 dble实例,数据库中间件,负责接收sql进行路由分发
mysql a服务器 192.168.56.181 物理实例a,将会创建db1,db3,db5三个schema
mysql b服务器 192.168.56.182 物理实例b,将会创建db2,db4,db6三个schema

dble搭建

dble快速安装

1.首先需要在dble server服务器配置java1.8的环境变量.

在centos 7中默认是安装了jdk1.8的,如果没有安装需要安装一下。我们系统本身已经安装好的环境做一下配置。

export java_home=/etc/alternatives/jre_1.8.0_openjdk
path=$path:$home/bin:$java_home/bin

[root@mycat bin]# java -version
openjdk version "1.8.0_161"

openjdk runtime environment (build 1.8.0_161-b14)
openjdk 64-bit server vm (build 25.161-b14, mixed mode)

2.在dble server服务器上创建安装目录并解压文件

把安装介质actiontech-dble-2.19.09.0.tar.gz上传到dble server服务器上。

cd /tmp
tar -xvf actiontech-dble-2.19.09.0.tar.gz
mkdir -p /dble
cd /tmp/dele
mv * /dble
cd /dble/conf

接下来需要copy三个模板文件进行修改。这里简单介绍下这三个文件的作用:
| 文件名称 | 作用 |
| --- | --- |
| rule.xml | 对分片的规则进行定义|
| schema.xml | 对具体的分片进行定义,定义table和schema以及datanode之间的关系,指定每个table将要使用哪种类型的分片方法,定义每个datanode的连接信息等等 |
| server.xml | dble服务器相关参数定义,包含dble性能、定时任务、端口、用户配置 |

cp -rp server_template.xml server.xml
cp -rp schema_template.xml schema.xml
cp -rp rule_template.xml rule.xml

3.在两套mysql服务器上配置root用户

为了快捷安装,需要在两台mysql server给root可以远程登录的相关操作权限.

mariadb [(none)]> grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
query ok, 0 rows affected (0.000 sec)

mariadb [(none)]> flush privileges;
query ok, 0 rows affected (0.000 sec

4.在dble server上配置schema.xml文件,主要修改下列地方

这里的datahost是节点名称。我们有两套服务器,需要配置相关writehost的ip地址,然后mysql的用户名和密码(为了简单方便这里暂时使用root)。

<datahost name="datahost1" maxcon="1000" mincon="10" balance="0" switchtype="-1" slavethreshold="100">
    <heartbeat>show slave status</heartbeat>
    <!-- can have multi write hosts -->
    <writehost host="hostm1" url="192.168.56.181:3306" user="root" password="123456">
    </writehost>
</datahost>
<datahost name="datahost2" maxcon="1000" mincon="10" balance="0" switchtype="-1" slavethreshold="100">
    <heartbeat>show slave status</heartbeat>
    <!-- can have multi write hosts -->
    <writehost host="hostm2" url="192.168.56.182:3306" user="root" password="123456">
    </writehost>
</datahost>

5.启动dble

这里通过dble命令就可以启动程序了,启动后,可以查看wrapper.log,显示server startup successfully则成功启动。

[root@mycat logs]# dble start
starting dble-server...

---->wrapper.log日志
status | wrapper  | 2019/12/17 23:25:25 | --> wrapper started as daemon
status | wrapper  | 2019/12/17 23:25:25 | launching a jvm...
info   | jvm 1    | 2019/12/17 23:25:26 | wrapper (version 3.2.3) http://wrapper.tanukisoftware.org
info   | jvm 1    | 2019/12/17 23:25:26 |   copyright 1999-2006 tanuki software, inc.  all rights reserved.
info   | jvm 1    | 2019/12/17 23:25:26 | 
info   | jvm 1    | 2019/12/17 23:25:28 | server startup successfully. dble version is [5.6.29-dble-2.19.09.0-fd62e7a27a561169acabc11df32b2f0d13a0b922-20191121135714]. please see logs in logs/dble.log

6.登录进行验证

接下来我们就可以使用root用户来登录192.168.56.185数据库中间件主机来进行管理了。这里通过181主机远程进行登录,因为185上没安装mysql客户端。

[root@mysql5 ~]# mysql -uroot -p -p8066 -h192.168.56.185 -p123456
welcome to the mariadb monitor.  commands end with ; or \g.
your mysql connection id is 2
server version: 5.6.29-dble-2.19.09.0-fd62e7a27a561169acabc11df32b2f0d13a0b922-20191121135714 dble server (actiontech)

copyright (c) 2000, 2018, oracle, mariadb corporation ab and others.

type 'help;' or '\h' for help. type '\c' to clear the current input statement.

mysql [(none)]> show databases;
+----------+
| database |
+----------+
| testdb   |
| testdb2  |
+----------+
2 rows in set (0.002 sec)
mysql [(none)]> use testdb;
database changed
mysql [testdb]> show tables;
empty set (0.002 sec)

7.创建数据分片

之前我们配置的schema.xml文件中,我们有以下默认的配置。该文件配置了6个数据分片,分别对应不同主机不同实例中的6套schema。

<!-- <datanode name="dn1$0-743" datahost="datahost1" database="db$0-743" /> -->
<datanode name="dn1" datahost="datahost1" database="db_1"/>
<datanode name="dn2" datahost="datahost2" database="db_2"/>
<datanode name="dn3" datahost="datahost1" database="db_3"/>
<datanode name="dn4" datahost="datahost2" database="db_4"/>
<datanode name="dn5" datahost="datahost1" database="db_5"/>
<datanode name="dn6" datahost="datahost2" database="db_6"/>

此时我们需要通过管理账号来进行操作。打开server.xml文件。找到用户这里。第一个定义的用户为管理用户。还有开头的端口配置,默认管理端口是9066。

<user name="man1">
        <property name="password">654321</property>
        <property name="manager">true</property>
        <!-- manager user can't set schema-->
</user>

<!--<property name="serverport">8066</property> -->
<!--<property name="managerport">9066</property> -->

通过管理账号man1和9066端口登录,就可以执行管理命令,这里我们按照schema.xml中datanode的规划创建分片。这里创建很方便,可以支持dn$1-n的写法。

[root@mysql5 ~]# mysql -uman1 -p -p9066 -h192.168.56.185 -p654321
welcome to the mariadb monitor.  commands end with ; or \g.
your mysql connection id is 3
server version: 5.6.29-dble-2.19.09.0-fd62e7a27a561169acabc11df32b2f0d13a0b922-20191121135714 dble server (actiontech)

copyright (c) 2000, 2018, oracle, mariadb corporation ab and others.

type 'help;' or '\h' for help. type '\c' to clear the current input statement.

mysql [(none)]> create database @@datanode='dn$1-6';
query ok, 1 row affected (0.049 sec)

接下来我们可以登录mysql a上进行验证。在a实例中,我们可以看到创建了schema db_1,db_3,db_5。和我们的schema.xml文件中配置结果一致。

[root@mysql5 ~]# mysql -uroot -s /tmp/mysql.sock1 -p
enter password: 
welcome to the mariadb monitor.  commands end with ; or \g.
your mariadb connection id is 190
server version: 10.3.17-mariadb mariadb server

copyright (c) 2000, 2018, oracle, mariadb corporation ab and others.

type 'help;' or '\h' for help. type '\c' to clear the current input statement.

mariadb [(none)]> show databases;
+--------------------+
| database           |
+--------------------+
| db_1               |
| db_3               |
| db_5               |
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
7 rows in set (0.001 sec)

8.创建测试表

在dble的conf目录下面有个配置文件叫template_table.sql,这个文件是提供给我们测试一些测试用例。因为在192.168.56.185上没有安装mysql服务,管理端口9066和服务端口8066实际都是java在监听,我们需要先把这个文件scp到192.168.56.181上。利用181上的mysql程序远程连接185来操作。

[root@mycat conf]# ps -ef | grep mysql
root      3670  1287  0 00:28 pts/0    00:00:00 grep --color=auto mysql
[root@mycat conf]# netstat -anp | grep 8066
tcp6       0      0 :::8066                 :::*                    listen      3432/java           
[root@mycat conf]# netstat -anp | grep 9066
tcp6       0      0 :::9066                 :::*                    listen      3432/java   
[root@mycat conf]# scp template_table.sql root@192.168.56.181:/root

---登录到181上连接管理端口8066执行脚本。
[root@mysql5 ~]# mysql -uroot -p -p8066 -h192.168.56.185 -p123456
welcome to the mariadb monitor.  commands end with ; or \g.
your mysql connection id is 4
server version: 5.6.29-dble-2.19.09.0-fd62e7a27a561169acabc11df32b2f0d13a0b922-20191121135714 dble server (actiontech)

copyright (c) 2000, 2018, oracle, mariadb corporation ab and others.

type 'help;' or '\h' for help. type '\c' to clear the current input statement.

mysql [(none)]> source /root/template_table.sql

---创建完之后就可以登录到testdb查询创建的表,可以看到表的类型,有全局表和分片表。
mysql [testdb]> use testdb;
database changed
mysql [testdb]> show all tables;
+----------------------+----------------+
| tables_in_testdb     | table_type     |
+----------------------+----------------+
| tb_child1            | sharding table |
| tb_child2            | sharding table |
| tb_child3            | sharding table |
| tb_date              | sharding table |
| tb_enum_sharding     | sharding table |
| tb_global1           | global table   |
| tb_global2           | global table   |
| tb_grandson1         | sharding table |
| tb_grandson2         | sharding table |
| tb_hash_sharding     | sharding table |
| tb_hash_sharding_er1 | sharding table |
| tb_hash_sharding_er2 | sharding table |
| tb_hash_sharding_er3 | sharding table |
| tb_hash_string       | sharding table |
| tb_jump_hash         | sharding table |
| tb_mod               | sharding table |
| tb_parent            | sharding table |
| tb_pattern           | sharding table |
| tb_range_sharding    | sharding table |
| tb_single            | sharding table |
| tb_uneven_hash       | sharding table |
+----------------------+----------------+
21 rows in set (0.002 sec)

---接下来可以使用explain来查看分片表的执行情况。这里我不带条件查询tb_mod,可以发现使用了广播sql,会对4个分片进行扫描。为什么会是4个分片,这里我们需要看schema.xml中对表的定义。
<table name="tb_mod" datanode="dn1,dn2,dn3,dn4" rule="rule_mod"/>[dble下载地址](https://github.com/actiontech/dble/releases),
mysql [testdb]> explain select * from tb_mod;
+-----------+----------+----------------------+
| data_node | type     | sql/ref              |
+-----------+----------+----------------------+
| dn1       | base sql | select * from tb_mod |
| dn2       | base sql | select * from tb_mod |
| dn3       | base sql | select * from tb_mod |
| dn4       | base sql | select * from tb_mod |
+-----------+----------+----------------------+
4 rows in set (0.006 sec)
如果我查询id=2,就会通过分片键进行查询,这里是取模算法,2是放在dn3分片上的。
mysql [testdb]> explain select * from tb_mod where id=2;
+-----------+----------+---------------------------------+
| data_node | type     | sql/ref                         |
+-----------+----------+---------------------------------+
| dn3       | base sql | select * from tb_mod where id=2 |
+-----------+----------+---------------------------------+
1 row in set (0.054 sec)

结尾语:

dble还是比较好安装的,但是它的概念和配置xml是有点繁琐的。需要细心一些。

参考文档

1.dble微课堂 2.开源分布式中间件 dble 快速入门指南

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网