1、配置环境:
示例环境 |
|||||
主机名 |
IP地址 |
角色 |
系统版本 |
数据目录 |
pg版本 |
pg-1 |
10.0.2.60 |
主库 |
RedHat6.4 |
/usr/local/postgresql-9.4.4/data |
9.4 |
pg-2 |
10.0.2.61 |
standby |
RedHat6.4 |
/usr/local/postgresql-9.4.4/data |
9.4 |
2、主数据库配置:
使用流复制,需要允许主库接受流复制的连接,需在pg_hba.conf中做如下配置:
host replication postgre 10.0.2.0/24 md5
建的是使用流复制的hot standby,需要修改postgresql.conf:
listen_addresses = '*'
max_wal_senders = 4
wal_level = hot_standby
3、在Standby上生成基础备份:
[postgre@pg-2 postgresql-9.4.4]$ pg_basebackup -h 10.0.2.60 -U postgre -Fp-P -R -x -D ./data -l postgresqlbackup20150717
Password:
204221/204221 kB (100%), 1/1 tablespace
查看data下拷贝过来的文件:
[postgre@pg-2 data]$ ls -la
total 136
drwx------. 20 postgre postgre 4096 Jul 17 11:30 .
drwxrwxr-x. 7 postgre postgre 4096 Jul 7 15:32 ..
drwx------. 20 postgre postgre 4096 Jul 17 11:30 backup
-rw-------. 1 postgre postgre 207 Jul 17 11:30 backup_label
drwx------. 6 postgre postgre 4096 Jul 17 11:30 base
drwx------. 2 postgre postgre 4096 Jul 17 11:30 global
-rw-------. 1 postgre postgre 684 Jul 17 11:30 logfile
drwx------. 2 postgre postgre 4096 Jul 17 11:30 pg_clog
drwx------. 2 postgre postgre 4096 Jul 17 11:30 pg_dynshmem
-rw-------. 1 postgre postgre 4671 Jul 17 11:30 pg_hba.conf
-rw-------. 1 postgre postgre 1636 Jul 17 11:30 pg_ident.conf
drwx------. 3 postgre postgre 4096 Jul 17 11:30 pg_log
drwx------. 4 postgre postgre 4096 Jul 17 11:30 pg_logical
drwx------. 4 postgre postgre 4096 Jul 17 11:30 pg_multixact
drwx------. 2 postgre postgre 4096 Jul 17 11:30 pg_notify
drwx------. 2 postgre postgre 4096 Jul 17 11:30 pg_replslot
drwx------. 2 postgre postgre 4096 Jul 17 11:30 pg_serial
drwx------. 2 postgre postgre 4096 Jul 17 11:30 pg_snapshots
drwx------. 2 postgre postgre 4096 Jul 17 11:30 pg_stat
drwx------. 2 postgre postgre 4096 Jul 17 11:30 pg_stat_tmp
drwx------. 2 postgre postgre 4096 Jul 17 11:30 pg_subtrans
drwx------. 2 postgre postgre 4096 Jul 17 11:30 pg_tblspc
-rw-rw-r--. 1 postgre postgre 0 Jul 17 11:30pgtest.sql
drwx------. 2 postgre postgre 4096 Jul 17 11:30 pg_twophase
-rw-------. 1 postgre postgre 4 Jul 17 11:30 PG_VERSION
drwx------. 3 postgre postgre 4096 Jul 17 11:30 pg_xlog
-rw-------. 1 postgre postgre 88 Jul 17 11:30postgresql.auto.conf
-rw-------. 1 postgre postgre 21260 Jul 17 11:30 postgresql.conf
-rw-rw-r--. 1 postgre postgre 129 Jul 17 11:30 recovery.conf
由于使用了-R 参数,所以生成了recovery.conf 文件,内容如下:
[postgre@pg-2 data]$ cat recovery.conf
standby_mode = 'on'
primary_conninfo = 'user=postgre password=postgre host=10.0.2.60 port=5432sslmode=disable sslcompression=1'
4、启动standby
在启动之前,需要修改postgresql.conf:
hot_standby = on
启动standby :
[postgre@pg-2 postgresql-9.4.4]$ pg_ctl -D /usr/local/postgresql-9.4.4/data start
登录:
[postgre@pg-2 postgresql-9.4.4]$ psql postgres
psql (9.4.4)
Type "help" for help.
5、测试:
在主库上建一个测试表,然后插入数据:
[postgre@pg-1 data]$ psql postgres
psql (9.4.4)
Type "help" for help.
postgres=# create table hotstandby (id int primary key,name varchar(30));
CREATE TABLE
postgres=# insert into hotstandby values (1,'001');
INSERT 0 1
postgres=# insert into hotstandby values (2,'002');
INSERT 0 1
postgres=# select * from hotstandby ;
id | name
----+------
1 | 001
2 | 002
(2 rows)
postgres=#
standby库上查询:
postgres=# select * from hotstandby ;
id | name
----+------
1 | 001
2 | 002
(2 rows)
在测试库上做修改(因为Hot Standby 是只读的,所以如果在Standby上做操作会失败):
postgres=# delete from hotstandby where id =1;
ERROR: cannot execute DELETE in aread-only transaction
postgres=# drop table hoststandby ;
ERROR: cannot execute DROP TABLE in a read-only transaction
6、检查异步流复制的情况:
可以使用主库上的视图pg_stat_replication查看流复制的信息:
postgres=# select pid,state,client_addr,sync_priority,sync_state from pg_stat_replication;
pid | state | client_addr | sync_priority |sync_state
------+-----------+-------------+---------------+------------
9552 | streaming | 10.0.2.61 | 0 | async
(1 row)
查看备库落后主库多少字节的WAL日志:
postgres=# select pg_xlog_location_diff(pg_current_xlog_location(),replay_location)from pg_stat_replication;
pg_xlog_location_diff
-----------------------
0
(1 row)
7、查看备库状态:
如何判断数据库处于备库状态:
1)hotstandby 状态:
可以连接数据库执行pg_is_in_recovery()函数,主库上返回False,standby上返回true;
主库:
postgres=# select pg_is_in_recovery();
pg_is_in_recovery
-------------------
f
(1 row)
standby库:
postgres=# select pg_is_in_recovery();
pg_is_in_recovery
-------------------
t
(1 row)
2)不是hotstandby:
不是host standby不能直接连上去,此时使用pg_controldata来判断:
从库:
[postgre@pg-2 postgresql-9.4.4]$pg_controldata
pg_control version number: 942
Catalog version number: 201409291
Database system identifier: 6167591978200871938
Database cluster state: in archive recovery
pg_control last modified: Fri 17Jul 2015 12:34:32 PM CST
Latest checkpoint location: 0/1B013A78
Prior checkpoint location: 0/1B0139A0
Latest checkpoint's REDO location: 0/1B013A40
Latest checkpoint's REDO WAL file: 00000001000000000000001B
Latest checkpoint's TimeLineID: 1
Latest checkpoint's PrevTimeLineID: 1
Latest checkpoint's full_page_writes: on
Latest checkpoint's NextXID: 0/1878
Latest checkpoint's NextOID: 24610
Latest checkpoint's NextMultiXactId: 1
Latest checkpoint's NextMultiOffset: 0
Latest checkpoint's oldestXID: 1801
Latest checkpoint's oldestXID's DB: 1
Latest checkpoint's oldestActiveXID: 1878
Latest checkpoint's oldestMultiXid: 1
Latest checkpoint's oldestMulti's DB: 1
Time of latest checkpoint: Fri 17Jul 2015 12:29:55 PM CST
Fake LSN counter for unlogged rels: 0/1
Minimum recovery ending location: 0/1B0132A8
Min recovery ending loc's timeline: 1
Backup start location: 0/0
Backup end location: 0/0
End-of-backup record required: no
Current wal_level setting: hot_standby
Current wal_log_hints setting: off
Current max_connections setting: 100
Current max_worker_processes setting: 8
Current max_prepared_xacts setting: 0
Current max_locks_per_xact setting: 64
Maximum data alignment: 8
Database block size: 8192
Blocks per segment of large relation: 131072
WAL block size: 8192
Bytes per WAL segment: 16777216
Maximum length of identifiers: 64
Maximum columns in an index: 32
Maximum size of a TOAST chunk: 1996
Size of a large-object chunk: 2048
Date/time type storage: 64-bit integers
Float4 argument passing: by value
Float8 argument passing: by value
Data page checksum version: 0
主库:
[postgre@pg-1 data]$ pg_controldata
pg_control version number: 942
Catalog version number: 201409291
Database system identifier: 6167591978200871938
Database cluster state: in production
pg_control last modified: Fri 17Jul 2015 12:34:56 PM CST
Latest checkpoint location: 0/1B013B50
Prior checkpoint location: 0/1B013A78
Latest checkpoint's REDO location: 0/1B013B18
Latest checkpoint's REDO WAL file: 00000001000000000000001B
Latest checkpoint's TimeLineID: 1
Latest checkpoint's PrevTimeLineID: 1
Latest checkpoint's full_page_writes: on
Latest checkpoint's NextXID: 0/1878
Latest checkpoint's NextOID: 24610
Latest checkpoint's NextMultiXactId: 1
Latest checkpoint's NextMultiOffset: 0
Latest checkpoint's oldestXID: 1801
Latest checkpoint's oldestXID's DB: 1
Latest checkpoint's oldestActiveXID: 1878
Latest checkpoint's oldestMultiXid: 1
Latest checkpoint's oldestMulti's DB: 1
Time of latest checkpoint: Fri 17Jul 2015 12:34:56 PM CST
Fake LSN counter for unlogged rels: 0/1
Minimum recovery ending location: 0/0
Min recovery ending loc's timeline: 0
Backup start location: 0/0
Backup end location: 0/0
End-of-backup record required: no
Current wal_level setting: hot_standby
Current wal_log_hints setting: off
Current max_connections setting: 100
Current max_worker_processes setting: 8
Current max_prepared_xacts setting: 0
Current max_locks_per_xact setting: 64
Maximum data alignment: 8
Database block size: 8192
Blocks per segment of large relation: 131072
WAL block size: 8192
Bytes per WAL segment: 16777216
Maximum length of identifiers: 64
Maximum columns in an index: 32
Maximum size of a TOAST chunk: 1996
Size of a large-object chunk: 2048
Date/time type storage: 64-bit integers
Float4 argument passing: by value
Float8 argument passing: by value
Data page checksum version: 0
8、在HotStandby,还可以执行如下一些函数,查看备库接收的WAL日志和应用WAL日志的状态:
pg_last_xlog_receive_location()
pg_last_xlog_replay_location()
pg_last_xact_replay_timestamp()
如下:
postgres=# select pg_last_xlog_receive_location(),pg_last_xlog_replay_location(),pg_last_xact_replay_timestamp();
pg_last_xlog_receive_location | pg_last_xlog_replay_location |pg_last_xact_replay_timestamp
-------------------------------+------------------------------+-------------------------------
0/1B013F50 | 0/1B013F50 | 2015-07-17 11:41:09.406696+08
(1 row)