replication_slot and PostgreSQL Replication
主库IP:192.168.230.128
备库IP:192.168.230.129
PostgreSQL版本:
主备机PostgreSQL源码包均位于/opt/soft_bak
OS:CentOS5
主备库PostgreSQL均安装在/usr/local/pg952目录下
数据目录均在/usr/local/pg952/data
主备机为为postgres用户配置如下环境变量:
export PGPORT=5432(postgresql.conf中的端口)
export PGDATA=/usr/local/pg952/data
export LANG=en_US.utf8
export PGHOME=/usr/local/pg952
export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH
export DATE='date + "%Y%m%d%H%M"'
export PATH=$PGHOME/bin:$PATH:.
export MANPATH=$PGHOME/share/man:$MANPATH
export PGHOST=$PGDATA
export PGDATABASE=postgres
export PGUSER=postgres
alias rm='rm -i'
alias ll='ls -lh'
安装PostgreSQL(主、备机器均需要安装PostgreSQL)
创建postgres用户
useradd postgres -p 123456
创建PostgreSQL安装目录
[root@localhost ~]# cd /usr/local/
[root@localhost local]# mkdir pg952
chown -R postgres:postgres /usr/local/pg952/
安装PostgreSQL运行所需要的依赖软件包
yum -y install readline readline-devel zlib zlib-devel openssl openssl-devel gcc make flex bison
解压PostgreSQL
chown -R postgres:postgres /opt/soft_bak/
su - postgres
[postgres@localhost soft_bak]$ tar zxvf postgresql-9.5.2.tar.gz
指定PostgreSQL的安装目录并安装
[postgres@localhost soft_bak]$ cd postgresql-9.5.2
[postgres@localhost postgresql-9.5.2]$ ./configure --prefix=/usr/local/pg952/
[postgres@localhost postgresql-9.5.2]$ gmake world -j 32
[postgres@localhost postgresql-9.5.2]$ gmake install-world -j 32
主库创建PostgreSQL 数据目录
[postgres@localhost postgresql-9.5.2]$ cd /usr/local/pg952/
[postgres@localhost pg952]$ mkdir data
[postgres@localhost pg952]$ ls
bin data include lib share
备库创建PostgreSQL数据目录
[postgres@localhost postgresql-9.5.2]$ cd /usr/local/pg952/
[postgres@localhost pg952]$ mkdir data
[postgres@localhost pg952]$ chmod 0700 /usr/local/pg952/data/
[postgres@localhost pg952]$ ls
bin data include lib share
主库初始化PostgreSQL集群
[postgres@localhost pg952]$ initdb -D $PGDATA -k -E UTF8 --locale=C -U postgres -W
配置主库
postgresql.conf
listen_addresses = '*'
port = 5432
max_connections = 500
superuser_reserved_connections = 10
unix_socket_directories = '.'
password_encryption = on
shared_buffers = 2048MB
wal_level = hot_standby
fsync = on
synchronous_commit = on
archive_mode = on
archive_command = 'cd .'
max_wal_senders = 5
hot_standby = on
max_replication_slots = 1
max_wal_size = 10GB
log_destination = 'csvlog'
logging_collector = on
log_directory = 'pg_log'
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
log_file_mode = 0600
log_truncate_on_rotation = on
pg_hba.conf
host all all 127.0.0.1/32 trust
host all all 192.168.230.0/24 trust
host replication postgres 127.0.0.1/32 trust
host replication postgres 192.168.230.129/24 trust
启动主库
[postgres@localhost pg952]$ pg_ctl -D $PGDATA start
主库创建slot
[postgres@localhost pg952]$ psql -h 192.168.230.128 -p 5432 -U postgres
Password for user postgres:
psql (9.5.2)
Type "help" for help.
postgres=# select pg_is_in_recovery();
-[ RECORD 1 ]-----+--
pg_is_in_recovery | f
postgres=# select * from pg_create_physical_replication_slot('slot1');
slot_name | xlog_position
-----------+---------------
slot1 |
(1 row)
postgres=# \x
Expanded display is on.
postgres=# select * from pg_replication_slots;
-[ RECORD 1 ]+---------
slot_name | slot1
plugin |
slot_type | physical
datoid |
database |
active | f
active_pid |
xmin |
catalog_xmin |
restart_lsn |
备库从主库做basebackup(即主库的基础备份)
[postgres@localhost bin]$ pg_basebackup -h 192.168.230.128 -P -Fp -Xs -v -p 5432 -U postgres -D /usr/local/pg952/data/
WARNING: skipping special file "./.s.PGSQL.5432"
transaction log start point: 0/7000024 on timeline 1
pg_basebackup: starting background WAL receiver
WARNING: skipping special file "./.s.PGSQL.5432"g952/data//backup_label)
21715/21715 kB (100%), 1/1 tablespace
transaction log end point: 0/7000118
pg_basebackup: waiting for background process to finish streaming ...
pg_basebackup: base backup completed
[postgres@localhost bin]$ cp /usr/local/pg952/share/postgresql/recovery.conf.sample /usr/local/pg952/data/recovery.conf
[postgres@localhost bin]$ cd /usr/local/pg952/data/
recovery.conf
recovery_target_timeline = 'latest'
standby_mode = on
primary_conninfo = 'host=192.168.230.128 port=5432 user=postgres password=postgres'
primary_slot_name = 'slot1'
启动备库
[postgres@localhost data]$ pg_ctl -D $PGDATA start
在主库上验证slot工作状态
[postgres@localhost pg952]$ psql -h 192.168.230.128 -p 5432 -U postgres
psql (9.5.2)
Type "help" for help.
postgres=# \x
Expanded display is on.
postgres=# select * from pg_replication_slots;
-[ RECORD 1 ]+----------
slot_name | slot1
plugin |
slot_type | physical
datoid |
database |
active | t
active_pid | 30131
xmin |
catalog_xmin |
restart_lsn | 0/80001F8
在主库上查看复制状态
postgres=# select * from pg_stat_replication ;
-[ RECORD 1 ]----+------------------------------
pid | 30131
usesysid | 10
usename | postgres
application_name | walreceiver
client_addr | 192.168.230.129
client_hostname |
client_port | 44356
backend_start | 2016-04-25 21:40:27.740259-07
backend_xmin |
state | streaming
sent_location | 0/80181F0
write_location | 0/80181F0
flush_location | 0/80181F0
replay_location | 0/80181F0
sync_priority | 0
sync_state | async
验证主备复制
主库创建表并插入数据
[postgres@localhost pg952]$ psql -h 192.168.230.128 -p 5432 -U postgres
psql (9.5.2)
Type "help" for help.
postgres=# create table test(id int);
CREATE TABLE
postgres=# insert into test values (1),(2),(3),(4),(5);
INSERT 0 5
postgres=# select * from test ;
-[ RECORD 1 ]
id | 1
-[ RECORD 2 ]
id | 2
-[ RECORD 3 ]
id | 3
-[ RECORD 4 ]
id | 4
-[ RECORD 5 ]
id | 5
在从库上验证数据是否从主库复制到备库
[postgres@localhost data]$ psql -h 192.168.230.129 -p 5432 -U postgres
psql (9.5.2)
Type "help" for help.
postgres=# \d
List of relations
Schema | Name | Type | Owner
--------+------+-------+----------
public | test | table | postgres
(1 row)
postgres=# select * from test ;
id
----
1
2
3
4
5
(5 rows)
活跃状态的 slot 不可以删除,需要取消从库的 primary_slotname = 'slot1' 设置, 之后重启从库
active 表示是否使用, 如果不想使用 replication_slot ,需要删掉 slots.
删除slot
select pg_drop_replication_slot('slot1');
replication_slot and PostgreSQL Replication的更多相关文章
- PostgreSQL Replication之第一章 理解复制概念(1)
PostgreSQL Replication系列翻译自PostgreSQL Replication一书 在本章中,将会介绍不同的复制概念,您会了解哪些类型的复制对哪一种实用场景是最合适的. 在本章的最 ...
- [转]PostgreSQL Replication之扩展与BDR
原文:https://www.cnblogs.com/xmzzp/p/6284300.html postgres 实现master, slave ,且master是多主. -------------- ...
- PostgreSQL Replication之第十五章 与Walbouncer 一起工作
与Walbouncer 一起工作 在本书的最后一章,将引导您通向2014年发布的一个工具,称为walbouncer.本书中的大多数技巧说明了如何复制整个数据库实例,如何分片,等等.在最后一章,是关于w ...
- PostgreSQL Replication之第十四章 扩展与BDR
在这一章中,将向您介绍一个全新的技术,成为BDR.双向复制(BDR),在PostgreSQL的世界里,它绝对是一颗冉冉升起的新星.在不久的将来,许多新的东西将会被看到,并且人们可以期待一个蓬勃发展的项 ...
- PostgreSQL Replication之第十二章 与Postgres-XC一起工作(7)
12.7 处理故障转移和删除节点 在本节中,我们将看看故障切换如何处理.我们还将看看如何使用安全可靠的方法添加节点到Postgres-XC设置以及如何从Postgres-XC设置删除节点. 12.7. ...
- PostgreSQL Replication之第十二章 与Postgres-XC一起工作(4)
12.4 性能优化 Postgres-XC不是一个奇特的PostgreSQL版本,而是一个真正的分布式系统.这意味这,您不能只存储数据,希望事情超出服务器之外的快速,高效.如果您想优化速度,思考数据是 ...
- PostgreSQL Replication之扩展与BDR
在这一章中,将向您介绍一个全新的技术,成为BDR.双向复制(BDR),在PostgreSQL的世界里,它绝对是一颗冉冉升起的新星.在不久的将来,许多新的东西将会被看到,并且人们可以期待一个蓬勃发展的项 ...
- PostgreSQL Replication之第四章 设置异步复制(4)
4.4 基于流和基于文件的恢复 生活并不总只是黑色或白色:有时也会有一些灰色色调.对于某些情况下,流复制可能恰到好处.在另一些情况下,基于文件复制和PITR是您所需要的.但是也有许多情况下,您既需要流 ...
- PostgreSQL Replication之第四章 设置异步复制(2)
4.2 配置级联复制 正如您在本章已经看到的,设置流复制真的很容易.只需要设置几个参数,做一个基础备份,并享受您的复制设置. 在许多情况下,这种情况更有一点点微妙.在这个例子中我们假设:我们要使用一个 ...
随机推荐
- subclipse安装后从svn资源库视图check out的资源无法创建server
不要从根目录下check out,只要把根目录下所需要的项目check out即可.
- delimiter
http://www.mysqltutorial.org/getting-started-with-mysql-stored-procedures.aspx The first command is ...
- spm完成dmp在windows系统上导入详细过程
--查询dmp字符集 cat spmprd_20151030.dmp ','xxxx')) from dual; spm完成dmp在windows系统上导入详细过程 create tablespace ...
- scala手动编译运行
1 Person.scala class Person { var name = "" } object Person { // a one-arg constructor de ...
- Java程序员从笨鸟到菜鸟之(二十一)java过滤器和监听器详解 【转】
过滤器 1.Filter工作原理(执行流程) 当客户端发出Web资源的请求时,Web服务器根据应用程序配置文件设置的过滤规则进行检查,若客户请求满足过滤规则,则对客户请求/响应进行拦截,对请求头和请 ...
- Qt 之 去除窗口部件被选中后的焦点虚线框(设置Qt::NoFocus即可)
http://blog.csdn.net/goforwardtostep/article/details/53420529
- Unix网络编程(迭代服务器)
#include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/soc ...
- [LeetCode]题解(python):047-Permutations II
题目来源 https://leetcode.com/problems/permutations-ii/ Given a collection of numbers that might contain ...
- 【C++】运算符重载
运算符重载,主要是简化类类型运算,能够让我们对类对象直接用运算符进行运算.基本语法: 类型 operator 运算符(参数列表){ ... } Complex operator+(Complex va ...
- PHP的日期和时间
<?php //2.获取日期和时间 //时间是一维的,所以任何一个时间的时间戳都是不一样的 //array getdate([int timestamp]); //string date(str ...