資料庫中間件DBLE學習(一) 基礎介紹和快速搭建

  • 2019 年 12 月 18 日
  • 筆記

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 [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