您的位置:首页 > 数据库

Postgresql 流复制同步模式

2016-03-11 11:13 453 查看
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数据库还是创建成功了。

结论:在同步模式下,虽然能够保证备库的数据的不丢失,但是很显然的,存在了备库会影响主库的问题,更多了一层的风险。所以在实际的生产环境中,我们一般不去采用这种同步复制的方式。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: