加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

PostgreSQL学习篇15.2 同步流复制的standby数据库

发布时间:2020-12-13 16:58:29 所属栏目:百科 来源:网络整理
导读:从9.1后,提供了同步流复制的架构。同步复制要求在数据写入standby数据库后,事务的commit才返回,所以standby库出现问题时,会导致主库hang
从9.1后,提供了同步流复制的架构。同步复制要求在数据写入standby数据库后,事务的commit才返回,所以standby库出现问题时,会导致主库hang住。可以启动两个standby数据库,只要有一个是正常的,主库就不会hang住。但是因为资源限制,这里只配置一个standby库。

环境:
主机名
IP地址
角色
数据目录
pg
186.168.100.14
主库
/PostgreSQL/9.6.1/datan
pghs
186.168.100.24
standby
/PostgreSQL/9.6.1/datass

实现同步复制功能主要是在主库上配置参数“synchronous_standby_names”,这个参数指定多个standby的名称,各个名称通过逗号分隔,而standby名称是在standby连接到主库时,由连接参数“application_name”指定的。要使用同步复制,standby库中的recovery.conf里的primary_conninfo一定要指定“application_name”。

配置步骤:
在主库上配置:
host    replication     postgres       186.168.100.0/24                  trust

postgresql.conf设置:
listen_addresses = '*'
max_wal_senders = 5
wal_level = hot_standby
增加:
synchronous_standby_names='standby01'      --在最后只需reload


standby库的操作:
[postgres@pghs ~]$ pg_basebackup -h 186.168.100.14 -U postgres -F p -P -x -R -D /PostgreSQL/9.6.1/datass -l  postgresbackup20170210
45098/45098 kB (100%),2/2 tablespaces

修改recovery.conf
standby_mode = 'on'
primary_conninfo = 'application_name=standby01 user=postgres host=186.168.100.14 port=5432 sslmode=disable sslcompression=1'

在主库reload,使synchronous_standby_names='standby01'  生效

起备库:
[postgres@pghs datass]$ pg_ctl start -D /PostgreSQL/9.6.1/datass


测试:
主库创建新表及插入数据:
postgres=# create table testss(id int,name varchar(10));
CREATE TABLE
postgres=# insert into testss values(1,'dxmy');
INSERT 0 1

目标端查看:
[postgres@pghs ~]$ psql
psql (9.6.1)
Type "help" for help.

postgres=# select * from testss;
 id | name
----+------
  1 | dxmy
(1 row)

postgres=

模拟备库挂掉:
[postgres@pghs ~]$ pg_ctl stop -D /PostgreSQL/9.6.1/datass/
waiting for server to shut down.... done
server stopped

在主库插入数据尝试:
postgres=# insert into testss values(2,'dxmy');
一直卡着,不完成

备库起来:
postgres=# insert into testss values(2,'dxmy');
INSERT 0 1
立马成功!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读