如何在不停机的情况下向现有的 PostgreSQL 级联复制添加额外的从属服务器?
步骤 1:将第 3 个从属信息放在从属 2 的 pg_hba.conf 中,如果这里没有 recovery.conf 文件,请立即创建
cat recovery.conf standby_mode='on' primary_conninfo = 'host=192.168.2.3 port=5432 user=postgres password=postgres' #restore_command = 'cp /archive/%f "%p"' #archive_cleanup_command = '/usr/pgsql-9.4/bin/pg_archivecleanup /archive %r' recovery_target_timeline = 'latest'
Pg_hba.conf:
host replication postgres 192.168.2.5/32 trust host replication postgres 192.168.2.4/32 trust
步骤2. reload或者重新启动slave_2服务器:
Option 1: From the command-line shell su - postgres /usr/bin/pg_ctl -D /DATA reload Option 2: Using SQL SELECT pg_reload_conf();
步骤3:暂停slave_2服务器,然后检查是否暂停
select pg_wal_replay_pause(); select pg_is_wal_replay_paused();
注意:如果您的服务器正在测试,则在暂停slave_2后,只需在主服务器上创建一些表,因为恢复后您需要检查slave_2和slave_3是否复制了数据
步骤4:在slave_2上进行物理备份并将其发送到slave_3:
ON SLAVE_3: /usr/pgsql-10/bin/pg_ctl -D /var/lib/pgsql/10/data/ stop -----stop the slave_3 cluster rm -vfr /var/lib/pgsql/10/data/* ---remove the old data's
FROM SLAVE_2: rsync -cva --inplace /DATA/* postgres@192.168.2.5:/var/lib/pgsql/10/data/
slave_3 pg_hba.conf: host replication postgres 192.168.2.4/3 2 trust
Slave_3 postgresql.conf: listen_addresses = 'localhost,192.168.2.5' port = 5432 wal_level = 'hot_standby' # minimal, archive, hot_standby, or logical max_wal_senders = 10 # max number of walsender processes # (change requires restart) wal_keep_segments = 64 # in logfile segments, 16MB each; 0 disables hot_standby = on archive_mode = on # allows archiving to be done archive_command = 'rsync -av %p /archive/%f && rsync -av %p postgres@172.18.2.190:/archive/%f' # or 'cd .'
slave_3 recovery.conf: cat /var/lib/pgsql/10/data/recovery.conf standby_mode = 'on' # to enable the standby (read-only) mode. primary_conninfo = 'host=192.168.2.4 port=5432 user=postgres password=postgres' # to specify a connection info to the master node.
步骤5:恢复slave_2服务器然后重新启动slave_3服务器:
At slave_2: select pg_wal_replay_resume(); At Slave_3: /usr/pgsql-10/bin/pg_ctl -D /var/lib/pgsql/10/data/ start
监控 SLAVE_3 和 MASTER 服务器:
ON SLAVE_3: postgres=# select * from pg_stat_wal_receiver ; pid | status | receive_start_lsn | receive_start_tli | received_lsn | received_tli | last_msg_send_time | last_msg_receipt_time | latest_end_lsn | latest_end_time | slot_name | conninfo -------+-----------+-------------------+-------------------+--------------+--------------+--------------------------------+----------------------------------+----------------+----------------------------------+-----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 39970 | streaming | 0/1A000000 | 1 | 0/1A000000 | 1 | 2018-03-15 16:39:03.4787+05:30 | 2018-03-15 16:39:01.426584+05:30 | 0/1A30B500 | 2018-03-15 16:37:03.161287+05:30 | | user=postgres password=******** dbname=replication host=192.168.2.4 port=5432 fallback_application_name=walreceiver sslmode=prefer sslcompression=1 krbsrvname=postgres target_session_attrs=any (1 row)
ON MASTER: select * from pg_stat_replication ; pid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | backend_xmin | state | sent_lsn | write_lsn | flush_lsn | replay_lsn | write_lag | flush_lag | replay_lag | sync_priority | sync_state -------+----------+----------+------------------+-------------+-----------------+-------------+----------------------------------+--------------+-----------+------------+------------+------------+------------+-----------+-----------+------------+---------------+------------ 14640 | 10 | postgres | walreceiver | 192.168.2.3 | | 57716 | 2018-03-15 09:33:48.617764+05:30 | | streaming | 0/1A30B500 | 0/1A30B500 | 0/1A30B500 | 0/1A30B500 | | | | 0 | async (1 row)