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

dble基本架构简介

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

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来实现的。底层各个数据库执行完成以后再返回到中间层进行合并、过滤、分组、排序等操做,最终在返回给客户端。mysql

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

服务器 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的,若是没有安装须要安装一下。咱们系统自己已经安装好的环境作一下配置。github

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三个模板文件进行修改。这里简单介绍下这三个文件的做用:sql

文件名称 做用
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)。bash

<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微课堂 opensource.actionsky.com/20191125-db…
2.开源分布式中间件 DBLE 快速入门指南 www.jianshu.com/p/cd5911058…

相关文章
相关标签/搜索