primary:10.189.102.118

standby:10.189.100.195

1. 配置ssh互信机制

  • 在primary主库执行
$ ssh-keygen -t rsa
$ cp ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys
$ ssh-copy-id postgres@10.189.100.195
  • 在standby备库执行
$ ssh-keygen -t rsa
$ cp ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys
$ ssh-copy-id postgres@10.189.102.118

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

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

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

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

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

$ grep "^host" pg_hba.conf
host all all 127.0.0.1/ trust
host replication repl 0.0.0.0/ md5
host all all ::/ trust

5. 在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 = 'test ! -f /usr/local/pgsql/arch/%f && cp %p /usr/local/pgsql/arch/%f && rsync -a %p postgres@10.189.100.195:/usr/local/pgsql/arch/%f'
max_wal_senders = # max number of walsender processes
wal_keep_segments = # in logfile segments, 16MB each; disables
hot_standby = on # "on" allows queries during recovery
#hot_standby_feedback = off # send info from standby to prevent

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

$ pg_ctl stop -m fast
$ pg_ctl start

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

  •    方法一,在primary服务器通过pg_(start|stop)_backup函数进行备份
postgres=# SELECT pg_start_backup('label', true);
pg_start_backup
-----------------
/E6000060
( 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
----------------
/E60005C8
( row)
  •  方法二,在standby服务器通过pg_basebackup命令进行备份,要求standby的PGDATA目录为空
$ pg_basebackup --host=10.189.102.118 --username=repl --port= --label=backup --verbose --progress --pgdata=/usr/local/pgsql/data --checkpoint=fast --format=p --xlog-method=stream
Password:
transaction log start point: /EA000028 on timeline
pg_basebackup: starting background WAL receiver
/ kB (%), / tablespace
transaction log end point: /EA000830
pg_basebackup: waiting for background process to finish streaming ...
pg_basebackup: base backup completed

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

$ mkdir /usr/local/pgsql/arch
$ grep "hot_standby" postgresql.conf
hot_standby = on # "on" allows queries during recovery
#hot_standby_feedback = off # send info from standby to prevent

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

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

10. 启动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

11. 检查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 : ? :: postgres: wal sender process repl 10.189.100.195() streaming /EC01A668 $ ps -ef | grep receiver | grep -v grep #在standby库查看
postgres : ? :: postgres: wal receiver process streaming /EC01A668
  •  查看备库落后主库多少字节的WAl日志
postgres=# select pg_xlog_location_diff(pg_current_xlog_location(),replay_location) from pg_stat_replication ;     #在primary库查看
pg_xlog_location_diff
-----------------------
0
(1 row)
  •  把主库WAL日志位置转换成WAL文件名称和偏移量
postgres=# select pg_current_xlog_location();
pg_current_xlog_location
--------------------------
7/EC021790
(1 row) postgres=# select * from pg_xlogfile_name_offset('7/EC021790');
file_name | file_offset
--------------------------+-------------
000000010000000700000076 | 137104
(1 row)
  • 查看主库备库的状态
postgres=# select pg_is_in_recovery();            #在primary主库查看
pg_is_in_recovery
-------------------
f
(1 row) postgres=# select pg_is_in_recovery(); #在standby库查看
pg_is_in_recovery
-------------------
t
(1 row)

PostgreSQL数据库单机扩展为流复制的更多相关文章

  1. 使用 Bitnami PostgreSQL Docker 镜像快速设置流复制集群

    bitnami-docker-postgresql 仓库 源码:bitnami-docker-postgresql https://github.com/bitnami/bitnami-docker- ...

  2. Postgresql 9.6 搭建 异步流复制 和 同步流复制 详细教程

    Basic Replication If you’re feeling overwhelmed, try setting up a slave to see how easy it is! We’ll ...

  3. postgresql数据库异步流复制hot standby环境搭建

    生命不息,test不止. 最近组里面修改了几个postgresql的bug,要进行回归测试,除了前面提到的WAL的RT测试和Mirroring Controller的RT测试,还要测试下postgre ...

  4. [原创] PostgreSQL Plus Advanced Server在Windows中配置双机热备流复制

    一.系统环境 操作系统:Windows Server 2003/2008 两个节点分别为master与slave. 主节点master:172.27.19.28 备机点slave:172.27.19. ...

  5. 再不了解PostgreSQL,你就晚了之PostgreSQL主从流复制部署

    前言 在MySQL被收购之后,虽然有其替代品为: MariaDB,但是总感觉心里有点膈应.大家发现了另一款开源的数据库: PostgreSQL. 虽然centos自带版本9.2也可以用,但是最近的几次 ...

  6. PostgreSQL流复制

    原理机制 参考--https://yq.aliyun.com/articles/51009 主备总体结构 PG主备流复制的核心部分由walsender,walreceiver和startup三个进程组 ...

  7. [笔记] postgresql 流复制(streaming replication)

    基本环境说明: os:FreeBSD 9.3 postgresql version: master:192.168.56.101 standby:192.168.56.102 安装过程略,基于pkg包 ...

  8. PostgreSQL异步主从流复制搭建

    1 总体规划   Master库 Slave库 操作系统 CentOS Linux release 7.5.1804 CentOS Linux release 7.5.1804 处理器 1 1 内存 ...

  9. PostgreSQL 流复制+高可用

    QA PgPool-II 同步 Postgresql X1 服务器准备 192.168.59.121 PostgreSQL10 192.168.59.120 PGPool-II 3.7 X2 安装Po ...

随机推荐

  1. Spring Cloud各组件超时总结

    Ribbon的超时 全局设置: ribbon: ReadTimeout: 60000 ConnectTimeout: 60000 1 2 3 局部设置: service-id: ribbon: Rea ...

  2. [转载]Linux中的网络接口及LO回环接口

    转自:https://blog.csdn.net/weixin_39863747/article/details/80564358 Linux中的网络接口及LO回环接口 2018年06月04日 10: ...

  3. JAVA怎样理解面向对象

    一.对象   现实世界中,随处可见的一种事物就是对象,对象是事物存在的实体,如人类.书桌.计算机.高楼大厦等.人类解决问题的方式总是将复杂的事物简单化,于是就会思考这些对象都是由哪些部分组成的.通常都 ...

  4. webservice跨域问题

    在webconfig里面加上   <httpProtocol>      <customHeaders>        <add name="Access-Co ...

  5. 小程序scss页面布局

    html <view class="main"> <form bindsubmit="feedback"> <textarea c ...

  6. ZZNUOJ 2022 摩斯密码

    map打表存一下对应的密码   不会map感觉不好弄这题 #include<stdio.h> #include<string.h> #include<math.h> ...

  7. Django 国际化和本地化

    所谓的国际化,是指使用不同语言的用户在访问同一个网站页面时能够看到符合其自身语言的文本页面. 国际化的基本原理是: 浏览器通过LANGUAGE_CODE在HTTP请求头中告诉网站后台服务器用户所需要的 ...

  8. hive表的存储路径查找以及表的大小

    1.在hive中知道一个表的存储路径可以通过hive命令   desc formatted table_name 显示表的详细信息; 2.然后找到该表的存储路径 "Location:    ...

  9. java 虹软ArcFace 2.0,java SDK使用、人脸识别-抽取人脸特征并做比对

    java人脸识别 虹软ArcFace 2.0,java SDK使用.人脸识别-抽取人脸特征并做比对 虹软产品地址:http://ai.arcsoft.com.cn/product/arcface.ht ...

  10. 手动增删windows 服务和dll函数

    ①注册windows服务 sc create "服务名AAA" binPath= "安装目录\AAA.exe" displayname= "服务显示名 ...