数据库中间件DBLE学习

数据库中间件DBLE学习,第1张

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

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


江湖人送外号MyCat Plus


开源地址



我们首先来看架构图,外部应用通过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快速安装 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 [email protected]:/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微课堂 https://opensource.actionsky.com/20191125-dble/

2.开源分布式中间件 DBLE 快速入门指南 https://www.jianshu.com/p/cd5911058c

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/585837.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-12
下一篇 2022-04-12

发表评论

登录后才能评论

评论列表(0条)

保存