PostgreSQL Hot Standby的搭建

一、 简介:

PG在9.*版本后热备提供了新的一个功能,那就是Stream Replication的读写分离,是PG高可用性的一个典型应用。这个功能在oracle中叫active dataguard,在PostgreSQL中称为hot standby。

二、系统环境

系统平台:CentOS 6.2

PostgreSQL版本:9.5.0

master :   183.60.192.238

slave  :   183.60.192.229

、搭建步骤

数据库搭建参考地址http://www.cnblogs.com/lottu/p/5149191.html

注:slave库可不需要init数据库;

  • master库操作
  1. 创建流复制用户repuser

CREATE USER repuser replication LOGIN CONNECTION LIMIT 3 ENCRYPTED PASSWORD ‘li0924‘;

# replication    做流复制的时候用到的一个用户属性,一般单独设定。

# LOGIN  用户登录的属性;CREATE USER默认自带;所以LOGIN关键字可以略去。

2. 在master数据库的/data/pgdata/postgresql.conf文件中设置如下配置项:

wal_level = hot_standby

max_wal_senders = 1

wal_keep_segments = 32

# max_wal_senders   是slave库的节点数,有多少个slave库就设多少,

# wal_keep_segments  默认值是16,是PG_XLOG下的日志文件数相关参数

3.在主数据库中的/var/lib/pgsql/data/pg_hba.conf中添加如下配置:

host    replication     repuser         183.60.192.229/16          md5

# 第二项必须填 replication;

4.重新启动主数据库,让配置生效:

pg_stop;pg_start

5.采用热备份的方式;把master数据库的PGDATA目录下面的文件传到slave库中.同时把创建的数据文件夹也传过去;

这个步骤就是一个数据库克隆的工作。

  • slave库操作

1.在slave数据库的/data/pgdata/postgresql.conf文件中设置如下配置项:

hot_standby = on

2.在PGDATA目录下;也就这里的/data/pgdata;新建文件recovery.conf

standby_mode = ‘on‘

primary_conninfo = ‘host=183.60.192.238 port=5432 user=repuser password=li0924‘

trigger_file = ‘/data/pgdata/trigger_standby‘

3.删除原先从master库上过来的/data/pgdata/postmaster.pid文件,

rm /data/pgdata/postmaster.pid

4.根据master库中的环境变量文件;修改slave库的postgres用户的环境变量 然后启动备库:

pg_start

四、验证工作

  • 查看进程

master库

[[email protected]_210 ~]$ ps -ef | grep postgre

root      2021   556  0 15:18 pts/1    00:00:00 su - postgres

postgres  2022  2021  0 15:18 pts/1    00:00:00 -bash

postgres  2239     1  0 15:24 pts/1    00:00:00 /opt/pgsql95/bin/postgres

postgres  2249  2239  0 15:24 ?        00:00:00 postgres: checkpointer process

postgres  2250  2239  0 15:24 ?        00:00:00 postgres: writer process

postgres  2251  2239  0 15:24 ?        00:00:00 postgres: wal writer process

postgres  2252  2239  0 15:24 ?        00:00:00 postgres: autovacuum launcher process

postgres  2253  2239  0 15:24 ?        00:00:00 postgres: archiver process   last was 00000006000000000000001E.00000028.backup

postgres  2254  2239  0 15:24 ?        00:00:00 postgres: stats collector process

postgres  3235  2239  0 15:54 ?        00:00:00 postgres: wal sender process repuser 183.60.192.229(40399) streaming 0/1F000D80

slave库

[[email protected]_222 pgdata]$ ps -ef | grep postgres

postgres  6856     1  0 15:54 pts/0    00:00:00 /opt/pgsql/bin/postgres

postgres  6863  6856  0 15:54 ?        00:00:00 postgres: startup process   recovering 00000006000000000000001F

postgres  6864  6856  0 15:54 ?        00:00:00 postgres: checkpointer process

postgres  6865  6856  0 15:54 ?        00:00:00 postgres: writer process

postgres  6866  6856  0 15:54 ?        00:00:00 postgres: wal receiver process   streaming 0/1F000CA0

postgres  6867  6856  0 15:54 ?        00:00:00 postgres: stats collector process

root      6922 30527  0 16:08 pts/0    00:00:00 su - postgres

postgres  6923  6922  0 16:08 pts/0    00:00:00 -bash

postgres  6974  6923  0 16:49 pts/0    00:00:00 ps -ef

postgres  6975  6923  0 16:49 pts/0    00:00:00 grep postgres

  • 数据验证

在master库操作

[[email protected]_210 ~]$ psql mydb repuser

psql (9.5.0)

Type "help" for help.

mydb=> \d

List of relations

Schema | Name  |     Type      |  Owner

--------+-------+---------------+----------

public | dept  | table         | lottu

public | emp   | foreign table | postgres

public | test  | table         | lottu

public | trade | table         | lottu

(4 rows)

mydb=> create table t_lottu (id int primary key,name varchar(20));

CREATE TABLE

