PostgreSQL逻辑复制之slony篇

前端之家收集整理的这篇文章主要介绍了PostgreSQL逻辑复制之slony篇前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

https://www.cnblogs.com/lottu/p/9138593.html

  Slony是Postgresql领域中最广泛的复制解决方案之一。它不仅是最古老的复制实现之一,它也是一个拥有最广泛的外部工具支持的工具,比如pgAdmin3。多年来,Slony是在Postgresql中复制数据的惟一可行的解决方案。Slony使用逻辑复制;Slony-I一般要求表有主键,或者唯一键;Slony的工作不是基于Postgresql事务日志的;而是基于触发器的;基于逻辑复制高可用性;Postgresql除了slony;还有Londiste,BDR等等后续文章会讲到

1. 安装Slony

  下载地址:http://www.slony.info;安装步骤:

  1. # tar -jxvf slony1-2.2.5.tar.bz2
  2. # cd slony1-2.2.5 ./configure --with-pgconfigdir=/opt/pgsql96/bin make make install

  安装完成!
  执行./configure时;会在当前目录是否可以找到pg_config命令;本例pg_config在/opt/pgsql96/bin目录下;

2. Slony架构图

3. 复制表

现有实验环境:

主机名 IP 角色
Postgresql201 192.168.1.201 master
Postgresql202 192.168.1.202 slave

3.1在两台数据库中都创建一个slony的超级用户;专为slony服务

create user slony superuser password 'li0924';

3.2 本实验两台主机都有lottu数据库;以lottu数据库中的表作为实验对象;在两个数据库中以相同的方式创建该表synctab,因为表结构不会自动复制。

create table synctab(id int primary key,name text);

3.3 在所有节点设置允许Slony-I用户远程登录;在pg_hba.conf文件添加

  1. host all slony 192.168.1.0/24 trust

