PostgreSQL数据库单机扩展为流复制

数据库 PostgreSQL
以下是PostgreSQL数据库单机扩展为流复制,希望对小伙伴们有所帮助。

[[194921]]

1. 在standby服务器安装postgres数据库,不需要初始化.

安装过程详见:http://www.cnblogs.com/ilifeilong/p/6979288.html

2. 在primary服务器创建具有REPLICATION权限的复制用户

  1. postgres=# CREATE ROLE repl WITH REPLICATION PASSWORD ‘repl‘ LOGIN; 

3. 允许复制用户远程连接到primary服务器

  1. $ grep "^host" pg_hba.conf 
  2. host    all             all             127.0.0.1/32            trust 
  3. host    replication             repl             0.0.0.0/0               md5  
  4. host    all             all             ::1/128                 trust 

4. 在primary服务器设置流复制相关的参数

  1. $ mkdir /usr/local/pgsql/arch  
  2. $ egrep "archive_mode|max_wal_senders|wal_keep_segments|archive_command|wal_level|hot_standby" postgresql.conf 
  3. al_level = hot_standby            # minimal, archive, hot_standby, or logical 
  4. archive_mode = on        # enables archiving; offonor always 
  5. archive_command = ‘test ! -f /usr/local/pgsql/arch/%f && cp %p /usr/local/pgsql/arch/%f‘         
  6. max_wal_senders = 5        # max number of walsender processes 
  7. wal_keep_segments = 30        # in logfile segments, 16MB each; 0 disables 
  8. hot_standby = on            # "on" allows queries during recovery 
  9. #hot_standby_feedback = off        # send info from standby to prevent 

5. 重新启动primary服务器进程

  1. $ pg_ctl stop -m fast 
  2. $ pg_ctl start 

6. 对primary服务器做一个全备并传输到standby服务器

  • 在primary服务器通过pg_(start|stop)_backup函数进行备份
  1. postgres=# SELECT pg_start_backup(‘label‘, true); 
  2.  pg_start_backup  
  3. ----------------- 
  4.  7/E6000060 
  5. (1 row) 
  6. $ rsync -az --progress ${PGDATA} postgres@10.189.100.195:/usr/local/pgsql/ --exclude postmaster.pid 
  7. postgres=# SELECT pg_stop_backup(); 
  8. NOTICE:  pg_stop_backup complete, all required WAL segments have been archived 
  9.  pg_stop_backup  
  10. ---------------- 
  11.  7/E60005C8 
  12. (1 row) 

在standby服务器通过pg_basebackup命令进行备份,要求standby的PGDATA目录为空

  1. $ pg_basebackup --host=10.189.102.118 --username=repl --port=5432 --label=backup --verbose --progress --pgdata=/usr/local/pgsql/data --checkpoint=fast --format=p --xlog-method=stream 
  2. Password:  
  3. transaction log start point: 7/EA000028 on timeline 1 
  4. pg_basebackup: starting background WAL receiver 
  5. 65933562/65933562 kB (100%), 1/1 tablespace                                          
  6. transaction log end point: 7/EA000830 
  7. pg_basebackup: waiting for background process to finish streaming ... 
  8. pg_basebackup: base backup completed 

7. 设置standby数据库复制相关参数,使得standby失效转移后可以作为主库工作

  1. $ mkdir /usr/local/pgsql/arch 
  2. $ egrep "archive_mode|max_wal_senders|wal_keep_segments|archive_command|wal_level|hot_standby" postgresql.conf 
  3. wal_level = hot_standby                 # minimal, archive, hot_standby, or logical 
  4. archive_mode = on               # enables archiving; offonor always 
  5. archive_command = ‘test ! -f /usr/local/pgsql/arch/%f && cp %p /usr/local/pgsql/arch/%f‘ 
  6. max_wal_senders = 5             # max number of walsender processes 
  7. wal_keep_segments = 30          # in logfile segments, 16MB each; 0 disables 
  8. hot_standby = on                        # "on" allows queries during recovery 
  9. #hot_standby_feedback = off             # send info from standby to prevent 

