CREATE…FROM…PUBLICATION…

语法说明

CREATE...FROM...PUBLICATION... 是订阅方订阅一个由发布方创建的发布,用来获取发布方的共享数据。

语法结构

  1. CREATE DATABASE database_name
  2. FROM account_name
  3. PUBLICATION pubname;

语法解释

  • database_name:订阅方创建的数据库名称。
  • pubname:发布方已发布的发布名称。
  • account_name:可获取该发布的租户名称。

示例

  1. -- 假设系统管理员创建了一个租户 acc1 为订阅方
  2. create account acc1 admin_name 'root' identified by '111';
  3. -- 假设会话 1 为发布方,由发布方先发布一个数据库给租户
  4. create database sys_db_1;
  5. use sys_db_1;
  6. create table sys_tbl_1(a int primary key );
  7. insert into sys_tbl_1 values(1),(2),(3);
  8. create view v1 as (select * from sys_tbl_1);
  9. create publication sys_pub_1 database sys_db_1;
  10. mysql> show publications;
  11. +-----------+----------+
  12. | Name | Database |
  13. +-----------+----------+
  14. | sys_pub_1 | sys_db_1 |
  15. +-----------+----------+
  16. 1 row in set (0.01 sec)
  17. -- 再开启一个新的会话,假设会话 2 为订阅方,由订阅方订阅已发布的数据库
  18. mysql -h 127.0.0.1 -P 6001 -u acc1:root -p --登录租户账号
  19. create database sub1 from sys publication pub1;
  20. mysql> create database sub1 from sys publication sys_pub_1;
  21. Query OK, 1 row affected (0.02 sec)
  22. mysql> show databases;
  23. +--------------------+
  24. | Database |
  25. +--------------------+
  26. | system |
  27. | system_metrics |
  28. | information_schema |
  29. | mysql |
  30. | mo_catalog |
  31. | sub1 |
  32. +--------------------+
  33. 6 rows in set (0.00 sec)
  34. mysql> show subscriptions;
  35. +------+--------------+
  36. | Name | From_Account |
  37. +------+--------------+
  38. | sub1 | sys |
  39. +------+--------------+
  40. 1 row in set (0.01 sec)
  41. mysql> use sub1;
  42. Reading table information for completion of table and column names
  43. You can turn off this feature to get a quicker startup with -A
  44. Database changed
  45. mysql> show tables;
  46. +----------------+
  47. | Tables_in_sub1 |
  48. +----------------+
  49. | sys_tbl_1 |
  50. | v1 |
  51. +----------------+
  52. 2 rows in set (0.01 sec)
  53. mysql> desc sys_tbl_1;
  54. +-------+---------+------+------+---------+-------+---------+
  55. | Field | Type | Null | Key | Default | Extra | Comment |
  56. +-------+---------+------+------+---------+-------+---------+
  57. | a | INT(32) | NO | PRI | NULL | | |
  58. +-------+---------+------+------+---------+-------+---------+
  59. 1 row in set (0.01 sec)
  60. mysql> select * from sys_tbl_1 order by a;
  61. +------+
  62. | a |
  63. +------+
  64. | 1 |
  65. | 2 |
  66. | 3 |
  67. +------+
  68. 3 rows in set (0.01 sec)
  69. -- 订阅成功

Note

如果需要取消订阅,可以直接删除已订阅的数据库名称,使用 DROP DATABASE