3.4 设置slony(在master主机操作)

  编写一个slonik脚本用于注册这些节点的脚本如下所示:

  1. [postgres@Postgres201 ~]$ cat slony_setup.sh
  2. #!/bin/sh
  3. MASTERDB=lottu
  4. SLAVEDB=lottu
  5. HOST1=192.168.1.201
  6. HOST2=1.202
  7. DBUSER=slony
  8. slonik<<_EOF_
  9. cluster name = first_cluster;
  10. # define nodes (this is needed by pretty much
  11. # all slonik scripts)
  12. node 1 admin conninfo = 'dbname=$MASTERDB host=$HOST1 user=$DBUSER';
  13. node 2 admin conninfo = dbname=$SLAVEDB host=$HOST2 user=$DBUSER';
  14. # init cluster
  15. init cluster ( id=1,comment = Master Node');
  16. # group tables into sets
  17. create set (Our tables');
  18. set add table (set lottu.synctab',0);line-height:1.5;">sample table');
  19. store node (2,0);line-height:1.5;">Slave node1);
  20. store path (server = ');
  21. store path (server = ');
  22. _EOF_

  现在这个表在Slony的控制下,我们可以开始订阅脚本如下所示:

cat slony_subscribe.1.202 DBUSER=slony slonik<<_EOF_ cluster name = first_cluster; node '; subscribe set ( id = no); _EOF_

  在master主机执行脚本

  1. [postgres@Postgres201 ~]$ ./slony_setup.sh
  2. [postgres@Postgres201 ~]$ ./slony_subscribe.sh &
  3. [1] 1225

  定义了我们想要复制的东西之后,我们可以在每台主机启动slon守护进程

  1. slon first_cluster host=192.168.1.201 dbname=lottu user=slony' &
  2. slon first_cluster host=192.168.1.202 dbname=lottu user=slony' &

3.5 验证slony-I是否配置成功?

  在master主机执行dml操作

  1. [postgres@Postgres201 ~]$ psql lottu lottu
  2. psql (9.6.0)
  3. Type "help" for help.
  4. lottu=# \d synctab
  5. Table "lottu.synctab"
  6. Column | Type | Modifiers
  7. --------+---------+-----------
  8. id | integer | not null
  9. name text |
  10. Indexes:
  11. "synctab_pkey" PRIMARY KEY,btree (id)
  12. Triggers:
  13. _first_cluster_logtrigger AFTER INSERT OR DELETE UPDATE ON synctab FOR EACH ROW EXECUTE PROCEDURE _first_cluster.logtrigger(_first_cluster1k')
  14. _first_cluster_truncatetrigger BEFORE TRUNCATE FOR EACH STATEMENT PROCEDURE _first_cluster.log_truncate(')
  15. Disabled user triggers:
  16. _first_cluster_denyaccess BEFORE PROCEDURE _first_cluster.denyaccess(')
  17. _first_cluster_truncatedeny BEFORE PROCEDURE _first_cluster.deny_truncate()
  18. lottu=# insert into synctab values (1001,0);line-height:1.5;">lottu');
  19. 0 1

  在slave主机查看是否对应变化

postgres@Postgres202 ~]$ psql psql (for help. postgres=# \c lottu lottu You are now connected to database "lottu" as user "lottu". lottu=> select * from synctab ; id | name ----+------- 1001 | lottu (1 row)

4. Slony-I相关表或者视图查看

4.1 配置成功;会在所在的数据库生成一个schema

=# \dn List of schemas Name | Owner --------------+---------- _first_cluster | slony lottu | lottu public | postgres (3 rows)

4.2 查看集群中的节点信息

  1. lottufrom _first_cluster.sl_node;
  2. no_id | no_active | no_comment | no_@R_403_159@
  3. -----+-----------+-------------+-----------
  4. 1 | t | Master Node | f
  5. 2 | Slave node | f
  6. (2 rows)

4.3 查看集群中的集合信息

  1. lottufrom _first_cluster.sl_set;
  2. set_id | set_origin | set_locked | set_comment
  3. ------+------------+------------+-------------
  4. | | | Our tables
  5. (1 row)

4.4 查看集群中的表信息

from _first_cluster.sl_table; - RECORD 1 ]----------- tab_id 1 tab_reloid 57420 tab_relname | synctab tab_nspname | lottu tab_set 1 tab_idxname | synctab_pkey tab_altered | f tab_comment | sample table

5. 日常维护

5.1 Slony-I向现有集群中增加一个复制表

  以表synctab2为例:

table synctab2(id text,reg_time timestamp);

  我们要创建一个新的表格集;脚本是这样的

cat slony_add_table_set.'; create set (a second replication setlottu.synctab2second table'); subscribe set(2); subscribe set(2); merge set(1); _EOF_

  执行slony_add_table_set.sh脚本

  1. [postgres@Postgres201 ~]$ ./slony_add_table_set.sh
  2. <stdin>:8 subscription in progress before mergeSet. waiting
  3. <stdin>:in progress before mergeSet. waiting

  查看是否添加成功

  1. lottu=# select * from _first_cluster.sl_table;
  2. -[ RECORD 1 ]--------------
  3. tab_id | 1
  4. tab_reloid | 57420
  5. tab_relname | synctab
  6. tab_nspname | lottu
  7. tab_set | 1
  8. tab_idxname | synctab_pkey
  9. tab_altered | f
  10. tab_comment | sample table
  11. -[ RECORD 2 ]--------------
  12. tab_id | 2
  13. tab_reloid | 57840
  14. tab_relname | synctab2
  15. tab_nspname | lottu
  16. tab_set | 1
  17. tab_idxname | synctab2_pkey
  18. tab_altered | f
  19. tab_comment | second table

5.2 Slony-I向现有集群中删除一个复制表

cat slony_drop_table.sh #!/bin/'; set drop table (

  执行slony_drop_table.sh脚本

  1. [postgres@Postgres201 ~]$ ./slony_drop_table.sh

  查看是否删除成功

from _first_cluster.sl_table; tab_id | tab_reloid | tab_relname | tab_nspname | tab_set | tab_idxname | tab_altered | tab_comment ------+------------+-------------+-------------+---------+--------------+-------------+-------------- | 57420 | synctab | lottu | | synctab_pkey | f table (1 row)

5. 3删除slony

cat slony_drop_node.'; uninstall node (1); uninstall node (2); _EOF_

  执行脚本如下:

  1. [postgres@Postgres201 ~]$ ./slony_drop_node.4: NOTICE: Slony-I: Please drop schema "_first_cluster"
  2. <stdin>:4: NOTICE: drop cascades to 175 other objects
  3. DETAIL: drop cascades to table _first_cluster.sl_node
  4. drop cascades to table _first_cluster.sl_nodelock
  5. drop cascades to table _first_cluster.sl_set
  6. drop cascades to table _first_cluster.sl_setsync
  7. drop cascades to table _first_cluster.sl_table
  8. drop cascades to table _first_cluster.sl_sequence
  9. drop cascades to table _first_cluster.sl_path
  10. drop cascades to table _first_cluster.sl_listen
  11. drop cascades to table _first_cluster.sl_subscribe
  12. drop cascades to table _first_cluster.sl_event
  13. drop cascades to table _first_cluster.sl_confirm
  14. drop cascades to table _first_cluster.sl_seqlog
  15. drop cascades to function _first_cluster.sequencelastvalue(text)
  16. drop cascades to table _first_cluster.sl_log_1
  17. drop cascades to table _first_cluster.sl_log_2
  18. drop cascades to table _first_cluster.sl_log_script
  19. drop cascades to table _first_cluster.sl_registry
  20. drop cascades to table _first_cluster.sl_apply_stats
  21. drop cascades to view _first_cluster.sl_seqlastvalue
  22. drop cascades to view _first_cluster.sl_failover_targets
  23. drop cascades to sequence _first_cluster.sl_local_node_id
  24. drop cascades to sequence _first_cluster.sl_event_seq
  25. drop cascades to sequence _first_cluster.sl_action_seq
  26. drop cascades to sequence _first_cluster.sl_log_status
  27. drop cascades to table _first_cluster.sl_config_lock
  28. drop cascades to table _first_cluster.sl_event_lock
  29. drop cascades to table _first_cluster.sl_archive_counter
  30. drop cascades to table _first_cluster.sl_components
  31. drop cascades to type _first_cluster.vactables
  32. drop cascades to function _first_cluster.createevent(name,text)
  33. drop cascades to function _first_cluster.denyaccess()
  34. drop cascades to trigger _first_cluster_denyaccess on table lottu.synctab
  35. drop cascades to function _first_cluster.lockedset()
  36. drop cascades to function _first_cluster.getlocalnodeid(name)
  37. drop cascades to function _first_cluster.getmoduleversion()
  38. drop cascades to function _first_cluster.resetsession()
  39. drop cascades to function _first_cluster.logapply()
  40. drop cascades to function _first_cluster.logapplysetcachesize(integer)
  41. drop cascades to function _first_cluster.logapplysavestats(name,integer,interval)
  42. drop cascades to function _first_cluster.checkmoduleversion()
  43. drop cascades to function _first_cluster.decode_tgargs(bytea)
  44. drop cascades to function _first_cluster.logtrigger()
  45. drop cascades to trigger _first_cluster_logtrigger on table lottu.synctab
  46. drop cascades to function _first_cluster.terminatenodeconnections(integer)
  47. drop cascades to function _first_cluster.killbackend(integer,255);line-height:1.5;">function _first_cluster.seqtrack(integer,bigint)
  48. drop cascades to function _first_cluster.slon_quote_brute(text)
  49. drop cascades to function _first_cluster.slon_quote_input(text)
  50. drop cascades to function _first_cluster.slonyversionmajor()
  51. drop cascades to function _first_cluster.slonyversionminor()
  52. drop cascades to function _first_cluster.slonyversionpatchlevel()
  53. drop cascades to function _first_cluster.slonyversion()
  54. drop cascades to function _first_cluster.registry_set_int4(text,integer)
  55. drop cascades to function _first_cluster.registry_get_int4(text,255);line-height:1.5;">function _first_cluster.registry_set_text(text,255);line-height:1.5;">function _first_cluster.registry_get_text(text,255);line-height:1.5;">function _first_cluster.registry_set_timestamp(text,timestamp with time zone)
  56. drop cascades to function _first_cluster.registry_get_timestamp(text,255);line-height:1.5;">function _first_cluster.cleanupnodelock()
  57. drop cascades to function _first_cluster.registernodeconnection(integer)
  58. drop cascades to function _first_cluster.initializelocalnode(integer,255);line-height:1.5;">function _first_cluster.storenode(integer,255);line-height:1.5;">function _first_cluster.storenode_int(integer,255);line-height:1.5;">function _first_cluster.enablenode(integer)
  59. drop cascades to function _first_cluster.enablenode_int(integer)
  60. drop cascades to function _first_cluster.disablenode(integer)
  61. drop cascades to function _first_cluster.disablenode_int(integer)
  62. drop cascades to function _first_cluster.dropnode(integer[])
  63. drop cascades to function _first_cluster.dropnode_int(integer)
  64. drop cascades to function _first_cluster.prefailover(integer,boolean)
  65. drop cascades to function _first_cluster.@R_403_159@node(integer,integer[])
  66. drop cascades to function _first_cluster.@R_403_159@node2(integer,bigint,255);line-height:1.5;">function _first_cluster.@R_403_159@node3(integer,255);line-height:1.5;">function _first_cluster.failoverset_int(integer,255);line-height:1.5;">function _first_cluster.uninstallnode()
  67. drop cascades to function _first_cluster.clonenodeprepare(integer,255);line-height:1.5;">function _first_cluster.clonenodeprepare_int(integer,255);line-height:1.5;">function _first_cluster.clonenodefinish(integer,255);line-height:1.5;">function _first_cluster.storepath(integer,255);line-height:1.5;">function _first_cluster.storepath_int(integer,255);line-height:1.5;">function _first_cluster.droppath(integer,255);line-height:1.5;">function _first_cluster.droppath_int(integer,255);line-height:1.5;">function _first_cluster.storelisten(integer,255);line-height:1.5;">function _first_cluster.storelisten_int(integer,255);line-height:1.5;">function _first_cluster.droplisten(integer,255);line-height:1.5;">function _first_cluster.droplisten_int(integer,255);line-height:1.5;">function _first_cluster.storeset(integer,255);line-height:1.5;">function _first_cluster.storeset_int(integer,255);line-height:1.5;">function _first_cluster.lockset(integer)
  68. drop cascades to function _first_cluster.unlockset(integer)
  69. drop cascades to function _first_cluster.moveset(integer,255);line-height:1.5;">function _first_cluster.moveset_int(integer,bigint)
  70. and 75 other objects (see server log for list)
  71. <stdin>:5: NOTICE: Slony-I: Please drop schema 5: NOTICE: drop cascades to for list)

  完美;一切归零!

查考文献

https://www.cnblogs.com/ilifeilong/p/7009322.html

https://www.cnblogs.com/gaojian/p/3196244.html

猜你在找的Postgre SQL相关文章