暂无图片
暂无图片
1
暂无图片
暂无图片
暂无图片

PostgreSQL 12 流复制配置Streaming Replication

原创 Mouhamadou 2019-11-17
3474

PostgreSQL 12 的PGDATA中不再有recovery.conf文件,现在recovery.conf的所有参数现在都放在了postgresql.conf文件中。并且在备用服务器的群集数据目录中应为名为standby.signal的文件以触发备用模式。

主服务器pg-primary:192.168.56.101
备用服务器pg-standby:192.168.56.102

步骤1:在primay服务器上创建用户repliuser
对于流复制需要一个用户来读取WAL流,我们可以用超级用户来完成,但生产环境一般不这样。我们将创建一个具有REPLICATION和LOGIN权限的用户。与SUPERUSER相反,REPLICATION不允许修改任何数据。
在这里,我们将创建一个名为repliuser的用户。

postgres=# create user repliuser with password 'postgres' replication ; CREATE ROLE postgres=#

步骤2:在主服务器上准备身份验证
应该允许用于复制的用户连接进行复制。然后,我们需要为两个服务器调整pg_hba.conf文件。

[postgres@pg-primary:/u02/pgdata/12/PG1/]$ grep repliuser pg_hba.conf host replication repliuser 192.168.56.101/32 md5 host replication repliuser 192.168.56.102/32 md5 [postgres@pg-primary:/u02/pgdata/12/PG1/]$

步骤3:在主服务器上创建复制slot
复制slot提供了一种自动方式,以确保在所有备用数据库都接收到WAL段之前,主服务器不会删除它们,并且主服务器在断开备用电源不会删除可能导致恢复冲突的行。

psql (12.1 dbi services build) Type "help" for help. postgres=# SELECT * FROM pg_create_physical_replication_slot('pg_slot_1'); slot_name | lsn -----------+----- pg_slot_1 | (1 row) postgres=#

步骤4:对主数据库进行备份并在备用数据库上还原
从备用服务器启动以下命令

[postgres@pg-standby:/u02/pgdata/12/PG1/]$ pg_basebackup -h 192.168.56.101 -D /u02/pgdata/12/PG1 --wal-method=fetch -U repliuser

步骤5:设置备用端流的主要连接信息。
主机名和端口号,连接用户名和密码在primary_conninfo中指定。由于不再有recovery.conf参数,现在应该在postgresql.conf中指定primary_conninfo

[postgres@pg-standby:/u02/pgdata/12/PG1/]$ grep primary postgresql.conf primary_conninfo = 'host=192.168.56.101 port=5432 user=repliuser password=postgres' primary_slot_name = 'pg_slot_1' # replication slot on sending server

步骤6:在备用服务器上创建standby.signal文件
在备用服务器的群集数据目录中,创建一个文件standby.signal

[postgres@pg-standby:/u02/pgdata/12/PG1/]$ pwd /u02/pgdata/12/PG1 [postgres@pg-standby:/u02/pgdata/12/PG1/]$ touch standby.signal

步骤7:然后启动备用集群

[postgres@pg-standby:/u02/pgdata/12/PG1/]$ pg_ctl start

如果一切正常,日志如下:

2019-11-16 17:41:21.552 CET [1590] LOG: database system is ready to accept read only connections 2019-11-16 17:41:21.612 CET [1596] LOG: started streaming WAL from primary at 0/5000000 on timeline 1
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论