find / -name recovery.conf.sample
/root/postgresql/postgresql-9.2.20/src/backend/access/transam/recovery.conf.sample
/usr/pgsql-9.4/share/recovery.conf.sample
复制
cp /usr/pgsql-9.4/share/recovery.conf.sample /var/lib/pgsql/9.4/data/recovery.conf
16. 然后编辑recovery.conf:
standby_mode = on
restore_command = 'cp /var/lib/pgsql/9.4/archive/%f %p' #这个参数,我还需要确认具体含义
primary_conninfo = 'host=192.168.0.136 port=5432 user=replica password=replica123' # 主服务器的信息以及连接的用户,这条信息最重要
recovery_target_timeline = 'latest'
17. 拷贝下面配置文件
cp /var/lib/pgsql/9.4/data.bk/postgresql.conf /var/lib/pgsql/9.4/data/postgresql.conf
cp /var/lib/pgsql/9.4/data.bk/pg_hba.conf /var/lib/pgsql/9.4/data/pg_hba.conf
18. 然后编辑pstgresql.conf:
hot_standby = on
19. 启动Hot-Standby:
/usr/pgsql-9.4/bin/postmaster -D /var/lib/pgsql/9.4/data --port=5432
20. 验证是否部署成功
在主节点上执行,验证已经成功搭建,说明5.160是从服务器,在接收流,而且是异步流复制。
postgres=# select client_addr,sync_state from pg_stat_replication;
client_addr | sync_state
-------------+------------
192.168.0.160 | async
21. 查询更多数据同步信息:
postgres=# select usename,application_name,client_addr,client_port,backend_start,backend_xmin,state,sent_location,write_location,flush_location,replay_location,sync_priority,sync_state from pg_stat_replication;
usename | application_name | client_addr | client_port | backend_start | backend_xmin | state | sent_location | write_location | flush_location | replay_location | sync_priority | sync_state
---------+------------------+-------------+-------------+-------------------------------+--------------+-----------+---------------+----------------+----------------+-----------------+---------------+------------
replica | walreceiver | 192.168.0.160 | 39375 | 2017-09-05 17:49:22.512393+08 | | streaming | 5/1049488 | 5/1049488 | 5/1049488 | 5/1049488 | 0 | async
22. 此外,还可以分别在主、从节点上运行 ps aux | grep postgres 来查看进程:
主服务器上,可以看到有一个 wal sender 进程,还有archiver进程等
# ps aux | grep postgres
postgres 1104 0.0 0.1 324652 15120 ? S 14:26 0:00 /usr/pgsql-9.4/bin/postmaster -D /var/lib/pgsql/9.4/data
postgres 1111 0.0 0.0 324652 5844 ? Ss 14:26 0:00 postgres: wal writer process
postgres 1113 0.0 0.0 179796 1544 ? Ss 14:26 0:00 postgres: archiver process last was 000000010000000500000000.00000060.backup
postgres 8515 0.0 0.0 325448 3108 ? Ss 17:49 0:00 postgres: wal sender process replica 192.168.0.160(39375) streaming 5/103A1D0
从服务器上,可以看到 wal receiver 进程,和recovering进程正在恢复archive log
$ ps aux | grep postgres
postgres 11508 0.0 0.1 324684 15128 ? S 17:49 0:00 /usr/pgsql-9.4/bin/postmaster -D /var/lib/pgsql/9.4/data
postgres 11510 0.0 0.0 324796 4336 ? Ss 17:49 0:00 postgres: startup process recovering 000000010000000500000001
postgres 11513 0.0 0.0 331892 3700 ? Ss 17:49 0:00 postgres: wal receiver process streaming 5/103A1D0
23. 可以在下面路径中,看到从库接收的archive log文件
# pwd
/var/lib/pgsql/9.4/data/pg_xlog
# ls
000000010000000500000001 000000010000000500000002 archive_status
至此,PostgreSQL主从流复制安装部署完成。
在主服务器上插入数据或删除数据,在从服务器上能看到相应的变化。从服务器上只能查询,不能插入或删除数据。
24. 第12、13、14步骤,可以通过另一种拷贝主库到从库的方法,pg_basebackup命令拷贝文件
pg_basebackup -F p --progress -D /var/lib/pgsql/9.4/data2 -h 192.168.0.136 -p 5432 -U replica --password
进入到/var/lib/pgsql/9.4/data2目录,修改recovery.conf,这个文件可以从pg的安装目录的share文件夹中获取,比如
cp /usr/pgsql-9.6/share/recovery.conf.sample /var/lib/pgsql/9.6/data2/recovery.conf
确保文件夹权限是700,这个很关键,其他权限,不能正常启动
$ chmod 0700 /var/lib/pgsql/9.6/data2
使用下面命令启动standby
$ /usr/pgsql-9.6/bin/postmaster -D /var/lib/pgsql/9.6/data2