postgres 9.2.3流复制环境搭建

环境:

primary 10.0.0.1    Red Hat Enterprise Linux Server release 5.4

standby 10.0.0.2    Red Hat Enterprise Linux Server release 5.4

1,主备节点分别安装PG,primary端启动PG服务,不详细描述了。

2,primary端修改流复制相关参数,如下:

   wal_level = hot_standby

   max_wal_senders = 10

   checkpoint_segments=128   #条件允许就适当增大

   archive_mode = on   #不是必要参数,建议打开archive,后续更改会重启PG服务。

   archive_command=''  #打开archive功能但不设置archive命令,DB的可恢复性通过standby来保证了,不然又要耗费大量的存储,不划算。

   wal_keep_segments = 512    #条件允许就适当增大,保证standby一旦出现问题,也能及时恢复并跟上primary,不然会增加STANDBY失效的概率。

   hot_standby = on

3,standby端修改流复制相关参数,如下:

   wal_level=hot_standby

   hot_standby=on

   通常会把primary和standby的参数设置成一样,这样一旦发生failover,不用另外再修改参数

4,primary端创建复制用户

   create role replica nosuperuser nocreatedb nocreaterole noinherit login replication encrypted password 'replica';

5,修改两节点的pg_hba.conf,添加以下内容:

   host  replication  replica  10.0.0.1/32  md5

   host  replication  replica  10.0.0.2/32  md5

6,primary端开始备份

   postgres=# select pg_start_backup('bak_yyyymmdd');
   pg_start_backup
   -----------------
   0/C000020
   (1 row)

   这里列下命令,关于备份及恢复会在后续文章中详细讲述。

7,打包整个$PGDATA,拷至standby(如果创建了其他表空间,要把这些表空间的目录一起打包)

8,standby端,解开$PGDATA,目录与primary保持一致,并在$PGDATA下创建recovery.conf文件,内容如下:

   recovery_target_timeline = 'latest'  #具体作用后续解释

   standby_mode = on
   primary_conninfo = 'host=10.0.0.1 port=1921 user=replica password=replica keepalives_idle=60'

9,primary端结束备份

   postgres=# select pg_stop_backup();
   NOTICE:  pg_stop_backup cleanup done, waiting for required WAL segments to be archived

   WARNING:  pg_stop_backup still waiting for all required WAL segments to be archived (60 seconds elapsed)
   HINT:  Check that your archive_command is executing properly.  pg_stop_backup can be canceled safely, but the database backup will not be usable without all the WAL segments.
   警告信息是由于没设置archive_command造成,不必在意。

10,standby端启动PG服务

    pg_ctl start -D $PGDATA

    pg_ctl: another server might be running; trying to start server anyway server starting
    LOG:  database system was interrupted; last known up at 2013-02-28 09:20:51 CST
    LOG:  entering standby mode
    LOG:  redo starts at 0/C000020
    LOG:  record with zero length at 0/C0000B8
    LOG:  streaming replication successfully connected to primary
    LOG:  consistent recovery state reached at 0/C0000E0
    LOG:  database system is ready to accept read only connections

11,流复制环境搭建完成,可在primary端执行各中DB操作,会在很短时间内同步到standby端(10ms级),可自行验证。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值