Logo
场景一:数据分片

场景一:数据分片 #

此场景利用 SphereEx- Boot(管理 SphereEx- DBPlusEngine 集群的命令行工具)安装 SphereEx- DBPlusEngine 集群,通过 DistSQL 命令行方式快速实现数据分片。

数据分片场景仅限于本地快速构建一个 SphereEx- DBPlusEngine 集群,并连接 MySQL 数据库,实现数据分片的场景,不包含其他组件,以及数据库的安装。

操作步骤 #

操作步骤

准备资源环境 #

实例IP 地址服务端口备注
1SphereEx-Boot127.0.0.1/SphereEx- Boot 工具
2SphereEx-DBPlusEngine127.0.0.13308SphereEx-DBPlusEngine 集群
3ZooKeeper127.0.0.12181ZooKeeper
4MySQL 8.0127.0.0.113306用于分片的数据库
(需提前准备)

说明:操作时需要替换为实际环境中的 IP 地址、端口。

安装 SphereEx-Boot #

安装准备 #

安装 SphereEx-Boot 前,确保操作系统已经安装了如下软件:

  • sshpass 1.0.0+

  • Python 2.7 或者 Python 3.5+

  • pip 20.0.0+

安装 SphereEx-Boot #

  1. 执行如下命令,安装 SphereEx-Boot。
# pip install sphereex‑boot‑1.1.0.tar.gz
 Processing ./sphereex‑boot‑1.1.0.tar.gz
 Requirement already satisfied: ansible<=2.10.7,>=2.8.0 in /usr/lib/python2.7/site-packages (from sphereex-boot==1.1.0) (2.10.7)
 ······
  Building wheel for sphereex-boot (setup.py) ... done
  Created wheel for spex: filename=sphereex_boot-1.1.0-py2-none-any.whl size=212249 sha256=5de4a3b4f347da57697ad942f74284be38ecf12e5e0d1fefda06e5e4282c22eb
  Stored in directory: /root/.cache/pip/wheels/e5/c4/d7/64a96c1f48ffabc68df6aa164beb7a93a1ad4d81aba4b751d7

Successfully built sphereex-boot

Installing collected packages: sphereex-boot
Successfully installed sphereex‑boot‑1.1.0

查看 SphereEx-Boot 安装结果 #

执行命令查看 SphereEx-Boot 版本,如果输出的版本为当前安装的版本,说明安装成功。

# spex --version
Version 1.1.0

使用 SphereEx-Boot 创建集群 #

本章节介绍使用 SphereEx-Boot 如何快速创建 SphereEx- DBPlusEngine 集群。

操作步骤 #

安装集群

安装集群 #

  1. 集群规划
集群名称IP 地址账号密码
demo127.0.0.1rootroot

说明:操作时需要替换为自己的 IP 地址、登录账号和密码。

  1. 上传安装包
  • 使用 MySQL

将 mysql-connector-java-8.jar、sphereex-dbplusengine-proxy-1.2.0.tar.gz 、apache-zookeeper-3.7.0-bin.tar.gz 安装包上传至 /root/demo 路径下。

说明:此处实际的安装包名称,会按照版本变化有所改变,按照实际情况上传相应的安装包即可。

# mkdir -p /root/demo 
# cd /root/demo
# ll
mysql-connector-java-8.jar
sphereex-dbplusengine-proxy-1.2.0.tar.gz
apache-zookeeper-3.7.0-bin.tar.gz
  • 使用 PostgreSQL

使用任何 PostgreSQL 的客户端连接。如: psql -U root -h 127.0.0.1 -p 3308。

  • 使用 openGauss

将以 org.opengauss 包名为前缀的 openGauss 的 JDBC 驱动程序复制至目录 /root/demo 路径下。

使用任何 openGauss 的客户端连接。如: gsql -U root -h 127.0.0.1 -p 3308。

  1. 获取并配置 zoo.cfg 文件

