天天看點

資料庫中間件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