master 192.168.2.21 5432
slave 192.168.2.22 5432
目录 /var/lib/pgsql/10/data/
psql
create role rep login replication encrypted password '1qaz2wsx';
在/var/lib/pgsql/10/data/pg_hba.conf增加
host replication rep 192.168.2.22/32 md5
cd /var/lib/pgsql/10/data/
vi recovery.done
recovery_target_timeline = 'latest'
standby_mode = on
primary_conninfo = 'host=192.168.2.21 port=5432 user=rep password=1qaz2wsx'
# recovery_min_apply_delay = 0 #延迟多少分钟应用,用户可以配置延迟的备库,例如给一点误操作的缓冲时间。在备库不会这么早被应用。
启动主机
安装从机之后,不要初始化和启动数据库
mkdir /var/lib/pgsql/10/data/
chown postgres:postgres /var/lib/pgsql/10/data/
chmod 700 /var/lib/pgsql/10/data/
su - postgres
pg_basebackup -D /var/lib/pgsql/10/data/ -F p -X stream -h 192.168.2.21 -p 5432 -U rep
根据从机配置适当修改/var/lib/pgsql/10/data/postgresql.conf
cd /var/lib/pgsql/10/data/
mv recovery.done recovery.conf
service postgresql-10 restart
su - postgres
psql
postgres=# \x
Expanded display is on.
postgres=# select pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), sent_lsn)) as sent_delay,
pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), write_lsn)) as write_delay,
pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), flush_lsn)) as flush_delay,
pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), replay_lsn)) as replay_delay,
*
from pg_stat_replication;
-[ RECORD 1 ]----+------------------------------
sent_delay | 0 bytes
write_delay | 0 bytes
flush_delay | 0 bytes
replay_delay | 0 bytes
pid | 25628
usesysid | 16399
usename | rep
application_name | walreceiver
client_addr | 192.168.2.22
client_hostname |
client_port | 59674
backend_start | 2018-01-03 17:57:00.690789+08
backend_xmin |
state | streaming
sent_lsn | 0/F787EA30
write_lsn | 0/F787EA30
flush_lsn | 0/F787EA30
replay_lsn | 0/F787EA30
write_lag | 00:00:00.000259
flush_lag | 00:00:00.013562
replay_lag | 00:00:00.013563
sync_priority | 0
sync_state | async
su - postgres
psql
postgres=# select pg_is_wal_replay_paused();
-[ RECORD 1 ]-----------+--
pg_is_wal_replay_paused | f
f表示没有暂停,t表示暂停
postgres=# select pg_last_wal_receive_lsn();
-[ RECORD 1 ]-----------+-----------
pg_last_wal_receive_lsn | 0/FFA88FB8
postgres=# select pg_last_wal_replay_lsn();
-[ RECORD 1 ]----------+---------
pg_last_wal_replay_lsn | 1/1EC488
postgres=# \x
Expanded display is on.
postgres=# select * from pg_stat_get_wal_receiver();
-[ RECORD 1 ]---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
pid | 11648
status | streaming
receive_start_lsn | 0/3A000000
receive_start_tli | 1
received_lsn | 1/2DE1E28
received_tli | 1
last_msg_send_time | 2018-01-04 17:20:38.645492+08
last_msg_receipt_time | 2018-01-04 17:20:38.640939+08
latest_end_lsn | 1/2DE1E28
latest_end_time | 2018-01-04 17:20:38.645492+08
slot_name |
conninfo | user=rep password=******** dbname=replication host=192.168.2.21 port=5432 fallback_application_name=walreceiver sslmode=prefer sslcompression=1 krbsrvname=postgres target_session_attrs=any
select pg_wal_replay_pause();
-[ RECORD 1 ]-------+-
pg_wal_replay_pause |
postgres=# select pg_is_wal_replay_paused();
-[ RECORD 1 ]-----------+--
pg_is_wal_replay_paused | t
postgres=# select * from pg_stat_get_wal_receiver();
-[ RECORD 1 ]---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
pid | 11648
status | streaming
receive_start_lsn | 0/3A000000
receive_start_tli | 1
received_lsn | 1/71D3E88
received_tli | 1
last_msg_send_time | 2018-01-04 17:23:52.302411+08
last_msg_receipt_time | 2018-01-04 17:23:52.297611+08
latest_end_lsn | 1/71D3E88
latest_end_time | 2018-01-04 17:23:52.302411+08
slot_name |
conninfo | user=rep password=******** dbname=replication host=192.168.2.21 port=5432 fallback_application_name=walreceiver sslmode=prefer sslcompression=1 krbsrvname=postgres target_session_attrs=any
postgres=# select pg_wal_replay_resume();
-[ RECORD 1 ]--------+-
pg_wal_replay_resume |
postgres=# select pg_is_wal_replay_paused();
-[ RECORD 1 ]-----------+--
pg_is_wal_replay_paused | f
方法有:使用slot、配置足够大的wal keep、wal归档。
配置方法
主 postgresql.conf
max_replication_slots = 10
备 recovery.conf
primary_slot_name = ''
当备库挂了或者不再使用,而忘记删除对应的SLOT时,可能导致主库WAL无限膨胀。
配置方法
主 postgresql.conf
wal_keep_segments = 1024
会导致主库的WAL预留足够的个数,占用一定空间(walkeepsegments*16M = 16G)。
配置主库的归档,同时备库需要能获取到已归档的WAL文件。
配置方式
主 postgresql.conf
archive_mode = on
archive_command = '......'
备 recovery.conf
restore_command = '....'
由于recovery.conf是明文,需要保护好recovery.conf文件的访问权限
或者限制只允许特定的从机访问