mydb=> \d

List of relations

Schema |  Name   |     Type      |  Owner

--------+---------+---------------+----------

public | dept    | table         | lottu

public | emp     | foreign table | postgres

public | t_lottu | table         | repuser

public | test    | table         | lottu

public | trade   | table         | lottu

(5 rows)

mydb=> insert into t_lottu values (1001,‘lottu‘);

INSERT 0 1

mydb=> insert into t_lottu values (1002,‘vincent‘);

INSERT 0 1

mydb=> insert into t_lottu values (1003,‘rax‘);

INSERT 0 1

mydb=> select * from t_lottu;

id  |  name

------+---------

1001 | lottu

1002 | vincent

1003 | rax

(3 rows)

登录slave库查看数据

[[email protected]_222 pgdata]$ psql mydb repuser

psql (9.5.0)

Type "help" for help.

mydb=> select * from t_lottu;

id  |  name

------+---------

1001 | lottu

1002 | vincent

1003 | rax

(3 rows)

mydb=> delete from t_lottu where id = 1003;
ERROR:  cannot execute DELETE in a read-only transaction

参考博客 --http://my.oschina.net/Kenyon/blog/54967

时间: 2024-08-28 12:36:38

PostgreSQL Hot Standby的搭建的相关文章

PostgreSQL Hot Standby环境监测脚本

Postgresql 在9.0引进了Stream Replication;为数据库集群提供一种高可用性:本文提供一个环境监测脚本 环境搭建: 参考--http://www.cnblogs.com/lottu/p/5584923.html 环境切换: 参考--http://www.cnblogs.com/lottu/p/5725309.html 提供脚本如下: ##=========================================================== ## post

postgresql异步流复制搭建

节点 IP 角色 citus-master 10.10.100.1 master citus-standby 10.10.100.2 standby master上创建流复制所需要的用户. CREATE ROLE replication WITH REPLICATION PASSWORD 'replication' LOGIN; 修改master的pg_hba.conf文件,设置replication用户远程访问权限 ## vim /data/pgsql/data/pg_hba.conf,追加下

postgresql同步流复制搭建

节点 IP 角色 master 10.10.100.1 master standby1 10.10.100.2 standby1 standby2 10.10.100.3 standby2 master上创建流复制所需要的用户. CREATE ROLE replication WITH REPLICATION PASSWORD 'replication' LOGIN; 修改master的pg_hba.conf文件,设置replication用户远程访问权限 ## vim /data/pgsql/

Logical standby database 搭建(配置)

说明 Logical standby 数据库是通过Physical standby数据库转换的.本Logical standby是通过之前创建的Physical standby转换的. Physical standby搭建可参考下列链接: http://www.cnblogs.com/zhenxing/p/5260693.html 逻辑DG支持的数据类型 在从物理DG转换为逻辑DG前,先需要确定逻辑DG支持的数据类型和支持哪些DDL操作,可参考下列链接: http://docs.oracle.c

Postgresql监控pgwatch的搭建

一,需要环境: You will need a handful of components to make this work: - Apache (webserver) #apache搭建web页面 - PHP 5 (scripting language) #php5 - pgsql extension for PHP (see http://www.php.net/manual/en/book.pgsql.php) #php的pgsql的扩展支持 - PostgreSQL 9 (to sto

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数据库原理

ORACLE 11G搭建dataguard详细步骤(物理standby所有操作总结)

序言:      DATAGUARD是通过建立一个PRIMARY和STANDBY组来确立其参照关系:STANDBY一旦创建,DATAGUARD就会通过将主数据库(PRIMARY)的REDO传递给STANDBY数据库,然后在STANDBY中应用REDO实现数据库的同步. 有两种类型的STANDBY:物理STANDBY和逻辑STANDBY    物理STANDBY提供与主数据库完全一样的拷贝(块到块),数据库SCHEMA,包括索引都是一样的.它是直接应用REDO实现同步的.    逻辑STANDBY

azure linux虚拟机openlogic_centos7.0搭建postgresql数据库

近日,需要用到postgresql数据库. 我搭建的环境为: azure平台,操作系统为azure平台自带的openlogic centos7.0. 搭建过程: 1,使用系统自带postgresql包.如果仅仅搭建postgresql数据库,仅仅安装postgresql server即可. sudo yum install postgresql-server postgresql 2,安装完毕后,启动数据库,使用root权限. --首先需要初始化数据库 postgresql-setup init

Nginx+PostgreSQL+Django+UWSGI搭建

最近因为项目上的需要开始大量使用nginx,因此也想趁机将以前常用的django+apache的架构换成django+nginx.常见的 django webapp 部署方式采用FCGI 或 WSGI的方式部署,在这里主要对CentOS 6.5下Python 2.7.5环境下采用 Nginx + PostgreSQL + Django + uwsgi 的搭建与配置步骤做一个简要说明,主要留作备忘,也希望对大家有所帮助. 一.Nginx-1.6.2安装 1. 在安装nginx前,需要确保系统安装了