From b813c9751ee9a2587f7949c3802ad33155c01f68 Mon Sep 17 00:00:00 2001 From: yangj1211 Date: Mon, 28 Oct 2024 11:31:47 +0800 Subject: [PATCH] add doc of pub-sub --- .../Publish-Subscribe/pub-sub-overview.md | 442 +++++++----------- .../alter-publication.md | 45 +- .../create-publication.md | 23 +- .../create-subscription.md | 102 ++-- .../SHOW-Statements/show-publications.md | 12 +- .../SHOW-Statements/show-subscriptions.md | 63 ++- docs/MatrixOne/Reference/System-tables.md | 16 + 7 files changed, 289 insertions(+), 414 deletions(-) diff --git a/docs/MatrixOne/Develop/Publish-Subscribe/pub-sub-overview.md b/docs/MatrixOne/Develop/Publish-Subscribe/pub-sub-overview.md index 69a98b06ed..8c57007b64 100644 --- a/docs/MatrixOne/Develop/Publish-Subscribe/pub-sub-overview.md +++ b/docs/MatrixOne/Develop/Publish-Subscribe/pub-sub-overview.md @@ -46,7 +46,7 @@ ### 发布/订阅数据范围 - 一个**发布**只能与单一数据库关联。 -- 发布和订阅只在数据库级别实现,目前还不支持直接进行表级别的发布和订阅。 +- 支持数据库级别和表级别的发布和订阅。 - **订阅端**对**订阅库**只具备读取权限。 - 若**发布端(Pub)**调整了发布的分享范围,那些不在新范围内的**订阅端(Sub)**如果已经创建了订阅库,那么对这个**订阅库**的访问将无效。 - 若**发布端(Pub)**修改了发布内容,那**订阅端(Sub)**无需额外操作,即可看到更新 @@ -54,327 +54,203 @@ - 若**发布端(Pub)**删除了**发布**,但订阅库中的对应对象仍存在,此时**订阅端(Sub)**访问这个对象会触发错误,需要由**订阅端(Sub)**删除对应的**订阅**。 - 若**发布端(Pub)**删除了**发布对象**,但在订阅库中的对应对象仍然存在,此时**订阅端(Sub)**访问这个对象会触发错误,需要由**订阅端(Sub)**删除对应的**订阅对象**。 -### 发布订阅示例 +## 发布订阅示例 -本章节将给出一个示例,介绍当前在 MatrixOne 集群中,存在 3 个租户,sys、acc1 与 acc2,按照操作顺序对三个租户进行操作: +假设有一家跨地区的零售公司,中心仓库数据库需要发布库存和商品价格变动,各分支机构数据库订阅这些变动,确保库存和价格信息在各分支系统中保持同步。 -![](https://community-shared-data-1308875761.cos.ap-beijing.myqcloud.com/artwork/docs/develop/pub-sub/data-share.png) +```sql +drop account if exists acc1; +create account acc1 admin_name = 'test_account' identified by '111'; +drop account if exists acc2; +create account acc2 admin_name = 'test_account' identified by '111'; +``` -1. **发布者**:sys 租户创建数据库 sub1 与表 t1,并发布 pub1: - - ```sql - create database sub1; - create table sub1.t1(a int,b int); - create publication pub1 database sub1; - mysql> show publications; - +-------------+----------+---------------------+-------------+-------------+----------+ - | publication | database | create_time | update_time | sub_account | comments | - +-------------+----------+---------------------+-------------+-------------+----------+ - | pub1 | sub1 | 2024-04-23 10:28:15 | NULL | * | | - +-------------+----------+---------------------+-------------+-------------+----------+ - 1 row in set (0.01 sec) - ``` - -2. **订阅者**:acc1 和 acc2 都创建订阅库 syssub1,于是得到共享的数据表 t1: - - ```sql - -- all 选项可以看到所有有权限的订阅,未订阅的 sub_time, sub_name 为 null,不加 all 只能看到已订阅的发布信息。 - mysql> show subscriptions all; - +----------+-------------+--------------+---------------------+----------+----------+ - | pub_name | pub_account | pub_database | pub_time | sub_name | sub_time | - +----------+-------------+--------------+---------------------+----------+----------+ - | pub1 | sys | sub1 | 2024-04-23 10:28:15 | NULL | NULL | - +----------+-------------+--------------+---------------------+----------+----------+ - 1 row in set (0.01 sec) - - -- acc1 和 acc2 创建订阅库的 sql 语句一致,此处不做赘述 - create database syssub1 from sys publication pub1; - use syssub1; - - mysql> show subscriptions; - +----------+-------------+--------------+---------------------+----------+---------------------+ - | pub_name | pub_account | pub_database | pub_time | sub_name | sub_time | - +----------+-------------+--------------+---------------------+----------+---------------------+ - | pub1 | sys | sub1 | 2024-04-23 10:28:15 | syssub1 | 2024-04-23 10:35:13 | - +----------+-------------+--------------+---------------------+----------+---------------------+ - 1 row in set (0.00 sec) - - mysql> show tables; - +--------------------+ - | Tables_in_syssub1 | - +--------------------+ - | t1 | - +--------------------+ - 2 rows in set (0.02 sec) - ``` - -3. **发布者**:sys 租户创建数据表 t2: +1. 创建中心仓库数据库并初始化数据 + + 在 `sys` 租户下创建一个中心仓库数据库,包含库存表和商品价格表,并插入一些测试数据用于验证功能。 ```sql - create table sub1.t2(a text); + -- 创建中心仓库数据库 + create database central_warehouse_db; + use central_warehouse_db; + + -- 创建库存表 + CREATE TABLE inventory ( + product_id INT PRIMARY KEY, + product_name VARCHAR(100), + stock_quantity INT + ); + + -- 创建商品价格表 + CREATE TABLE products ( + product_id INT PRIMARY KEY, + product_name VARCHAR(100), + price DECIMAL(10, 2) + ); + + -- 向库存表插入初始数据 + INSERT INTO inventory (product_id, product_name, stock_quantity) VALUES + (1, 'Laptop', 100), + (2, 'Smartphone', 200), + (3, 'Tablet', 150); + + -- 向商品价格表插入初始数据 + INSERT INTO products (product_id, product_name, price) VALUES + (1, 'Laptop', 999.99), + (2, 'Smartphone', 599.99), + (3, 'Tablet', 399.99); ``` -4. **订阅者**:acc1 和 acc2 得到共享的数据表 t1 和 t2: - - ```sql - use syssub1; - mysql> show tables; - +-------------------+ - | Tables_in_syssub1 | - +-------------------+ - | t1 | - | t2 | - +-------------------+ - 2 rows in set (0.01 sec) - ``` +2. 配置发布 -5. **发布者**:sys 租户创建数据库 sub2 与表 t1,并发布 pub2 给租户 acc1 + 在中心仓库数据库中创建一个数据库级别的发布,将所有表的数据更新发布给租户 `acc1`。如果某分支机构仅关心商品价格的变化,则可以创建一个表级别的发布,仅将商品价格表的更新发布给租户 `acc2`。 ```sql - create database sub2; - create table sub2.t1(a float); - create publication pub2 database sub2 account acc1; + create publication db_warehouse_pub database central_warehouse_db account acc1; + create publication tab_products_pub database central_warehouse_db table products account acc2; ``` -6. **订阅者**:acc1 和 acc2 都创建订阅库 syssub2,acc1 得到共享的数据表 t1;acc2 创建订阅库 syssub2 失败: - - - acc1 +3. 在分支机构租户中分别订阅发布 ```sql - mysql> show subscriptions all; - +----------+-------------+--------------+---------------------+----------+---------------------+ - | pub_name | pub_account | pub_database | pub_time | sub_name | sub_time | - +----------+-------------+--------------+---------------------+----------+---------------------+ - | pub1 | sys | sub1 | 2024-04-23 10:28:15 | syssub1 | 2024-04-23 10:30:43 | - | pub2 | sys | sub2 | 2024-04-23 10:40:54 | NULL | NULL | - +----------+-------------+--------------+---------------------+----------+---------------------+ - 2 rows in set (0.01 sec) + -- 在 acc1 中订阅 db_warehouse_pub + create database db_warehouse_sub from sys publication db_warehouse_pub; - create database syssub2 from sys publication pub2; - use syssub2; + mysql> show subscriptions; + +------------------+-------------+----------------------+------------+-------------+---------------------+------------------+---------------------+--------+ + | pub_name | pub_account | pub_database | pub_tables | pub_comment | pub_time | sub_name | sub_time | status | + +------------------+-------------+----------------------+------------+-------------+---------------------+------------------+---------------------+--------+ + | db_warehouse_pub | sys | central_warehouse_db | * | | 2024-10-15 11:58:04 | db_warehouse_sub | 2024-10-15 11:59:47 | 0 | + +------------------+-------------+----------------------+------------+-------------+---------------------+------------------+---------------------+--------+ + 1 row in set (0.01 sec) - mysql> show subscriptions all; - +----------+-------------+--------------+---------------------+----------+---------------------+ - | pub_name | pub_account | pub_database | pub_time | sub_name | sub_time | - +----------+-------------+--------------+---------------------+----------+---------------------+ - | pub2 | sys | sub2 | 2024-04-23 10:40:54 | syssub2 | 2024-04-23 10:42:31 | - | pub1 | sys | sub1 | 2024-04-23 10:28:15 | syssub1 | 2024-04-23 10:30:43 | - +----------+-------------+--------------+---------------------+----------+---------------------+ - 2 rows in set (0.01 sec) + use db_warehouse_sub; mysql> show tables; - +--------------------+ - | Tables_in_syssub2 | - +--------------------+ - | t1 | - +--------------------+ - 2 rows in set (0.02 sec) - ``` + +----------------------------+ + | Tables_in_db_warehouse_sub | + +----------------------------+ + | inventory | + | products | + +----------------------------+ + 2 rows in set (0.01 sec) - - acc2 + mysql> select * from inventory; + +------------+--------------+----------------+ + | product_id | product_name | stock_quantity | + +------------+--------------+----------------+ + | 1 | Laptop | 100 | + | 2 | Smartphone | 200 | + | 3 | Tablet | 150 | + +------------+--------------+----------------+ + 3 rows in set (0.01 sec) + + mysql> select * from products; + +------------+--------------+--------+ + | product_id | product_name | price | + +------------+--------------+--------+ + | 1 | Laptop | 999.99 | + | 2 | Smartphone | 599.99 | + | 3 | Tablet | 399.99 | + +------------+--------------+--------+ + 3 rows in set (0.01 sec) + + -- 在 acc2 中订阅 tab_products_pub + create database tab_products_sub from sys publication tab_products_pub; - ```sql - -- acc2 看不到 pub2,因为没有订阅权限 - mysql> show subscriptions all; - +----------+-------------+--------------+---------------------+----------+---------------------+ - | pub_name | pub_account | pub_database | pub_time | sub_name | sub_time | - +----------+-------------+--------------+---------------------+----------+---------------------+ - | pub1 | sys | sub1 | 2024-04-23 10:28:15 | syssub1 | 2024-04-23 10:35:13 | - +----------+-------------+--------------+---------------------+----------+---------------------+ + mysql> show subscriptions; + +------------------+-------------+----------------------+------------+-------------+---------------------+------------------+---------------------+--------+ + | pub_name | pub_account | pub_database | pub_tables | pub_comment | pub_time | sub_name | sub_time | status | + +------------------+-------------+----------------------+------------+-------------+---------------------+------------------+---------------------+--------+ + | tab_products_pub | sys | central_warehouse_db | products | | 2024-10-15 11:58:04 | tab_products_sub | 2024-10-15 13:59:22 | 0 | + +------------------+-------------+----------------------+------------+-------------+---------------------+------------------+---------------------+--------+ 1 row in set (0.01 sec) - mysql> create database syssub2 from sys publication pub2; - ERROR 20101 (HY000): internal error: the account acc2 is not allowed to subscribe the publication pub2 - ``` - -7. **发布者**:sys 租户修改发布 pub2 给全部租户: - - ```sql - alter publication pub2 account all; - mysql> show publications; - +-------------+----------+---------------------+---------------------+-------------+----------+ - | publication | database | create_time | update_time | sub_account | comments | - +-------------+----------+---------------------+---------------------+-------------+----------+ - | pub2 | sub2 | 2024-04-23 10:40:54 | 2024-04-23 10:47:53 | * | | - | pub1 | sub1 | 2024-04-23 10:28:15 | NULL | * | | - +-------------+----------+---------------------+---------------------+-------------+----------+ - 2 rows in set (0.00 sec) - ``` - -8. **订阅者**:acc2 创建订阅库 syssub2 成功,得到共享的数据表 t1: - - ```sql - -- acc2 现在能看到 pub2 了 - mysql> show subscriptions all; - +----------+-------------+--------------+---------------------+----------+---------------------+ - | pub_name | pub_account | pub_database | pub_time | sub_name | sub_time | - +----------+-------------+--------------+---------------------+----------+---------------------+ - | pub1 | sys | sub1 | 2024-04-23 10:28:15 | syssub1 | 2024-04-23 10:35:13 | - | pub2 | sys | sub2 | 2024-04-23 10:40:54 | NULL | NULL | - +----------+-------------+--------------+---------------------+----------+---------------------+ - 2 rows in set (0.00 sec) - - create database syssub2 from sys publication pub2; - use syssub2; - - mysql> show subscriptions all; - +----------+-------------+--------------+---------------------+----------+---------------------+ - | pub_name | pub_account | pub_database | pub_time | sub_name | sub_time | - +----------+-------------+--------------+---------------------+----------+---------------------+ - | pub2 | sys | sub2 | 2024-04-23 10:40:54 | syssub2 | 2024-04-23 10:50:43 | - | pub1 | sys | sub1 | 2024-04-23 10:28:15 | syssub1 | 2024-04-23 10:35:13 | - +----------+-------------+--------------+---------------------+----------+---------------------+ - 2 rows in set (0.00 sec) + use tab_products_sub; mysql> show tables; - +--------------------+ - | Tables_in_syssub2 | - +--------------------+ - | t1 | - +--------------------+ - 2 rows in set (0.02 sec) + +----------------------------+ + | Tables_in_tab_products_sub | + +----------------------------+ + | products | + +----------------------------+ + 1 row in set (0.01 sec) + + mysql> select * from products; + +------------+--------------+--------+ + | product_id | product_name | price | + +------------+--------------+--------+ + | 1 | Laptop | 999.99 | + | 2 | Smartphone | 599.99 | + | 3 | Tablet | 399.99 | + +------------+--------------+--------+ + 3 rows in set (0.01 sec) ``` -9. **发布者**:sys 租户删除发布 pub1: +4. 数据变动 - ```sql - drop publication pub1; - mysql> show publications; - +-------------+----------+---------------------+---------------------+-------------+----------+ - | publication | database | create_time | update_time | sub_account | comments | - +-------------+----------+---------------------+---------------------+-------------+----------+ - | pub2 | sub2 | 2024-04-23 10:40:54 | 2024-04-23 10:47:53 | * | | - +-------------+----------+---------------------+---------------------+-------------+----------+ - 1 row in set (0.00 sec) - ``` + 在发布端数据,发布订阅机制将把这些变动同步到订阅端。 -10. **订阅者**:acc1、acc2 连接 syspub1 失败: - - ```sql - mysql> use syssub1; - ERROR 20101 (HY000): internal error: there is no publication pub1 - ``` - -11. **发布者**:sys 租户新建数据库 sub1_new,并重新以 pub1 发布 - - ```sql - create database sub1_new; - use sub1_new; - create table t3(n1 int); - insert into t3 values (1); - create publication pub1 database sub1_new; - mysql> show publications; - +-------------+----------+---------------------+---------------------+-------------+----------+ - | publication | database | create_time | update_time | sub_account | comments | - +-------------+----------+---------------------+---------------------+-------------+----------+ - | pub2 | sub2 | 2024-04-23 10:40:54 | 2024-04-23 10:47:53 | * | | - | pub1 | sub1_new | 2024-04-23 10:59:11 | NULL | * | | - +-------------+----------+---------------------+---------------------+-------------+----------+ - 2 rows in set (0.00 sec) - ``` - -12. **订阅者**:acc1、acc2 连接 syspub1,可以看到 pub1 新的内容,就是说如果发布方更改了发布的内容,订阅方不用做任何操作即可看到更新。 - ```sql - use syssub1; - mysql> show subscriptions; - +----------+-------------+--------------+---------------------+----------+---------------------+ - | pub_name | pub_account | pub_database | pub_time | sub_name | sub_time | - +----------+-------------+--------------+---------------------+----------+---------------------+ - | pub2 | sys | sub2 | 2024-04-23 10:40:54 | syssub2 | 2024-04-23 10:42:31 | - | pub1 | sys | sub1_new | 2024-04-23 10:59:11 | syssub1 | 2024-04-23 10:30:43 | - +----------+-------------+--------------+---------------------+----------+---------------------+ + -- 修改库存表中的库存数量 + UPDATE inventory SET stock_quantity = 80 WHERE product_id = 1; + + -- 删除商品价格表中的某一行 + DELETE FROM products WHERE product_id = 2; + + mysql> select * from inventory; + +------------+--------------+----------------+ + | product_id | product_name | stock_quantity | + +------------+--------------+----------------+ + | 1 | Laptop | 80 | + | 2 | Smartphone | 200 | + | 3 | Tablet | 150 | + +------------+--------------+----------------+ + 3 rows in set (0.00 sec) + + mysql> select * from products; + +------------+--------------+--------+ + | product_id | product_name | price | + +------------+--------------+--------+ + | 1 | Laptop | 999.99 | + | 3 | Tablet | 399.99 | + +------------+--------------+--------+ 2 rows in set (0.01 sec) - - mysql> show tables; - +-------------------+ - | Tables_in_syssub1 | - +-------------------+ - | t3 | - +-------------------+ - 1 row in set (0.01 sec) - - mysql> select * from t3; - +------+ - | n1 | - +------+ - | 1 | - +------+ - 1 row in set (0.01 sec) ``` -13. **订阅者**:acc1 删除订阅: +5. 在订阅端查看变动情况 ```sql - - -- 通过 drop database 删除订阅 - drop database syssub1; - mysql> show subscriptions; - +----------+-------------+--------------+---------------------+----------+---------------------+ - | pub_name | pub_account | pub_database | pub_time | sub_name | sub_time | - +----------+-------------+--------------+---------------------+----------+---------------------+ - | pub2 | sys | sub2 | 2024-04-23 10:40:54 | syssub2 | 2024-04-23 10:42:31 | - +----------+-------------+--------------+---------------------+----------+---------------------+ - 1 row in set (0.00 sec) + -- 在 acc1 中查看库存情况 + mysql> select * from inventory ; + +------------+--------------+----------------+ + | product_id | product_name | stock_quantity | + +------------+--------------+----------------+ + | 1 | Laptop | 80 | + | 2 | Smartphone | 200 | + | 3 | Tablet | 150 | + +------------+--------------+----------------+ + 3 rows in set (0.01 sec) + + -- 在 acc2 中查看商品价格情况 + + mysql> select * from products; + +------------+--------------+--------+ + | product_id | product_name | price | + +------------+--------------+--------+ + | 1 | Laptop | 999.99 | + | 3 | Tablet | 399.99 | + +------------+--------------+--------+ + 2 rows in set (0.01 sec) ``` -14. **发布者**:sys 租户删除已发布的数据库之前,要先删除其对应的发布: +6. 删除发布 - ```sql - mysql> drop database sub1_new; - ERROR 20101 (HY000): internal error: can not drop database 'sub1_new' which is publishing - mysql> drop publication pub1; - Query OK, 0 rows affected (0.00 sec) - - mysql> drop database sub1_new; - Query OK, 1 row affected (0.03 sec) - ``` - -15. **发布者**:sys 租户修改发布内容: - - ```sql - alter publication pub2 comment "this is pub2";--修改comments - mysql> show publications; - create database new_sub2; - create table new_sub2.new_t (xxx int); - insert into new_sub2.new_t values (123); - alter publication pub2 database new_sub2;--修改database - mysql> show publications; - +-------------+----------+---------------------+---------------------+-------------+--------------+ - | publication | database | create_time | update_time | sub_account | comments | - +-------------+----------+---------------------+---------------------+-------------+--------------+ - | pub2 | new_sub2 | 2024-04-23 10:40:54 | 2024-04-23 11:04:20 | * | this is pub2 | - +-------------+----------+---------------------+---------------------+-------------+--------------+ - 1 row in set (0.00 sec) - ``` - -16. **订阅者**:acc1、acc2 查看订阅,能看到发布数据库修改后的内容: + 在某些情况下,当某个仓库停止使用,或者数据发布不再需要对某张表进行同步时,可以通过 DROP PUBLICATION 来解除发布关系,防止不必要的资源消耗。 ```sql - mysql> show subscriptions; - +----------+-------------+--------------+---------------------+----------+---------------------+ - | pub_name | pub_account | pub_database | pub_time | sub_name | sub_time | - +----------+-------------+--------------+---------------------+----------+---------------------+ - | pub2 | sys | new_sub2 | 2024-04-23 10:40:54 | syssub2 | 2024-04-23 10:42:31 | - +----------+-------------+--------------+---------------------+----------+---------------------+ - 1 row in set (0.00 sec) - - use syssub2; - mysql> show tables; - +-------------------+ - | Tables_in_syssub2 | - +-------------------+ - | new_t | - +-------------------+ - 1 row in set (0.00 sec) - - mysql> select * from new_t; - +------+ - | xxx | - +------+ - | 123 | - +------+ - 1 row in set (0.00 sec) + -- 删除商品价格表的发布 + drop publication tab_products_sub; ``` ## 参考文档 diff --git a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/alter-publication.md b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/alter-publication.md index d04a9c307b..ffb0d18c64 100644 --- a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/alter-publication.md +++ b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/alter-publication.md @@ -15,6 +15,7 @@ ALTER PUBLICATION pubname | DROP account_name, [, ... ]] [COMMENT 'string'] [DATABASE database_name] + [TAble table_name] ``` ## 语法解释 @@ -32,39 +33,39 @@ create account acc2 admin_name 'root' identified by '111'; create database t; create publication pub3 database t account acc0,acc1; -alter publication pub3 account add accx;--修改发布范围 +alter publication pub3 account add acc2;--修改发布范围 mysql> show create publication pub3; -+-------------+-----------------------------------------------------------------------+ -| Publication | Create Publication | -+-------------+-----------------------------------------------------------------------+ -| pub3 | CREATE PUBLICATION `pub3` DATABASE `t` ACCOUNT `acc0`, `acc1`, `accx` | -+-------------+-----------------------------------------------------------------------+ ++-------------+-----------------------------------------------------------+ +| Publication | Create Publication | ++-------------+-----------------------------------------------------------+ +| pub3 | CREATE PUBLICATION pub3 DATABASE t ACCOUNT acc0,acc1,acc2 | ++-------------+-----------------------------------------------------------+ 1 row in set (0.01 sec) mysql> show publications; -+-------------+----------+---------------------+---------------------+----------------+----------+ -| publication | database | create_time | update_time | sub_account | comments | -+-------------+----------+---------------------+---------------------+----------------+----------+ -| pub3 | t | 2024-04-24 11:17:37 | 2024-04-24 11:17:44 | acc0,acc1,accx | | -+-------------+----------+---------------------+---------------------+----------------+----------+ -1 row in set (0.01 sec) ++-------------+----------+--------+----------------+---------------------+---------------------+---------------------+----------+ +| publication | database | tables | sub_account | subscribed_accounts | create_time | update_time | comments | ++-------------+----------+--------+----------------+---------------------+---------------------+---------------------+----------+ +| pub3 | t | * | acc0,acc1,acc2 | | 2024-10-28 11:20:20 | 2024-10-28 11:20:43 | | ++-------------+----------+--------+----------------+---------------------+---------------------+---------------------+----------+ +1 row in set (0.00 sec) alter publication pub3 comment "this is pubs";--修改发布备注 mysql> show publications; -+-------------+----------+---------------------+---------------------+----------------+--------------+ -| publication | database | create_time | update_time | sub_account | comments | -+-------------+----------+---------------------+---------------------+----------------+--------------+ -| pub3 | t | 2024-04-24 11:17:37 | 2024-04-24 11:41:43 | acc0,acc1,accx | this is pubs | -+-------------+----------+---------------------+---------------------+----------------+--------------+ ++-------------+----------+--------+----------------+---------------------+---------------------+---------------------+--------------+ +| publication | database | tables | sub_account | subscribed_accounts | create_time | update_time | comments | ++-------------+----------+--------+----------------+---------------------+---------------------+---------------------+--------------+ +| pub3 | t | * | acc0,acc1,acc2 | | 2024-10-28 11:20:20 | 2024-10-28 11:21:28 | this is pubs | ++-------------+----------+--------+----------------+---------------------+---------------------+---------------------+--------------+ 1 row in set (0.00 sec) create database new_pub3; alter publication pub3 database new_pub3;--修改发布数据库 mysql> show publications; -+-------------+----------+---------------------+---------------------+----------------+--------------+ -| publication | database | create_time | update_time | sub_account | comments | -+-------------+----------+---------------------+---------------------+----------------+--------------+ -| pub3 | new_pub3 | 2024-04-24 11:17:37 | 2024-04-24 11:43:36 | acc0,acc1,accx | this is pubs | -+-------------+----------+---------------------+---------------------+----------------+--------------+ ++-------------+----------+--------+----------------+---------------------+---------------------+---------------------+--------------+ +| publication | database | tables | sub_account | subscribed_accounts | create_time | update_time | comments | ++-------------+----------+--------+----------------+---------------------+---------------------+---------------------+--------------+ +| pub3 | new_pub3 | * | acc0,acc1,acc2 | | 2024-10-28 11:20:20 | 2024-10-28 11:21:44 | this is pubs | ++-------------+----------+--------+----------------+---------------------+---------------------+---------------------+--------------+ 1 row in set (0.00 sec) ``` diff --git a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-publication.md b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-publication.md index 71af9f6e81..efbbc0203b 100644 --- a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-publication.md +++ b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-publication.md @@ -7,8 +7,8 @@ ## **语法结构** ``` -CREATE PUBLICATION pubname - DATABASE database_name ACCOUNT +CREATE PUBLICATION + DATABASE [] ACCOUNT [ { ALL | account_name, [, ... ] }] [ COMMENT 'string'] @@ -23,13 +23,20 @@ CREATE PUBLICATION pubname ## **示例** ```sql -create database t; -create account acc0 admin_name 'root' identified by '111'; -create account acc1 admin_name 'root' identified by '111'; -mysql> create publication pub1 database t account acc0,acc1; -Query OK, 0 rows affected (0.01 sec) +create account acc01 admin_name 'root' identified by '111'; +create account acc02 admin_name 'root' identified by '111'; +create database db1; +use db1; +create table t1(n1 int); +create table t2(n1 int); + +--数据库级别发布 +create publication db_pub1 database db1 account acc01,acc02; + +--表级别发布 +create publication tab_pub1 database db1 table t1,t2 account acc01,acc02; ``` ## 限制 -MatrxiOne 当前仅支持一次发布一个数据库数据。 +- 数据库级别的发布,目前仅支持一次发布一个数据库数据。 diff --git a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-subscription.md b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-subscription.md index 9a95c42b5b..5c3b04fc9d 100644 --- a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-subscription.md +++ b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/create-subscription.md @@ -21,81 +21,37 @@ PUBLICATION pubname; ## **示例** ```sql --- 假设系统管理员创建了一个租户 acc1 为订阅方 -create account acc1 admin_name 'root' identified by '111'; - --- 假设会话 1 为发布方,由发布方先发布一个数据库给租户 -create database sys_db_1; -use sys_db_1; -create table sys_tbl_1(a int primary key ); -insert into sys_tbl_1 values(1),(2),(3); -create view v1 as (select * from sys_tbl_1); -create publication sys_pub_1 database sys_db_1; -mysql> show publications; -+-------------+----------+---------------------+-------------+-------------+----------+ -| publication | database | create_time | update_time | sub_account | comments | -+-------------+----------+---------------------+-------------+-------------+----------+ -| sys_pub_1 | sys_db_1 | 2024-04-24 11:54:36 | NULL | * | | -+-------------+----------+---------------------+-------------+-------------+----------+ -1 row in set (0.01 sec) - --- 再开启一个新的会话,假设会话 2 为订阅方,由订阅方订阅已发布的数据库 -mysql -h 127.0.0.1 -P 6001 -u acc1:root -p --登录租户账号 - -create database sub1 from sys publication sys_pub_1; -mysql> show databases; -+--------------------+ -| Database | -+--------------------+ -| information_schema | -| mo_catalog | -| mysql | -| sub1 | -| system | -| system_metrics | -+--------------------+ -6 rows in set (0.01 sec) +create account acc01 admin_name 'root' identified by '111'; +create database db1; +use db1; +create table t1(n1 int); +create table t2(n1 int); + +--创建发布 +create publication db_pub1 database db1 account acc01; +create publication tab_pub1 database db1 table t1,t2 account acc01; + +--连接租户 acc01 +mysql> show subscriptions all; ++----------+-------------+--------------+------------+-------------+---------------------+----------+----------+--------+ +| pub_name | pub_account | pub_database | pub_tables | pub_comment | pub_time | sub_name | sub_time | status | ++----------+-------------+--------------+------------+-------------+---------------------+----------+----------+--------+ +| tab_pub1 | sys | db1 | t1,t2 | | 2024-10-25 17:06:06 | NULL | NULL | 0 | +| db_pub1 | sys | db1 | * | | 2024-10-25 17:05:54 | NULL | NULL | 0 | ++----------+-------------+--------------+------------+-------------+---------------------+----------+----------+--------+ +2 rows in set (0.00 sec) + +create database db_sub1 from sys publication db_pub1; +create database tab_sub1 from sys publication tab_pub1; mysql> show subscriptions; -+-----------+-------------+--------------+---------------------+----------+---------------------+ -| pub_name | pub_account | pub_database | pub_time | sub_name | sub_time | -+-----------+-------------+--------------+---------------------+----------+---------------------+ -| sys_pub_1 | sys | sys_db_1 | 2024-04-24 11:54:36 | sub1 | 2024-04-24 11:56:05 | -+-----------+-------------+--------------+---------------------+----------+---------------------+ -1 row in set (0.01 sec) - -mysql> use sub1; -Reading table information for completion of table and column names -You can turn off this feature to get a quicker startup with -A - -Database changed -mysql> show tables; -+----------------+ -| Tables_in_sub1 | -+----------------+ -| sys_tbl_1 | -| v1 | -+----------------+ -2 rows in set (0.01 sec) - -mysql> desc sys_tbl_1; -+-------+---------+------+------+---------+-------+---------+ -| Field | Type | Null | Key | Default | Extra | Comment | -+-------+---------+------+------+---------+-------+---------+ -| a | INT(32) | NO | PRI | NULL | | | -+-------+---------+------+------+---------+-------+---------+ -1 row in set (0.01 sec) - -mysql> select * from sys_tbl_1 order by a; -+------+ -| a | -+------+ -| 1 | -| 2 | -| 3 | -+------+ -3 rows in set (0.01 sec) --- 订阅成功 ++----------+-------------+--------------+------------+-------------+---------------------+----------+---------------------+--------+ +| pub_name | pub_account | pub_database | pub_tables | pub_comment | pub_time | sub_name | sub_time | status | ++----------+-------------+--------------+------------+-------------+---------------------+----------+---------------------+--------+ +| tab_pub1 | sys | db1 | t1,t2 | | 2024-10-25 17:06:06 | tab_sub1 | 2024-10-25 17:09:24 | 0 | +| db_pub1 | sys | db1 | * | | 2024-10-25 17:05:54 | db_sub1 | 2024-10-25 17:09:23 | 0 | ++----------+-------------+--------------+------------+-------------+---------------------+----------+---------------------+--------+ +2 rows in set (0.00 sec) ``` !!! note diff --git a/docs/MatrixOne/Reference/SQL-Reference/Other/SHOW-Statements/show-publications.md b/docs/MatrixOne/Reference/SQL-Reference/Other/SHOW-Statements/show-publications.md index e8a1d0d9ee..0c7ffe6fa4 100644 --- a/docs/MatrixOne/Reference/SQL-Reference/Other/SHOW-Statements/show-publications.md +++ b/docs/MatrixOne/Reference/SQL-Reference/Other/SHOW-Statements/show-publications.md @@ -2,7 +2,7 @@ ## **语法说明** -返回所有发布名、发布的数据库名、发布创建时间、发布最新修改时间、发布指定的租户名列表(如果是所有则展示 "*")和备注。 +返回所有发布名、发布的数据库/表名、发布创建时间、发布最新修改时间、发布指定的租户名列表(如果是所有则展示 "*")等信息。 如需查看更多信息,需要拥有租户管理员权限,查看系统表 mo_pubs 查看更多参数。 @@ -22,10 +22,10 @@ create database t; create publication pub3 database t account acc0,acc1; mysql> show publications; -+-------------+----------+---------------------+-------------+-------------+----------+ -| publication | database | create_time | update_time | sub_account | comments | -+-------------+----------+---------------------+-------------+-------------+----------+ -| pub3 | t | 2024-04-23 10:10:59 | NULL | acc0,acc1 | | -+-------------+----------+---------------------+-------------+-------------+----------+ ++-------------+----------+--------+-------------+---------------------+---------------------+-------------+----------+ +| publication | database | tables | sub_account | subscribed_accounts | create_time | update_time | comments | ++-------------+----------+--------+-------------+---------------------+---------------------+-------------+----------+ +| pub3 | t | * | acc0,acc1 | | 2024-10-25 16:36:04 | NULL | | ++-------------+----------+--------+-------------+---------------------+---------------------+-------------+----------+ 1 row in set (0.00 sec) ``` diff --git a/docs/MatrixOne/Reference/SQL-Reference/Other/SHOW-Statements/show-subscriptions.md b/docs/MatrixOne/Reference/SQL-Reference/Other/SHOW-Statements/show-subscriptions.md index e87828b33f..91e4843781 100644 --- a/docs/MatrixOne/Reference/SQL-Reference/Other/SHOW-Statements/show-subscriptions.md +++ b/docs/MatrixOne/Reference/SQL-Reference/Other/SHOW-Statements/show-subscriptions.md @@ -2,7 +2,7 @@ ## **语法说明** -返回所有发布名、发布租户名、发布的数据库名、发布给该租户的时间、订阅名和创建该订阅的时间。 +返回所有发布名、发布租户名、发布的数据库名、发布的表名、备注、发布给该租户的时间、订阅名、创建该订阅的时间和订阅状态(0:可正常订阅;1:发布存在,但无订阅权限;2:本来订阅了发布,但是又删除了发布)。 ## **语法结构** @@ -17,34 +17,53 @@ SHOW SUBSCRIPTIONS [ALL]; ## **示例** ```sql + +-- 在 sys 租户执行 + +create account acc01 admin_name 'root' identified by '111'; +create account acc02 admin_name 'root' identified by '111'; +create database db1; +use db1; +create table t1(n1 int); +create table t2(n1 int); + +--数据库级别发布 +create publication db_pub1 database db1 account acc01,acc02; + +--表级别发布 +create publication tab_pub1 database db1 table t1,t2 account acc01,acc02; + +-- 在 acc01 租户执行 mysql> show subscriptions all; -+----------+-------------+--------------+---------------------+----------+----------+ -| pub_name | pub_account | pub_database | pub_time | sub_name | sub_time | -+----------+-------------+--------------+---------------------+----------+----------+ -| pub3 | sys | t | 2024-04-23 11:11:06 | NULL | NULL | -+----------+-------------+--------------+---------------------+----------+----------+ -1 row in set (0.01 sec) ++----------+-------------+--------------+------------+-------------+---------------------+----------+----------+--------+ +| pub_name | pub_account | pub_database | pub_tables | pub_comment | pub_time | sub_name | sub_time | status | ++----------+-------------+--------------+------------+-------------+---------------------+----------+----------+--------+ +| tab_pub1 | sys | db1 | t1,t2 | | 2024-10-14 19:00:21 | NULL | NULL | 0 | +| db_pub1 | sys | db1 | * | | 2024-10-14 19:00:16 | NULL | NULL | 0 | ++----------+-------------+--------------+------------+-------------+---------------------+----------+----------+--------+ +2 rows in set (0.00 sec) mysql> show subscriptions; Empty set (0.00 sec) -mysql> create database sub3 from sys publication pub3; -Query OK, 1 row affected (0.02 sec) +create database db_sub1 from sys publication db_pub1; +create database tab_sub1 from sys publication tab_pub1; mysql> show subscriptions all; -+----------+-------------+--------------+---------------------+----------+---------------------+ -| pub_name | pub_account | pub_database | pub_time | sub_name | sub_time | -+----------+-------------+--------------+---------------------+----------+---------------------+ -| pub3 | sys | t | 2024-04-23 11:11:06 | sub3 | 2024-04-23 11:12:11 | -+----------+-------------+--------------+---------------------+----------+---------------------+ -1 row in set (0.00 sec) ++----------+-------------+--------------+------------+-------------+---------------------+----------+---------------------+--------+ +| pub_name | pub_account | pub_database | pub_tables | pub_comment | pub_time | sub_name | sub_time | status | ++----------+-------------+--------------+------------+-------------+---------------------+----------+---------------------+--------+ +| tab_pub1 | sys | db1 | t1,t2 | | 2024-10-14 19:00:21 | tab_sub1 | 2024-10-14 19:01:41 | 0 | +| db_pub1 | sys | db1 | * | | 2024-10-14 19:00:16 | db_sub1 | 2024-10-14 19:01:30 | 0 | ++----------+-------------+--------------+------------+-------------+---------------------+----------+---------------------+--------+ +2 rows in set (0.00 sec) mysql> show subscriptions; -+----------+-------------+--------------+---------------------+----------+---------------------+ -| pub_name | pub_account | pub_database | pub_time | sub_name | sub_time | -+----------+-------------+--------------+---------------------+----------+---------------------+ -| pub3 | sys | t | 2024-04-23 11:11:06 | sub3 | 2024-04-23 11:12:11 | -+----------+-------------+--------------+---------------------+----------+---------------------+ -1 row in set (0.01 sec) - ++----------+-------------+--------------+------------+-------------+---------------------+----------+---------------------+--------+ +| pub_name | pub_account | pub_database | pub_tables | pub_comment | pub_time | sub_name | sub_time | status | ++----------+-------------+--------------+------------+-------------+---------------------+----------+---------------------+--------+ +| tab_pub1 | sys | db1 | t1,t2 | | 2024-10-14 19:00:21 | tab_sub1 | 2024-10-14 19:01:41 | 0 | +| db_pub1 | sys | db1 | * | | 2024-10-14 19:00:16 | db_sub1 | 2024-10-14 19:01:30 | 0 | ++----------+-------------+--------------+------------+-------------+---------------------+----------+---------------------+--------+ +2 rows in set (0.00 sec) ``` diff --git a/docs/MatrixOne/Reference/System-tables.md b/docs/MatrixOne/Reference/System-tables.md index bb20393423..75b0f35bd3 100644 --- a/docs/MatrixOne/Reference/System-tables.md +++ b/docs/MatrixOne/Reference/System-tables.md @@ -178,10 +178,26 @@ MatrixOne 系统数据库和表是 MatrixOne 存储系统信息的地方,你 | table_list | TEXT(0) | 在非 all table 时,发布库内包含的表清单,表名与 database_id 对应数据库下的表一一对应| | account_list | TEXT(0) |在非 all account 时,允许订阅该发布库的 account 清单| | created_time | TIMESTAMP(0) |创建发布库的时间 | +| updated_time | TIMESTAMP(0) |更新发布库的时间 | | owner | INT UNSIGNED(32) | 创建发布库对应的角色 ID | | creator | INT UNSIGNED(32) | 创建发布库对应的用户 ID | | comment | TEXT(0) | 创建发布库的备注信息 | +### `mo_subs` 表 + +| 列属性 | 类型 | 描述 | +| ---------------------| --------------- | ----------------- | +| sub_account_id | INT(32) | 订阅租户 id| +| sub_name | VARCHAR(5000) | 订阅名 | +| sub_time | TIMESTAMP(0) | 订阅时间 | +| pub_account_name |VARCHAR(300) | 发布租户的名称 | +| pub_name | VARCHAR(64) | 发布名称| +| pub_database | VARCHAR(5000) |发布的数据库| +| pub_tables | TEXT(0) |发布的表 | +| pub_time | TIMESTAMP(0) | 发布的时间 | +| pub_comment | TEXT(0) | 来自发布库的备注信息 | +| status | TINYINT(8) | 订阅状态,0 是正常订阅,1 是发布存在,但无订阅权限,2 是本来订阅了发布,但是又删除了发布 | + ### `mo_stages` 表 | 列属性 | 类型 | 描述 |