主庫配置
pg_hba.conf
host replication all 10.2.0.0/0 trust
postgresql.conf
listen_addresses = '*' max_wal_senders = 5 wal_level = hot_standby wal_keep_segments = 256
重啟主庫
從庫配置
安裝
使用yum安裝 (找源 http://yum.postgresql.org/)
yum install https://download.postgresql.org/pub/repos/yum/9.5/redhat/rhel-7-x86_64/pgdg-centos95-9.5-2.noarch.rpm yum install postgresql95-server postgresql95-contrib
生成基礎備份(主數據庫IP 10.2.0.14)
pg_basebackup -h 10.2.0.14 -U postgres -F p -P -x -R -D /var/lib/pgsql/9.5/data/ -l postgresbackup20160506 postgres 10 不在有參數 -x 說明 pg_log, pg_xlog, pg_clog目錄分別重命名為log, pg_wal, pg_xact。
pg_basebackup支持兩種全量備份的方式,
1.以fetch的方式,先備份數據在備份日志
2.以stream的方式,並行的備份數據和日志
pg_basebackup對於全量備份的數據和日志,提供了串行備份和並行備份的方式。fetch模式也就是串行備份需要保證在備份數據的過程中,備份開始時刻的日志需要一直保存下來, 也就說pg的wal_keep_segments需要足夠大去保存日志文件,如果備份數據期間,日志開始時刻的日志已經被移除,那么備份就會失敗。而stream模式,也就是並行備份過程中wal_max_sender必須保證不小於2。 而stream模式不支持,將數據和日志以流的方式輸出到標准輸出。
查看生成文件(注意文件的權限,用戶組)
chown postgres:postgres /var/lib/pgsql/9.5/data/ -R
chmod 700 /var/lib/pgsql/9.5/data/
ls -l /var/lib/pgsql/9.5/data -rw-------. 1 postgres postgres 197 5月 6 13:11 backup_label.old drwx------. 6 postgres postgres 50 5月 6 13:11 base drwx------. 2 postgres postgres 4096 5月 6 13:38 global drwx------. 2 postgres postgres 17 5月 6 13:11 pg_clog drwx------. 2 postgres postgres 6 5月 6 13:11 pg_commit_ts drwx------. 2 postgres postgres 6 5月 6 13:11 pg_dynshmem -rw-------. 1 postgres postgres 4214 5月 6 13:11 pg_hba.conf -rw-------. 1 postgres postgres 1636 5月 6 13:11 pg_ident.conf drwx------. 2 postgres postgres 56 5月 6 13:11 pg_log drwx------. 4 postgres postgres 37 5月 6 13:11 pg_logical drwx------. 4 postgres postgres 34 5月 6 13:11 pg_multixact drwx------. 2 postgres postgres 17 5月 6 13:38 pg_notify drwx------. 2 postgres postgres 6 5月 6 13:11 pg_replslot drwx------. 2 postgres postgres 6 5月 6 13:11 pg_serial drwx------. 2 postgres postgres 6 5月 6 13:11 pg_snapshots drwx------. 2 postgres postgres 6 5月 6 13:11 pg_stat drwx------. 2 postgres postgres 6 5月 6 13:11 pg_stat_tmp drwx------. 2 postgres postgres 17 5月 6 13:11 pg_subtrans drwx------. 2 postgres postgres 6 5月 6 13:11 pg_tblspc drwx------. 2 postgres postgres 6 5月 6 13:11 pg_twophase -rw-------. 1 postgres postgres 4 5月 6 13:11 PG_VERSION drwx------. 3 postgres postgres 89 5月 6 13:29 pg_xlog -rw-------. 1 postgres postgres 88 5月 6 13:11 postgresql.auto.conf -rw-------. 1 postgres postgres 21756 5月 6 13:38 postgresql.conf -rw-------. 1 postgres postgres 59 5月 6 13:38 postmaster.opts -rw-------. 1 postgres postgres 87 5月 6 13:38 postmaster.pid -rw-r--r--. 1 postgres postgres 140 5月 6 13:35 recovery.conf
cat recovery.conf standby_mode = 'on' primary_conninfo = 'user=postgres host=10.2.0.14 port=5432 sslmode=disable sslcompression=1'
修改 postgresql.conf(否則啟動失敗 錯誤:postgresql-9.5.service start operation timed out. Terminating.)
hot_standby=on
啟動從庫
systemctl start postgresql-9.5.service systemctl enable postgresql-9.5.service
測試
主庫操作從庫查看同步結果
psql -U postgres postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- 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 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | (4 rows) postgres=# \c test est=# create table test01(id int primary key,note text); CREATE TABLE test=# \d List of relations Schema | Name | Type | Owner --------+--------+-------+---------- public | test01 | table | postgres (1 row) test=# insert into test01 values(1,'1111'); INSERT 0 1 test=# insert into test01 values(2,'2222'); INSERT 0 1 test=# insert into test01 values(4,'4444'); INSERT 0 1 test=# insert into test01 values(5,'5555'); INSERT 0 1 test=# \q select * from test01; id | note ----+------ 1 | 1111 2 | 2222 4 | 4444 5 | 5555
主從同步狀況查看
主庫 select * from pg_stat_replication ; -[ RECORD 1 ]----+------------------------------ pid | 29362 usesysid | 10 usename | postgres application_name | walreceiver client_addr | 10.2.0.15 client_hostname | client_port | 53730 backend_start | 2018-05-09 15:57:49.191165+08 backend_xmin | state | streaming sent_lsn | 0/BA000140 write_lsn | 0/BA000140 flush_lsn | 0/BA000140 replay_lsn | 0/BA000140 write_lag | flush_lag | replay_lag | sync_priority | 0 sync_state | async
從庫 select * from pg_stat_wal_receiver ; -[ RECORD 1 ]---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- pid | 1559 status | streaming receive_start_lsn | 0/BA000000 receive_start_tli | 1 received_lsn | 0/BA000140 received_tli | 1 last_msg_send_time | 2018-05-09 16:02:19.692318+08 last_msg_receipt_time | 2018-05-09 16:02:19.724085+08 latest_end_lsn | 0/BA000140 latest_end_time | 2018-05-09 15:57:49.194596+08 slot_name | conninfo | user=postgres passfile=/root/.pgpass dbname=replication host=10.2.0.14 port=5432 fallback_application_name=walreceiver sslmode=prefer sslcompression=1 krbsrvname=postgres target_session_attrs=any
