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

38次阅读
没有评论

共计 4878 个字符,预计需要花费 13 分钟才能阅读完成。

本篇文章为大家展示了 PostgreSQL 数据库单机怎样扩展为流复制,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

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

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

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

postgres=# CREATE ROLE repl WITH REPLICATION PASSWORD  lsquo;repl lsquo; LOGIN;

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

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

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

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

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

$ pg_ctl stop -m fast $ pg_ctl start

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

在 primary 服务器通过 pg_(start|stop)_backup 函数进行备份

postgres=# SELECT pg_start_backup(lsquo;label lsquo;, true); pg_start_backup ----------------- 7/E6000060 (1 row) $ rsync -az --progress ${PGDATA} postgres@10.189.100.195:/usr/local/pgsql/ --exclude postmaster.pid postgres=# SELECT pg_stop_backup(); NOTICE: pg_stop_backup complete, all required WAL segments have been archived pg_stop_backup ---------------- 7/E60005C8 (1 row)

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

$ 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 Password: transaction log start point: 7/EA000028 on timeline 1 pg_basebackup: starting background WAL receiver 65933562/65933562 kB (100%), 1/1 tablespace transaction log end point: 7/EA000830 pg_basebackup: waiting for background process to finish streaming ... pg_basebackup: base backup completed

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

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

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

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

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

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

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

通过函数和系统表查看

edbstore=# select * from pg_stat_replication; #在 primary 主库查看  -[ RECORD 1 ]----+------------------------------ pid | 15013 usesysid | 19206 usename | repl application_name | walreceiver client_addr | 10.189.100.195 client_hostname | client_port | 56072 backend_start | 2017-06-13 08:10:35.400508-07 backend_xmin | state | streaming sent_location | 7/EC01A588 write_location | 7/EC01A588 flush_location | 7/EC01A588 replay_location | 7/EC01A588 sync_priority | 0 sync_state | async edbstore=# SELECT pg_current_xlog_location(); #在 primary 主库查看  pg_current_xlog_location -------------------------- 7/EC01A588 (1 row) postgres=# select pg_last_xlog_receive_location(),pg_last_xlog_replay_location(),pg_last_xact_replay_timestamp(); # 在 standby 备库查看  pg_last_xlog_receive_location | pg_last_xlog_replay_location | pg_last_xact_replay_timestamp -------------------------------+------------------------------+------------------------------- 7/EC01A588 | 7/EC01A588 | 2017-06-13 08:25:20.281568-07 (1 row)

通过进程查看

$ ps -ef | grep sender | grep -v grep #在 primary 库查看  postgres 15013 24883 0 08:10 ? 00:00:00 postgres: wal sender process repl 10.189.100.195(56072) streaming 7/EC01A668 $ ps -ef | grep receiver | grep -v grep # 在 standby 库查看  postgres 12857 12843 0 08:10 ? 00:00:00 postgres: wal receiver process streaming 7/EC01A668

上述内容就是 PostgreSQL 数据库单机怎样扩展为流复制,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注丸趣 TV 行业资讯频道。

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-19发表,共计4878字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)