Postgresql 流复制同步模式

Postgresql 流复制同步模式

这里配基于同步复制的Hot-Standby 就非常容易了,简单的来讲基于第6个教程只需要修改两个地方:


编辑 postgresql.conf 以支持 WAL archiving:
wal_level = archive
archive_mode = on
archive_command = 'cp %p /home/postgres/archive/%f'
max_wal_senders = 1
里增加配置:
synchronous_standby_names = 'mydb_standby1'


以及B. 在“3.配置Warm-standby,并启动”中:
然后编辑recovery.conf里修改primary_conninfo 配置为:
standby_mode = on
restore_command = 'cp /home/postgres/archive/%f %p'
primary_conninfo = 'host=10.211.55.4 port=5432 user=repuser password=123456 application_name=mydb_standby1'
trigger_file = '/home/postgres/trigger/pgsql.trigger.6432'




这个时候我们看主库的报警日志
LOG:  shutting down
LOG:  database system is shut down
LOG:  database system was shut down at 2015-08-15 04:26:43 CST
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started
LOG:  connection received: host=10.211.55.3 port=23237
LOG:  replication connection authorized: user=repuser
LOG:  standby "mydb_standby1" is now the synchronous standby with priority 1
LOG:  connection received: host=[local]
LOG:  connection authorized: user=postgres database=postgres


可以看到备库已经处于了同步模式中


我们后面如何的去修改这种同步,异步的模式呢?
就只需要去修改
synchronous_standby_names = 'mydb_standby1'这个参数
然后在主库重新装在配置 pg_ctl reload
LOG:  connection received: host=[local]
LOG:  connection authorized: user=postgres database=postgres
LOG:  received SIGHUP, reloading configuration files
LOG:  parameter "synchronous_standby_names" removed from configuration file, reset to default




然后我们测试下,在备库不能够使用的情况下,主库的状态
可以看到,主库的状态是可以开启的状态
postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 kiwi      | kiwi     | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 test      | kiwi     | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
(5 rows)



但是我们这个时候如果进行操作,比如创建一个数据库的操作
postgres=# create database test1 with owner kiwi;
这个时候可以看到,这个数据库处于hang住的状态,操作不能够执行
这个就是同步复制的最大好处,能够永远保证主备课数据的一致性
我们cancel掉这步操作,可以看到这样的报错信息
postgres=# create database test1 with owner kiwi;
^CCancel request sent
WARNING:  canceling wait for synchronous replication due to user request
DETAIL:  The transaction has already committed locally, but might not have been replicated to the standby.
CREATE DATABASE


如果我们这个时候再把备库启动
postgres=# create database test1 with owner kiwi;
ERROR:  database "test1" already exists
postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 kiwi      | kiwi     | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 test      | kiwi     | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 test1     | kiwi     | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
(6 rows)


可以看到虽然我们已经cancel掉了这步的操作,但是我们test1数据库还是创建成功了。


结论:在同步模式下,虽然能够保证备库的数据的不丢失,但是很显然的,存在了备库会影响主库的问题,更多了一层的风险。所以在实际的生产环境中,我们一般不去采用这种同步复制的方式。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值