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 配置级联复制 正如您在本章已经看到的,设置流复制真的很容易.只需要设置几个参数,做一个基础备份,并享受您的复制设置. 在许多情况下,这种情况更有一点点微妙.在这个例子中我们假设:我们要使用一个 ...
 
随机推荐
- memcached学习笔记3--telnet操作memcached
			
方式: 一.telnet访问memcached缓存系统(主要用于教学,不讨论) telnet 127.0.0.1 11211 => telnet IP地址 端口号 //往Memcache ...
 - nginx日志配置[转]
			
* * * sh /home/zyf/sh/cut_nginx_log.sh 这样就每天的0点1分把nginx日志重命名为日期格式,并重新生成今天的新日志文件。 日志对于统计排错来说非常有利的。本文 ...
 - mysql备份恢复
			
备份命令: mysqldump -u root -p --opt 数据库名 > /data/数据库文件名.sql 恢复命令: mysql -u root -p 数据库名</data/恢复的 ...
 - PHP7革新与性能优化
			
有幸参与2015年的PHP技术峰会(PHPCON),听了鸟哥(惠新宸)的关于PHP7的新特性和性能优化的分享,一切都令人感到激动.鸟哥是国内最权威的PHP专家,他的分享有很多非常有价值的东西,我通过整 ...
 - CLI下另一种多进程实现方式----PCNTL
			
有些时候,你需要对一些脚本进行优化,以期跑的更快,在更短的时间内完成任务.PCNTL是一个不错的选择,它可以fork多个进程,来协同完成一个任务,理论上完成的时间将会和进程数成反比. 不过,PCNTL ...
 - Grid组件 列头居中
			
在grdMain组件中的 class中设置“ x-grid-title-center ”
 - 图片放大方法、、菜单栏的位置随滚轮移动固定方法、、<a></a>去外层虚线方法:a:focus {  outline:none;   -moz-outline:none;};
			
图片放大方法一: <style type="text/css">.xt{ width:230px; height:230px;}.tp{ width:230px; he ...
 - yii2.0框架中session与cookie的用法
			
我们在开发项目中南面使用到session给和cookie,那么在yii中有他自己的规则 如下案例: session使用 function actionS1(){ echo $t ...
 - php--列表展示(小实训一月考)
			
效果图:
 - LightOj1137 - Expanding Rods(二分+数学)
			
题目链接:http://lightoj.com/volume_showproblem.php?problem=1137 题意:有一根绳子的长度为l,在有温度的情况下会变形为一个圆弧,长度为 l1 = ...