zoo.cfg 是 ZooKeeper 的配置文件,系统根据此配置文件中的信息,安装 Zookeeper 软件。

# tar -xzvf apache-zookeeper-3.7.0-bin.tar.gz 
# cd apache-zookeeper-3.7.0-bin/conf
# cp zoo_sample.cfg /root/demo/zoo.cfg
# cd /root/demo
# ls zoo.cfg
zoo.cfg

配置 zoo.cfg 文件,将数据目录和端口参数值替换,其他参数保持默认。

# vi zoo.cfg
dataDir=/root/demo/zk
clientPort=2181  
  1. 获取并配置 global.yaml 文件

需要获取 conf 文件,其中此步骤需要配置的是其中的 global.yaml 文件。

global.yaml 文件是 DBPlusEngine 集群的配置文件,其中包括 Zookeeper 的相关信息、集群用户的信息,以及事物相关设置等。

# tar -zxvf sphereex-dbplusengine-proxy-1.2.0.tar.gz
# cd sphereex-dbplusengine-proxy-1.2.0
# cp -r conf /root/demo/conf
# cd /root/demo
# ls conf/global.yaml 
conf/global.yaml

配置 global.yaml 文件, 将以下参数的注释去掉。

# vi conf/global.yaml
mode:
  type: Cluster
  repository:
    type: ZooKeeper
    props:
      namespace: governance_ds
      server-lists: localhost:2181
      retryIntervalMilliseconds: 500
      timeToLiveSeconds: 60
      maxRetries: 3
      operationTimeoutMilliseconds: 500
  
rules:
  - !AUTHORITY
    users:
      - user: root@%
        password: root
      - user: sharding
        password: sharding
    privilege:
      type: ALL_PERMITTED
  - !TRANSACTION
    defaultType: XA
    providerType: Atomikos
  1. 获取并配置安装集群文件

生成配置文件。

# spex config template --type full --output ./
# ls cluster-template.yaml
cluster-template.yaml

集群拓扑文件初始化,按照实际环境修改集群拓扑文件,修改信息如下:

# vi cluster-template.yaml
cluster_name: demo
install_user: root
install_password: 'root'
 
proxy:
  version: '1.2.0'
  install_dir: /root/demo/engine
  conf_dir: /root/demo/conf
  file: /root/demo/sphereex-dbplusengine-proxy-1.2.0.tar.gz
# agent_conf_file: 
  depend_files:
   - /root/demo/mysql-connector-java-8.jar
  port: 3308
  servers:
   - host: 127.0.0.1
 
zookeeper:
  version: '3.7.0'
  install_dir: /root/demo/zk
  data_dir: /root/demo/zk
  conf_file: /root/demo/zoo.cfg
  file: /root/demo/apache-zookeeper-3.7.0-bin.tar.gz
  port: 2181
  servers:
   - host: 127.0.0.1
     myid: 1
  1. 检查集群拓扑文件
# spex config check -f cluster-template.yaml 
Proxy: The host:127.0.0.1 port:3308 missing 'agent_conf_file' value //忽略此报错即可,此处不需要agent。
No error with ZooKeeper
  1. 添加集群配置文件到 SphereEx-Boot 工具管理环境
#  spex config add -f cluster-template.yaml 
Add cluster successfully
  1. 安装集群
# spex cluster install --name demo
check proxy install dir exist!          
Completed......
create install directory          
127.0.0.1:3308 => success
install proxy          
127.0.0.1:3308 => success
copying shell file          
127.0.0.1:3308 => success
copying config file          
127.0.0.1:3308 => success
copying agent config file          
skipped host:127.0.0.1 item:None
copying depend file          
127.0.0.1:3308 => success
Completed......
Operation ZooKeeper
check ZooKeeper install dir exist!          
Completed......
Operation ZooKeeper
create ZooKeeper install directory          
127.0.0.1:2181 => success
create ZooKeeper data directory          
127.0.0.1:2181 => success
install ZooKeeper          
127.0.0.1:2181 => success
copy ZooKeeper config file          
127.0.0.1:2181 => success
create myid          
127.0.0.1:2181 => success
Completed......

