Postgresql后备服务器部署实战
Postgresql后备服务器部署实战这篇文章介绍Postgresql后备服务器的配置使用方法。 1 目录
2 部署架构3 主机配置(主机ID20) sed -ir "s/#*max_replication_slots.*/max_replication_slots= 10/" $PGDATA/postgresql.conf
sed -ir "s/#*max_wal_senders.*/max_wal_senders = 10/" $PGDATA/postgresql.conf
sed -ir "s/#*wal_level.*/wal_level = replica/" $PGDATA/postgresql.conf
sed -ir "s/#*archive_mode.*/archive_mode = on/" $PGDATA/postgresql.conf
sed -ir "s/#*archive_command.*/archive_command = 'test ! -f ${PGHOME}/archive/%f &;&; cp %p ${PGHOME}/archive/%f'/" $PGDATA/postgresql.conf
4 归档冷备服务器配置(ID21) 4.1 制作基础备份(主节点操作)
第一步:配置pg_hba.conf通道 第二步: 4.2 配置归档恢复cp $PGHOME/share/recovery.conf.sample ./recovery.conf
sed -ir "s/#*standby_mode.*/standby_mode= on/" $PGDATA/recovery.conf
sed -ir "s/#*restore_command.*/restore_command = 'cp /home/gaomingjie/app/pgsql20/archive/%f %p'/" $PGDATA/recovery.conf
备机日志(不停的获取最新日志): cp: cannot stat `/home/gaomingjie/app/pgsql20/archive/000000010000000000000004': No such file or directory
cp: cannot stat `/home/gaomingjie/app/pgsql20/archive/000000010000000000000004': No such file or directory
cp: cannot stat `/home/gaomingjie/app/pgsql20/archive/000000010000000000000004': No such file or directory
cp: cannot stat `/home/gaomingjie/app/pgsql20/archive/000000010000000000000004': No such file or directory
cp: cannot stat `/home/gaomingjie/app/pgsql20/archive/000000010000000000000004': No such file or directory
LOG: restored log file "000000010000000000000004" from archive
cp: cannot stat `/home/gaomingjie/app/pgsql20/archive/000000010000000000000005': No such file or directory
cp: cannot stat `/home/gaomingjie/app/pgsql20/archive/000000010000000000000005': No such file or directory
cp: cannot stat `/home/gaomingjie/app/pgsql20/archive/000000010000000000000005': No such file or directory
进程状态: /home/gaomingjie/app/pgsql21/bin/postgres
_ postgres: startup process recovering 000000010000000000000005
_ postgres: checkpointer process
_ postgres: writer process
5 流复制冷备服务器配置(ID22)
5.1 制作基础备份(主节点操作)第一步:配置pg_hba.conf通道
配置方法1(本例中不使用这种配置方法):
pg_hba.conf:
host replication gaomingjie 127.0.0.1/32 trust
配置方法2(创建用户后使用密码校验):
create role foo login replication password 'server@123';
pg_hba.conf:
host replication foo 127.0.0.1/32 md5
第二步: 4.2 配置流复制参数cp $PGHOME/share/recovery.conf.sample ./recovery.conf
sed -ir "s/#*standby_mode.*/standby_mode= on/" $PGDATA/recovery.conf
sed -ir "s/#*primary_conninfo.*/primary_conninfo= 'host=127.0.0.1 port=9420 user=foo password=server@123'/" $PGDATA/recovery.conf
日志信息 LOG: database system was shut down in recovery at 2017-04-27 11:46:42 CST
LOG: entering standby mode
LOG: redo starts at 0/6000028
LOG: consistent recovery state reached at 0/7000000
LOG: started streaming WAL from primary at 0/7000000 on timeline 1
进程状态 /home/gaomingjie/app/pgsql22/bin/postgres
_ postgres: startup process recovering 000000010000000000000007
_ postgres: checkpointer process
_ postgres: writer process
_ postgres: wal receiver process streaming 0/7000140
4.3 监控流复制状态
postgres=# select * from pg_stat_replication;
-[ RECORD 1 ]----+-----------------------------
pid | 11715
usesysid | 16393
usename | foo
application_name | walreceiver
client_addr | 127.0.0.1
client_hostname |
client_port | 51930
backend_start | 2017-04-27 14:12:57.43909+08
backend_xmin |
state | streaming
sent_location | 0/8000610
write_location | 0/8000610
flush_location | 0/8000610
replay_location | 0/8000610
sync_priority | 0
sync_state | async
5 流复制槽热备服务器配置(级联主)(ID23) 5.1 制作基础备份(主节点操作)第一步:权限配置 create role foo login replication password 'server@123';
pg_hba.conf:
host replication foo 127.0.0.1/32 md5
第二步: 第三步:主节点创建流复制槽 SELECT * FROM pg_create_physical_replication_slot('node_slot_23');
SELECT * FROM pg_replication_slots;
-[ RECORD 1 ]-------+-------------
slot_name | node_slot_23
plugin |
slot_type | physical
datoid |
database |
active | f
active_pid |
xmin |
catalog_xmin |
restart_lsn |
confirmed_flush_lsn |
5.2 配置流复制参数sed -ir "s/#*hot_standby.*/hot_standby= on/" $PGDATA/postgresql.conf
cp $PGHOME/share/recovery.conf.sample ./recovery.conf
sed -ir "s/#*standby_mode.*/standby_mode= on/" $PGDATA/recovery.conf
sed -ir "s/#*primary_conninfo.*/primary_conninfo= 'host=127.0.0.1 port=9420 user=foo password=server@123'/" $PGDATA/recovery.conf
sed -ir "s/#*primary_slot_name.*/primary_slot_name= 'node_slot_23'/" $PGDATA/recovery.conf
日志信息 LOG: entering standby mode
LOG: redo starts at 0/9000028
LOG: consistent recovery state reached at 0/A000060
LOG: invalid record length at 0/A000060: wanted 24,got 0
LOG: database system is ready to accept read only connections
LOG: started streaming WAL from primary at 0/A000000 on timeline 1
进程状态 /home/gaomingjie/app/pgsql23/bin/postgres
_ postgres: startup process recovering 00000001000000000000000A
_ postgres: checkpointer process
_ postgres: writer process
_ postgres: stats collector process
_ postgres: wal receiver process
主节点查询流复制槽状态 psql
psql (9.6.0)
Type "help" for help.
postgres=# x
Expanded display is on.
postgres=# SELECT * FROM pg_replication_slots;
-[ RECORD 1 ]-------+-------------
slot_name | node_slot_23
plugin |
slot_type | physical
datoid |
database |
active | t
active_pid | 14799
xmin |
catalog_xmin |
restart_lsn | 0/B001148
confirmed_flush_lsn |
psql -U foo -W "dbname=postgres replication=database"
Password for user foo:
psql (9.6.0)
Type "help" for help.
postgres=> IDENTIFY_SYSTEM;
systemid | timeline | xlogpos | dbname
---------------------+----------+-----------+----------
6413518490021561706 | 1 | 0/B001148 | postgres
(1 row)
5.3 流复制槽概念
5.3.1 查询和操纵复制槽每个复制槽都有一个名字,名字可以包含小写字母、数字和下划线字符。已有的复制槽和它们的状态可以在 pg_replication_slots 视图中看到。 流复制槽相关函数 pg_create_physical_replication_slot pg_drop_replication_slot pg_create_logical_replication_slot pg_logical_slot_get_changes pg_logical_slot_peek_changes pg_logical_slot_get_binary_changes pg_logical_slot_peek_binary_changes pg_replication_origin_create pg_replication_origin_drop pg_replication_origin_oid pg_replication_origin_session_setup pg_replication_origin_session_reset pg_replication_origin_session_is_setup pg_replication_origin_session_progress pg_replication_origin_xact_setup pg_replication_origin_xact_reset pg_replication_origin_advance pg_replication_origin_progress pg_logical_emit_message 6 级联备服务器配置(ID24) 6.1 制作基础备份(主节点23操作)
注:这里连接23去做基础备份。 6.2 配置流复制参数sed -ir "s/#*standby_mode.*/standby_mode= on/" $PGDATA/recovery.conf
sed -ir "s/#*primary_conninfo.*/primary_conninfo= 'host=127.0.0.1 port=9423 user=foo password=server@123'/" $PGDATA/recovery.conf
日志信息 LOG: database system was interrupted while in recovery at log time 2017-04-28 11:13:33 CST
HINT: If this has occurred more than once some data might be corrupted and you might need to choose an earlier recovery target.
LOG: entering standby mode
LOG: redo starts at 0/B00D958
LOG: consistent recovery state reached at 0/B00DA38
LOG: invalid record length at 0/B00DA38: wanted 24,got 0
LOG: database system is ready to accept read only connections
LOG: started streaming WAL from primary at 0/B000000 on timeline 1
进程状态 /home/gaomingjie/app/pgsql23/bin/postgres
_ postgres: startup process recovering 00000001000000000000000B
_ postgres: checkpointer process
_ postgres: writer process
_ postgres: stats collector process
_ postgres: wal receiver process streaming 0/B00DBF8
_ postgres: wal sender process foo 127.0.0.1(58019) streaming 0/B00DBF8
/home/gaomingjie/app/pgsql24/bin/postgres
_ postgres: startup process recovering 00000001000000000000000B
_ postgres: checkpointer process
_ postgres: writer process
_ postgres: stats collector process
_ postgres: wal receiver process streaming 0/B00DBF8
7 流复制同步热备服务器配置(开启连续归档)(ID25)
7.1 制作基础备份(主节点20操作)第一步:权限配置 create role foo login replication password 'server@123';
pg_hba.conf:
host replication foo 127.0.0.1/32 md5
第二步: 注:这里连接20去做基础备份。 第三步:修改synchronous_standby_names参数。 sed -ir "s/#*synchronous_standby_names.*/synchronous_standby_names= '1 (s1)'/" $PGDATA/postgresql.conf
7.2 配置流复制参数sed -ir "s/#*hot_standby.*/hot_standby= on/" $PGDATA/postgresql.conf
cp $PGHOME/share/recovery.conf.sample ./recovery.conf
sed -ir "s/#*standby_mode.*/standby_mode= on/" $PGDATA/recovery.conf
sed -ir "s/#*primary_conninfo.*/primary_conninfo= 'application_name=s1 host=127.0.0.1 port=9420 user=foo password=server@123'/" $PGDATA/recovery.conf
sed -ir "s/#*archive_mode.*/archive_mode = always/" $PGDATA/postgresql.conf
sed -ir "s/#*archive_command.*/archive_command = 'test ! -f ${PGHOME}/archive/%f &;&; cp %p ${PGHOME}/archive/%f'/" $PGDATA/postgresql.conf
主节点日志信息 LOG: standby "s1" is now a synchronous standby with priority 1
主节点查询双机状态 postgres=# select * from pg_stat_replication where application_name='s1';
-[ RECORD 1 ]----+------------------------------
pid | 23543
usesysid | 16393
usename | foo
application_name | s1
client_addr | 127.0.0.1
client_hostname |
client_port | 48481
backend_start | 2017-04-28 14:45:03.051153+08
backend_xmin |
state | streaming
sent_location | 0/E0000D0
write_location | 0/E0000D0
flush_location | 0/E0000D0
replay_location | 0/E0000D0
sync_priority | 1
sync_state | sync
7.3 相关参数synchronous_commit
后面有时间记录一些参数的使用经验。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |