CREATE…FROM…PUBLICATION…

Description

CREATE...FROM...PUBLICATION... is when the subscriber subscribes to a publication created by the publisher to obtain the publisher’s shared data.

Syntax

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

Explanations

  • database_name: The name of the database created by the subscriber.
  • pubname: The name of the publication that the publisher has published.
  • account_name: The account name of the publication can be obtained.

Examples

  1. --Suppose the system administrator creates a account acc1 as the subscriber
  2. create account acc1 admin_name 'root' identified by '111';
  3. --Assuming session 1 is the publisher, the publisher first publishes a database to the account
  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. --Open a new session again, assuming that session 2 is the subscriber and the subscriber subscribes to the published database
  18. mysql -h 127.0.0.1 -P 6001 -u acc1:root -p -- Log into the account
  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. -- Subscribe successfully

Note

If you need to unsubscribe, you can directly delete the subscribed database. Refer to ‘DROP DATABASE`.