启动集群 #

# spex cluster start --name demo
Operation ZooKeeper
start ZooKeeper          
127.0.0.1:2181 => success
/usr/bin/java
Starting zookeeper ... STARTED
ZooKeeper JMX enabled by default
Using config: /root/demo/zk/apache-zookeeper-3.7.0-bin/bin/../conf/zoo.cfg
Completed......
start proxy          
/usr/bin/java
we find java version: java8, full_version=1.8.0_322, full_path=/usr/bin/java
The port is 3308
The classpath is /root/demo/engine/sphereex-dbplusengine-proxy-1.2.0/conf:.:/root/demo/engine/sphereex-dbplusengine-proxy-1.2.0/lib/*:/root/demo/engine/sphereex-dbplusengine-proxy-1.2.0/ext-lib/*
main class org.apache.shardingsphere.proxy.Bootstrap 3308 /root/demo/engine/sphereex-dbplusengine-proxy-1.2.0/conf 0.0.0.0 false
Starting the SphereEx-DB-Plus-Engine ... PID: 10335
Please check the STDOUT file: /root/demo/engine/sphereex-dbplusengine-proxy-1.2.0/logs/stdout.log
Completed......

查看集群运行状态

# spex cluster status --name demo 
proxy status          
127.0.0.1:3308 => success
PID:10335 PORT:3308 %CPU:16.9 %MEM:0.2 START:14:09 TIME:0:01
Results summary
+-----------+------+------+------+------+-------+------+
|    HOST   | PORT | PID  | %CPU | %MEM | START | TIME |
+-----------+------+------+------+------+-------+------+
| 127.0.0.1 | 3308 |10335 | 16.9 | 0.2  | 14:09 | 0:01 |
+-----------+------+------+-------+------+------+------+
Completed......
Operation ZooKeeper
ZooKeeper status          
127.0.0.1:2181 => success
/usr/bin/java
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: standalone
ZooKeeper JMX enabled by default
Using config: /root/demo/zk/apache-zookeeper-3.7.0-bin/bin/../conf/zoo.cfg
Results summary
+-----------+------+------------------+
|    HOST   | PORT |      STATUS      |
+-----------+------+------------------+
| 127.0.0.1 | 2181 | Mode: standalone |
+-----------+------+------------------+

登录集群 #

本章节介绍如何连接已经创建好的集群

# mysql -uroot -p -P3308 -h127.0.0.1
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.22 sphereex-dbplusengine-proxy-1.2.0 
 
Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql>

注册 License #

  1. 上传 License

将获得的 License 文件上传至默认的存储目录:/demo/conf。

# scp -r sphere.license  root@10.10.10.1:/root/demo/conf
  1. 查看 License-key 内容。
# cat /root/demo/conf/sphere.license 
Ic5OXgAAACMAAAACAAAACwAAAAxjcmVEaWdlc3RTSEEtNTEy
  1. 注册 License。

登录 SphereEx- DBPlusEngine 集群,注册 License。

# mysql -uroot -p -P3308 -h127.0.0.1
sql> REGISTER LICENSE '[Ic5OXgAAACMAAAACAAAACwAAAAxjcmVEaWdlc3RTSEEtNTEy]';
Query OK, 0 rows affected (1.29 sec)
  1. 查看 License 是否生效。
sql> SHOW LICENSE INFO;
| company_name | instance_count | resource_count | expiry_date         | generation_data     | status | license 
| sphereextest | 8              | 8              | 2023-01-01 00:00:00 | 2022-09-08 18:00:00 | valid  | Ic5OXgAAACMAAAACAAAACwAAAAxjcmVEaWdlc3RTSEEtNTEy

数据分片 #

本章节将介绍使用 DistSQL(命令工具)如何创建数据分片。也可以使用 SphereEx-Console(SphereEx 企业数据服务可视化平台) 实现数据分片功能。

操作步骤 #

数据分片操作步骤

创建分片 #

  1. 创建分布式数据库

a.创建数据源

登陆 MySQL 数据库: 127.0.0.1/13306, 创建数据库 test01 和 test02,如:

# mysql -uroot -proot -P13306 -h127.0.0.1
sql> CREATE DATABASE test01;
Query OK, 1 row affected (0.00 sec)
sql> CREATE DATABASE test02;
Query OK, 1 row affected (0.00 sec)
sql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test01             |
| test02             |
+--------------------+
5 rows in set (0.01 sec)

b.创建逻辑库

登陆 Proxy

# mysql -uroot -proot -P3308 -h127.0.0.1
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 37
Server version: 5.7.22 SphereEx-DBPlusEngine-Proxy 1.2.0 MySQL Community Server (GPL)
 
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
sql> 

创建逻辑库

sql>  CREATE DATABASE testdb;
Query OK, 0 rows affected (0.65 sec)

查看逻辑库

sql> SHOW DATABASES;
+--------------------+
| schema_name        |
+--------------------+
| mysql              |
| information_schema |
| performance_schema |
| sys                |
| testdb             |
+--------------------+
5 rows in set (0.00 sec)

c.添加数据源

切换至逻辑库 testdb

sql> USE testdb;
Database changed

添加数据源

sql> REGISTER STORAGE UNIT test01 (
    ->     HOST="127.0.0.1",
    ->     PORT=13306,
    ->     DB="test01",
    ->     USER="root",
    ->     PASSWORD="root"
    -> ),test02 (
    ->     HOST="127.0.0.1",
    ->     PORT=13306,
    ->     DB="test02",
    ->     USER="root",
    ->     PASSWORD="root"
    -> );
Query OK, 0 rows affected (0.09 sec)

d.查看数据源信息

sql> SHOW STORAGE UNITS\G
*************************** 1. row ***************************
                           name: test02
                           type: MySQL
                           host: 127.0.0.1
                           port: 13306
                             db: test02
connection_timeout_milliseconds: 30000
      idle_timeout_milliseconds: 60000
      max_lifetime_milliseconds: 2100000
                  max_pool_size: 50
                  min_pool_size: 1
                      read_only: false
               other_attributes: {"dataSourceProperties":{"cacheServerConfiguration":"true","elideSetAutoCommits":"true","useServerPrepStmts":"true","cachePrepStmts":"true","useSSL":"false","rewriteBatchedStatements":"true","cacheResultSetMetadata":"false","useLocalSessionState":"true","maintainTimeStats":"false","prepStmtCacheSize":"8192","tinyInt1isBit":"false","prepStmtCacheSqlLimit":"2048","serverTimezone":"UTC","netTimeoutForStreamingResults":"0","zeroDateTimeBehavior":"round"},"healthCheckProperties":{},"initializationFailTimeout":1,"validationTimeout":5000,"leakDetectionThreshold":0,"registerMbeans":false,"allowPoolSuspension":false,"autoCommit":true,"isolateInternalQueries":false}
*************************** 2. row ***************************
                           name: test01
                           type: MySQL
                           host: 127.0.0.1
                           port: 13306
                             db: test01
connection_timeout_milliseconds: 30000
      idle_timeout_milliseconds: 60000
      max_lifetime_milliseconds: 2100000
                  max_pool_size: 50
                  min_pool_size: 1
                      read_only: false
               other_attributes: {"dataSourceProperties":{"cacheServerConfiguration":"true","elideSetAutoCommits":"true","useServerPrepStmts":"true","cachePrepStmts":"true","useSSL":"false","rewriteBatchedStatements":"true","cacheResultSetMetadata":"false","useLocalSessionState":"true","maintainTimeStats":"false","prepStmtCacheSize":"8192","tinyInt1isBit":"false","prepStmtCacheSqlLimit":"2048","serverTimezone":"UTC","netTimeoutForStreamingResults":"0","zeroDateTimeBehavior":"round"},"healthCheckProperties":{},"initializationFailTimeout":1,"validationTimeout":5000,"leakDetectionThreshold":0,"registerMbeans":false,"allowPoolSuspension":false,"autoCommit":true,"isolateInternalQueries":false}
2 rows in set (0.00 sec)
 

至此,分布式数据库创建完成。

  1. 创建分片表规则

选择逻辑库

sql> USE testdb;
Database changed

创建数据分片规则

sql> CREATE SHARDING TABLE RULE t_user(
    ->     STORAGE_UNITS(test01,test02),
    ->     SHARDING_COLUMN=user_id,
    ->     TYPE(NAME="hash_mod",PROPERTIES("sharding-count"=4))
    ->     );
Query OK, 0 rows affected (0.07 sec)

查看数据分片规则

sql> SHOW SHARDING TABLE RULE t_user\G
*************************** 1. row ***************************
                            table: t_user
                actual_data_nodes: 
              actual_data_sources: test01,test02
           database_strategy_type: 
         database_sharding_column: 
 database_sharding_algorithm_type: 
database_sharding_algorithm_props: 
              table_strategy_type: STANDARD
            table_sharding_column: user_id
    table_sharding_algorithm_type: hash_mod
   table_sharding_algorithm_props: sharding-count=4
              key_generate_column: 
               key_generator_type: 
              key_generator_props: 
                    auditor_types: 
               allow_hint_disable: 
1 row in set (0.01 sec)
  1. 创建表并插入数据

a.创建表

sql> CREATE TABLE `t_user` (
    ->  `user_id` int NOT NULL,
    ->  `order_id` int NOT NULL,
    ->  `status` varchar(45) DEFAULT NULL,
    ->  PRIMARY KEY (`user_id`)
    -> );
Query OK, 0 rows affected (0.28 sec)

b.插入数据

sql> INSERT INTO t_user VALUES
    -> (1,1,'active'),
    -> (2,2,'active'),
    -> (3,3,'active'),
    -> (4,4,'active');
Query OK, 4 rows affected (0.10 sec)

c.预览分片信息

sql> PREVIEW SELECT * FROM t_user;
+------------------+---------------------------------------------------------+
| data_source_name | actual_sql                                              |
+------------------+---------------------------------------------------------+
| test01           | SELECT * FROM t_user_0 UNION ALL SELECT * FROM t_user_2 |
| test02           | SELECT * FROM t_user_1 UNION ALL SELECT * FROM t_user_3 |
+------------------+---------------------------------------------------------+
2 rows in set (0.04 sec)
  1. 查看分片结果

登陆数据库 test01 ,查看实际分片数据

# mysql -uroot -proot -P13306 -h127.0.0.1
sql> use test01;
sql> select * from t_user_0;
+---------+----------+--------+
| user_id | order_id | status |
+---------+----------+--------+
|       4 |        4 | active |
+---------+----------+--------+
1 row in set (0.00 sec)
sql> select * from t_user_2;
+---------+----------+--------+
| user_id | order_id | status |
+---------+----------+--------+
|       2 |        2 | active |
+---------+----------+--------+
1 row in set (0.00 sec)

登陆数据库 test02 ,查看实际分片数据

sql> use test02;
sql> select * from t_user_1;
+---------+----------+--------+
| user_id | order_id | status |
+---------+----------+--------+
|       1 |        1 | active |
+---------+----------+--------+
1 row in set (0.00 sec)
sql> select * from t_user_3;
+---------+----------+--------+
| user_id | order_id | status |
+---------+----------+--------+
|       3 |        3 | active |
+---------+----------+--------+
1 row in set (0.00 sec)