posgtresql主从备份

master: 172.30.2.202
slave: 172.30.2.201

一、准备

连接上master节点创建replication用户

# CREATE ROLE replicator with login replication password xxxxxx;

修改白名单pg_hba.conf

# host    replication    all        xxxxx/32    md5

二:配置slave

配置master的备份

$ sudo su - postgres
$ /opt/pgsql/bin/pg_basebackup -X s -h 172.30.2.202 -U replicator -D /data/postgresql/rdrc_95/data/

配置recovery.conf

$ vim /data/postgresql/rdrc_94/data/recovery.conf
standby_mode = 'on'
primary_conninfo = 'host=172.30.2.202 port=5432 user=replicator password=xxxxxxx application_name=node2'
restore_command = ''
archive_cleanup_command = ''

启动

$ /opt/pgsql/bin/pg_ctl -D /data/postgresql/rdrc_95/data/ start

三:查看状态

在master上执行:

$ psql -U postgres
psql (9.5.1)
Type "help" for help.


postgres=# select * from pg_stat_replication ;
  pid  | usesysid |  usename   | application_name | client_addr  | client_hostname | client_port |         backend_start         | backend_xmin |   state   | sent_location | write_location | flush_location | replay_location | sync_priority | sync_state
-------+----------+------------+------------------+--------------+-----------------+-------------+-------------------------------+--------------+-----------+---------------+----------------+----------------+-----------------+---------------+------------
 24304 |    16384 | replicator | node2            | 172.30.2.201 |                 |       59935 | 2015-10-12 06:37:21.794013+00 |         1892 | streaming | 0/62000000    | 0/62000000     | 0/62000000     | 0/62000000      |             0 | async
(1 rows)

从最后的sync_state可以看出两个slave还是异步的

四、修改为同步复制

在master上执行:

postgres=# alter system set synchronous_standby_names = 'node2';
ALTER SYSTEM
postgres=# select pg_reload_conf();
 pg_reload_conf
----------------
 t
(1 row)


postgres=# select * from pg_stat_replication ;
 pid  | usesysid |  usename   | application_name |  client_addr   | client_hostname | client_port |         backend_start         | backend_xmin |   state   | sent_l
ocation | write_location | flush_location | replay_location | sync_priority | sync_state
------+----------+------------+------------------+----------------+-----------------+-------------+-------------------------------+--------------+-----------+-------
--------+----------------+----------------+-----------------+---------------+------------
 9114 |    16384 | replicator | node2            | 172.30.200.121 |                 |       22295 | 2016-03-19 18:25:40.074707+08 |         1831 | streaming | 0/2700
0000    | 0/27000000     | 0/27000000     | 0/27000000      |             1 | sync
(1 row)

可见node2变成了同步节点

五、给master添加VIP

$ sudo ip a a local 172.30.200.103/32 brd + dev eth1
$ sudo arping -q -c 3 -A -I eth1 172.30.200.103



你可能感兴趣的:(posgtresql主从备份)