Postgresql 异步流复制

本文介绍如何在 CentOS 6.5 上配置 PostgreSQL 9.5.2 的主从流复制环境,包括设置环境变量、配置 pg_hba.conf 和 postgresql.conf 文件、创建复制用户以及在备机上进行数据同步。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

压力测试环境
version:Postgresql 9.5.2
OS: CentOS 6.5
Source Code: Postgresql9.5.2

172.31.107.30 primary 
172.31.107.32 slave
172.31.107.31 slave

开启  防火墙端口 
 iptables -I INPUT -p tcp --dport 5432 -j ACCEPT

Primary
  1. 添加 环境变量
      echo 'PATH=$PATH:/usr/local/postgresql/bin' >> /etc/profile
     echo 'PGDATA=/usr/local/postgresql/data' >> /etc/profile
     source /etc/profile#使其生效

  1. 编辑  pg_hba.conf 

host    all             all             172.31.107.31/24            trust
host    all             all             172.31.107.32/24        trust
host    all             all             172.29.10.250/16        trust
host    all             all             172.28.10.250/16        trust

host    replication     postgres        172.31.107.31/24        trust
host    replication     replUser        172.31.107.31/24        trust
host    replication     postgres        172.31.107.32/24        trust
host    replication     replUser        172.31.107.32/24        trust

  1. 编辑 postgresql.conf 添加下列项
listen_addresses = '*'
port = 5432                            
max_connections = 900

wal_level = hot_standby
fsync = on 
synchronous_commit = on
wal_sync_method = fsync

checkpoint_segments = 3
checkpoint_timeout = 5min


max_wal_senders = 2
wal_keep_segments = 3
wal_sender_timeout = 60s
max_replication_slots =2

  1. 启动Postgresql 数据库服务
shell>su - postgres
pg_ctl -D $PGDATA start

  1. 修改postgres账号密码 
增加一个 同步复制数据专用账号 (本案例使用的是postgres账号)
create user replUser REPLICATION LOGIN CONNECTION LIMIT 3 ENCRYPTED PASSWORD 'replUserfxiaoke2016';


Slave 
  1. 添加 环境变量
          echo 'PATH=$PATH:/usr/local/postgresql/bin' >> /etc/profile
         echo 'PGDATA=/usr/local/postgresql/data' >> /etc/profile
         source /etc/profile#使其生效
  2. 暂停 postgresql服务

  1. 拷贝Primary上 数据目录文件
pg_basebackup -D $PGDATA -F p -h 172.31.107.30 -p 5432 -U postgres -W


注:9.1版本以前在做该 过程前 登录 primary 
做  selec pg_start_backup('Replition work');
拷贝完毕后
select pg_stop_backup();
9.1版本以后由于提供了 pg_basebackup 工具 可以跳过该环节。

  1. 编辑 postgresql.conf 配置文件

  1. 生成并编辑 reconvery.conf配置文件


  1. 创建 /tmp/postgres/trigger 文件
  2. 确保 /usr/local/postgresql/目录和 /tmp/postgres/ 目录  postgres 具有访问读写权限
  3. 启动 postgresql 数据库服务
pg_ctl -D $PGDATA start

  1. ps -ef |grep postgre     验证是否启动成功


登录 Primary
验证是否为 异步流复制 方式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值