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

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

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

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

postgres=# CREATE ROLE repl WITH REPLICATION PASSWORD ‘repl‘ 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 = ‘test ! -f /usr/local/pgsql/arch/%f && cp %p /usr/local/pgsql/arch/%f‘
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(‘label‘, true);
 pg_start_backup
-----------------
 7/E6000060
(1 row)
$ rsync -az --progress ${PGDATA} [email protected]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 = ‘test ! -f /usr/local/pgsql/arch/%f && cp %p /usr/local/pgsql/arch/%f‘
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 = ‘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‘

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
时间: 2024-08-10 23:17:56

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

PostgreSQL数据库Streaming Replication流复制主备延迟测试

PostgreSQL数据库流复制主库和备库之间的延迟时间是多少,无论对HA还是负载均衡来说都应该做个评估.比如单纯的HA架构,当主库发生故障时,我们允许多少时间内的数据丢失.不废话,直接进入本次实验测试. 测试环境: 主库:内存:32G,CPU:8核,IP:192.168.122.101 备库:内存:32G,CPU:8核,IP:192.168.122.102 数据库配置:默认 测试准备: 在两台服务器上安装好PostgreSQL数据库,安装过程不清楚的可以参考文章<PostgreSQL数据库编译

corosync+pacemaker实现pg流复制自动切换

一.环境 $ cat /etc/redhat-release CentOS Linux release 7.0.1406 (Core) node1: 192.168.111.128 node2: 192.168.111.129 vip-master: 192.168.111.228 vip-slave: 192.168.111.229 配置主机名 hostnamectl set-hostname postgres128 hostnamectl set-hostname postgres129 [

Postgresql流复制+pgpool实现高可用

pgpool 概述 pgpool-II 是一个位于 PostgreSQL 服务器和 PostgreSQL 数据库客户端之间的中间件,它提供以下功能:连接池.复制.负载均衡.限制超过限度的连接以及并行查询.文档在此. 四种模式 O 意味着“可用”, X 意味着“不可用(1) 并行查询模式需要同时打开复制和负载均衡,但是复制和负载均衡无法用于并行查询模式中的分布式表.(2) 在线恢复可以和流复制同时使用.(*3) 客户端仅仅是通过 pgpool-II 连接到 PostgreSQL服务器.这种模式仅仅

金庸武功之“”左右互搏术“”postgresql 主从异步流复制配置

一.环境准备 a.关闭selinxu b.关闭iptables c.centos6.5 d.postgresql9.4.4 master:192.168.1.211 slave:  192.168.1.212 时间同步: #同步系统时间 [[email protected] ~]#  rm  -rf  /etc/localtime [[email protected] ~]#  ln  -s   /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime

PostgreSQL流复制

原理机制 参考--https://yq.aliyun.com/articles/51009 主备总体结构 PG主备流复制的核心部分由walsender,walreceiver和startup三个进程组成. walsender进程是用来发送WAL日志记录的 walreceiver进程是用来接收WAL日志记录的 startup进程是用来apply日志的 配置环境 主机名 IP地址 角色 数据目录 postgres202 192.168.1.202 primary /home/postgres/dat

PostgreSQL 9.5.5主从实现之异步流复制(Hot Standby)

前言 简单记录一下postgresql主从的实现方式之一--基于Standby的异步流复制,这是PostgreSQL9.x版本(2010.9)之后提供的一个很nice的功能,类似的功能在Oracle中是11g之后才提供的active dataguard和SQL Server 2012版本之后才提供的日志传送,此处再次为pg鼓掌,确实是一个很棒的开源数据库.废话不多说,本篇blog就详细记录一下在pg9.5中实现Hot Standby异步流复制的完整配置过程和注意事项. Standby数据库原理

postgresql 流复制(streaming replication)

基本环境说明: os:FreeBSD 9.3 postgresql version:9.3.6 master:192.168.56.101 standby:192.168.56.102 安装过程略,基于pkg包 1.配置master端 # psql -U pgsql -d postgres -c "CREATE USER rep REPLICATION LOGIN ENCRYPTED PASSWORD 'password';" # cd /usr/local/pgsql # vim d

基于PostgreSQL的可扩展的开源大数据集群数据库:Postgres-XL

基于PostgreSQL的可扩展的开源大数据集群数据库:Postgres-XL http://www.postgres-xl.org/ Postgres的-XL是一个基于PostgreSQL数据库的横向扩展开源SQL数据库集群,具有足够的灵活性来处理不同的数据库工作负载: OLTP 写频繁的业务 需要MPP并行性商业智能 操作数据存储 Key-value 存储 GIS的地理空间 混合业务工作环境 多租户服务提供商托管环境 可扩展性 Postgres-XL(eXtensible Lattice),

PostgreSQL使用pg_basebackup搭建主备流复制环境

今天用pg_basebackup搭建了主备流复制环境,操作流程很简单,可在线操作,相当方便. 环境均为CentOS6.6 + postgresql9.4,我用已安装的库做测试,端口不一致,不影响环境搭建,但要注意某些相关配置: 主:192.168.3.201 port:5431 备:192.168.3.202 port:5432 数据库安装过程省略.可参考 http://blog.csdn.net/baiyinqiqi/article/details/45560229 主库参数配置postgre