欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

postgresql的streaming replication和slot实作

程序员文章站 2022-04-21 23:50:52
...

环境准备:

1、两台centos先装,然后以root身份执行:

yum update

yum install gcc gcc-c++ openssl openssl-devel zlib zlib-devel pcre pcre-devel readline readline-devel

yum install openssh-clients

2、在postgresql.org上下载pg源码进行编译安装,configure选项默认。(master、slave上均需完成安装)

postgresql-9.4.0.tar.bz2

tar xvf post*bz2

cd p*0

./configure

make ; make install clean

useradd postgres(作为root)

echo 'export PATH=$PATH:/usr/local/pgsql/bin' >> /etc/profile

su - postgres

source /etc/profile

mkdir data

initdb -D data

3、实现streaming replication

MASTER上:

vi data/postgresql.conf

修改或加入:

listen_addresses='*'

wal_level=hot_standby

max_wal_senders=2 #不需要很大

wal_keep_segments=10 #意味着可以保留的log有160M,10块。

archive_mode=on

archive_command= 'rsync -a %p [email protected]:~/wals/%f </dev/null' #请自行脑补rsync

插曲:到你的从库系统上执行su - postgres -c 'mkdir wals'

其他配置一般保持默认就好,生产环境下需按服务器实际情况调整。

vi data/pg_hba.conf

在尾部加上:


host replication postgres MASTERIP/32 trust
host replication postgres SLAVEIP/32 trust

开放5432端口

启动主库。

pg_ctl -D data -l ./pg.log start

执行:


处理rsync等命令所需的key

ssh-****** -b 2048

ssh-copy-id -i ~/.ssh/id_rsa.pub SLAVEIP

psql

SELECT pg_switch_xlog(); #告诉pg,你要备份了

SELECT pg_start_backup('backup',true); #backup可修改成你喜欢的女神名字?

\q


rsync -av  --exclude postgresql.conf --exclude postmaster.pid  data/*  SLAVEIP:/home/postgres/data/

psql

SELECT pg_stop_backup();

至此,主库配置完成


从库上接着来:

vi data/postgresql.conf

hot_standby=on

listen_addresses='*'

开放5432端口

vi data/recovery.conf 输入

standby_mode='on'

primary_conninfo='host=MASTERIP port=5432 user=postgres'

restore_command='cp /home/postgres/wals/%f %p </dev/null'

trigger_file='/tmp/pg.trigger'

archive_cleanup_command='pg_archivecleanup /home/postgres/wals/ %r'

现在可以启动从库了。


试着在从库上进行数据库操作,看看从库的反应。


4,在streaming replication的基础上实现slot

超级简单啦,现在。

停止主库

pg_ctl -D data

vi data/postgresql.conf

其他不变,除了:

max_replication_slots = 1

启动主库

psql

select * from pg_create_physical_replication_slot('myslot');

停止从库

修改recovery.conf

加上

primary_slot_name='myslot'

启动从库

5、如何判断slot成功?

a,停止从库

b,在主库上干点活儿

c,去瞄瞄妹纸帅锅

d,开启从库

e,看主库刚才的动作在从库上重做了没。

试一把,把结果告诉我,有问题愿意效劳。



转载于:https://my.oschina.net/progoney/blog/369005