8. 在standby文件创建恢复文件

  1. $ cat recovery.conf  
  2. restore_command = ‘cp /usr/local/pgsql/arch/%f "%p"‘ 
  3. standby_mode = ‘on‘ 
  4. primary_conninfo = ‘user=repl password=repl host=10.189.102.118 port=5432 sslmode=disable sslcompression=1‘ 
  5. archive_cleanup_command = ‘pg_archivecleanup -d /usr/local/pgsql/arch %r >> /usr/local/pgsql/arch/archive_cleanup.log‘ 
  6. trigger_file = ‘/usr/local/pgsql/data/trigger_active_standby‘ 

9. 启动standby数据库进程,自动启动流复制

  1. $ pg_ctl start -w 
  2. waiting for server to start....LOG:  could not create IPv6 socket: Address family not supported by protocol 
  3. LOG:  redirecting log output to logging collector process 
  4. HINT:  Future log output will appear in directory "pg_log"
  5.  done 
  6. server started 

10. 检查primary和standby数据库的延迟

  • 通过函数和系统表查看
  1. edbstore=# select * from pg_stat_replication;           #在primary主库查看 
  2. -[ RECORD 1 ]----+------------------------------ 
  3. pid              | 15013 
  4. usesysid         | 19206 
  5. usename          | repl 
  6. application_name | walreceiver 
  7. client_addr      | 10.189.100.195 
  8. client_hostname  |  
  9. client_port      | 56072 
  10. backend_start    | 2017-06-13 08:10:35.400508-07 
  11. backend_xmin     |  
  12. state            | streaming 
  13. sent_location    | 7/EC01A588 
  14. write_location   | 7/EC01A588 
  15. flush_location   | 7/EC01A588 
  16. replay_location  | 7/EC01A588 
  17. sync_priority    | 0 
  18. sync_state       | async 
  19.  
  20. edbstore=# SELECT pg_current_xlog_location();                      #在primary主库查看 
  21.  pg_current_xlog_location  
  22. -------------------------- 
  23.  7/EC01A588 
  24. (1 row) 
  25.  
  26. postgres=# select pg_last_xlog_receive_location(),pg_last_xlog_replay_location(),pg_last_xact_replay_timestamp();     #在standby备库查看 
  27.  pg_last_xlog_receive_location | pg_last_xlog_replay_location | pg_last_xact_replay_timestamp  
  28. -------------------------------+------------------------------+------------------------------- 
  29.  7/EC01A588                    | 7/EC01A588                   | 2017-06-13 08:25:20.281568-07 
  30. (1 row) 
  • 通过进程查看
  1. $ ps -ef | grep sender | grep -v grep #在primary库查看  
  2. postgres 15013 24883 0 08:10 ? 00:00:00 postgres: wal sender process repl 10.189.100.195(56072) streaming 7/EC01A668  
  3. $ ps -ef | grep receiver | grep -v grep #在standby库查看  
  4. postgres 12857 12843 0 08:10 ? 00:00:00 postgres: wal receiver process streaming 7/EC01A668 
责任编辑:武晓燕 来源: 码迷
相关推荐

2017-06-16 21:36:14

2024-01-18 08:00:00

PostgreSQLPgvector

2019-11-20 09:08:46

PostgreSQL数据库

2023-07-24 09:00:00

数据库

2011-04-15 13:41:27

SqlServer数据复制

2022-01-10 07:59:14

PostgreSQl 主从流复制归档配置

2021-07-07 21:07:16

PostgreSQL架构容灾库

2024-03-04 10:48:15

PostgreSQL数据库

2011-03-08 08:49:55

MySQL优化单机

2022-10-12 13:33:25

PostgreSQL数据库

2010-05-26 10:15:11

MySQL数据库

2017-10-13 15:06:18

数据库PostgreSQL特性

2010-08-27 09:59:51

SQL Server

2010-03-02 15:16:23

Ubuntu Post

2011-03-25 13:08:19

PostgreSQL数

2019-02-15 14:59:09

华为云

2011-07-26 14:34:28

openSUSEpostgresql

2023-11-29 09:53:29

数据库迁移SQL Server

2020-09-03 11:35:22

SQLiteMySQLPostgreSQL

2011-05-17 13:43:23

Oracle数据库
点赞
收藏

51CTO技术栈公众号