来自sure网友的博文,原文地址:http://my.oschina.net/Suregogo/blog/551626。
环境:
- OS:
[ha@node0 ~]$ uname -a Linux node0 2.6.32-358.el6.x86_64 #1 SMP Fri Feb 22 00:31:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
- 内存:1G
- CPU:1核
- 数据库:
postgres=# select version(); version -------------------------------------------------------------------------------------------------------- PostgreSQL 9.4.5 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.4.7 20120 313 (Red Hat 4.4.7-16), 64-bit (1 row) postgres=#
- .bash_profile
[ha@node0 ~]$ cat .bash_profile # .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs export PGHOME=/home/ha/pgdb/ export PGDATA=/home/ha/pgdb/data/ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PGHOME/lib/ export PATH=$PGHOME/bin:$PATH:$HOME/bin
搭建过程:
1、主库配置
- postgresql.conf:
listen_addresses = '*' wal_level = hot_standby archive_mode = off max_wal_senders = 3 wal_keep_segments = 16
这里要说明的是参数archive_mode,我在实验过程中有一次发现参数archive_mode和archive_command在不设置的情况下也可以完成流复制,所以我对此进行了调研,发现francs已经对此进行了解释,这里引用francs的结论:
在搭建流复制环境时,并不必须设置 archive_mode 参数为 on ,很多资料在介绍搭建流复制环境时设置这个参数为 on ,可能是出于开启 WAL 归档更安全的原因,因为在主库宕机并且较长时间不能恢复时,从库依然可以读取归档目录的 WAL,从而保证不丢数据; 另一方面,如果主库设置了较大的 wal_keep_segments ,也可以不用开启archive_mode,因为主库保留了足够的 WAL,从而大大降低了因从库所需要的 WAL 被主库覆盖而需要从归档去取 WAL 的情况。所以从这方面说,archive_mode 参数的设置与搭建流复制并没有直接关系。 提示: 对于比较繁忙的库,在搭建流复制从库时,建议主库设置较大的 wal_keep_segments 参数。
- pg_hba.conf
host all all 192.168.238.0/24 md5 host replication rep 192.168.238.0/24 trust
这里说明的是,网上有其他对replication设置为md5,并建立.pgpass文件,这种方法也是可行的。
- 新建用户rep
create user rep replication encrypted password 'rep';
2.使用pg_basebackup进行从库备份
[ha@localhost pgdb]$ pg_basebackup -D $PGDATA -F p -X stream -v -P -h 192.168.238.130 -U rep transaction log start point: 0/2000028 on timeline 1 pg_basebackup: starting background WAL receiver 20945/20945 kB (100%), 1/1 tablespace transaction log end point: 0/20000F0 pg_basebackup: waiting for background process to finish streaming ... pg_basebackup: base backup completed [ha@localhost pgdb]$ ll total 20 drwxrwxr-x. 2 ha ha 4096 Dec 26 00:54 bin drwx------. 18 ha ha 4096 Dec 26 12:02 data drwxrwxr-x. 4 ha ha 4096 Dec 26 00:54 include drwxrwxr-x. 4 ha ha 4096 Dec 26 00:54 lib drwxrwxr-x. 4 ha ha 4096 Dec 26 00:54 share [ha@localhost pgdb]$ cd data/ [ha@localhost data]$ ls backup_label pg_hba.conf pg_replslot pg_subtrans postgresql.auto.conf base pg_ident.conf pg_serial pg_tblspc postgresql.conf global pg_logical pg_snapshots pg_twophase pg_clog pg_multixact pg_stat PG_VERSION pg_dynshmem pg_notify pg_stat_tmp pg_xlog
这里需要对目录进行一下说明:data目录可以通过pg_basebackup自动创建,但是其他表空间,需要手动创建并赋权,这里需要注意的是,权限为700。
3.从库配置
- postgresql.conf
hot_standby = on
- recovery.conf
standby_mode = on primary_conninfo = 'host=192.168.238.130 port=5432 user=rep' trigger_file = '/home/ha/pgdb/pg.trigger.file'
4.备库启动前,主库进程
[ha@localhost pgdb]$ ps -ef | grep post root 2124 1 0 04:47 ? 00:00:00 /usr/libexec/postfix/master postfix 2147 2124 0 04:47 ? 00:00:00 qmgr -l -t fifo -u postfix 10385 2124 0 11:27 ? 00:00:00 pickup -l -t fifo -u ha 10691 1 0 12:33 pts/3 00:00:00 /home/ha/pgdb/bin/postgres_ora ha 10693 10691 0 12:33 ? 00:00:00 postgres: checkpointer process ha 10694 10691 0 12:33 ? 00:00:00 postgres: writer process ha 10695 10691 0 12:33 ? 00:00:00 postgres: wal writer process ha 10696 10691 0 12:33 ? 00:00:00 postgres: autovacuum launcher process ha 10697 10691 0 12:33 ? 00:00:00 postgres: stats collector process ha 10717 4087 0 12:37 pts/3 00:00:00 grep post
5.备库启动后
- 主库进程
[ha@localhost pgdb]$ ps -ef | grep post root 2124 1 0 04:47 ? 00:00:00 /usr/libexec/postfix/master postfix 2147 2124 0 04:47 ? 00:00:00 qmgr -l -t fifo -u postfix 10385 2124 0 11:27 ? 00:00:00 pickup -l -t fifo -u ha 10691 1 0 12:33 pts/3 00:00:00 /home/ha/pgdb/bin/postgres_ora ha 10693 10691 0 12:33 ? 00:00:00 postgres: checkpointer process ha 10694 10691 0 12:33 ? 00:00:00 postgres: writer process ha 10695 10691 0 12:33 ? 00:00:00 postgres: wal writer process ha 10696 10691 0 12:33 ? 00:00:00 postgres: autovacuum launcher process ha 10697 10691 0 12:33 ? 00:00:00 postgres: stats collector process ha 10718 10691 0 12:37 ? 00:00:00 postgres: wal sender process rep 192.168.238.131(59195) streaming 0/3000060 ha 10720 4087 0 12:37 pts/3 00:00:00 grep post
- 备库进程
[ha@localhost data]$ ps -ef | grep post root 2086 1 0 04:48 ? 00:00:00 /usr/libexec/postfix/master postfix 2108 2086 0 04:48 ? 00:00:00 qmgr -l -t fifo -u postfix 9657 2086 0 11:27 ? 00:00:00 pickup -l -t fifo -u ha 9782 1 0 12:36 pts/2 00:00:00 /home/ha/pgdb/bin/postgres ha 9783 9782 0 12:36 ? 00:00:00 postgres: startup process recovering 000000010000000000000003 ha 9784 9782 0 12:36 ? 00:00:00 postgres: checkpointer process ha 9785 9782 0 12:36 ? 00:00:00 postgres: writer process ha 9786 9782 0 12:36 ? 00:00:00 postgres: stats collector process ha 9787 9782 0 12:36 ? 00:00:00 postgres: wal receiver process streaming 0/3000060 ha 9792 3744 0 12:37 pts/2 00:00:00 grep post
6.实验效果
- 主库
[ha@localhost pgdb]$ psql postgres psql (9.4.5) Type "help" for help. postgres=# create table test(id int); CREATE TABLE postgres=# insert into test values (1),(2); INSERT 0 2 postgres=# select * from test ; id ---- 1 2 (2 rows) postgres=#
- 备库
[ha@localhost data]$ psql postgres psql (9.4.5) Type "help" for help. postgres=# select * from test ; id ---- 1 2 (2 rows)
https://fyhuanan.tiancebbs.cn/ https://shqpkaq.tiancebbs.cn/ http://km.lstcxxw.cn/haixi/ https://rhshi.tiancebbs.cn/ http://jinqiang.ahtcbmw.cn/gzmdnc/ http://huilong.sctcbmw.cn/ynsm/ http://yz.cqtcxxw.cn/jys/ http://fuyang.tjtcbmw.cn/hnzjj/ http://cf.lstcxxw.cn/jiayuguan/ http://huaguang.jxtcbmw.cn/gczp/ http://ouyu.hftcbmw.cn/ydqczr/ http://taiying.njtcbmw.cn/jjxyx/ http://cf.lstcxxw.cn/nxszs/ http://cf.lstcxxw.cn/shaoxing/ https://yb.tiancebbs.cn/ https://ljgucheng.tiancebbs.cn/ http://huilong.sctcbmw.cn/changsha/
http://huilong.sctcbmw.cn/lanzhou/ http://yz.cqtcxxw.cn/cdq/ http://nalei.zjtcbmw.cn/jxnc/ https://bachengxi.tiancebbs.cn/ http://taiying.njtcbmw.cn/zpq/ https://xinyuan.tiancebbs.cn/ http://js.sytcxxw.cn/luohe/ https://jiangnandadaonan.tiancebbs.cn/ http://ouyu.hftcbmw.cn/nxzw/ http://tuiguang.hntcxxw.cn/hainan/ http://bjtcxxw.cn/qujing/ https://baituzhen.tiancebbs.cn/ https://baizhuzhen.tiancebbs.cn/ http://cf.lstcxxw.cn/liangshan/ http://taiying.njtcbmw.cn/sdrz/ https://zulin.tiancebbs.cn/fxsh/ https://yangqiao.tiancebbs.cn/
有深意的网名:https://www.nanss.com/mingcheng/986.html 两字网名高冷:https://www.nanss.com/mingcheng/1380.html 男伤感网名:https://www.nanss.com/mingcheng/1415.html 昵称qq:https://www.nanss.com/mingcheng/1227.html 让对方感动的网名:https://www.nanss.com/mingcheng/797.html 网络游戏名字排行:https://www.nanss.com/mingcheng/1086.html 大气的名字:https://www.nanss.com/shenghuo/1179.html 经典群名称:https://www.nanss.com/mingcheng/1361.html 好看的微信号:https://www.nanss.com/mingcheng/506.html 独一无二情侣网名:https://www.nanss.com/mingcheng/1212.html 两个字的游戏名字:https://www.nanss.com/mingcheng/1253.html 关于山的诗句:https://www.nanss.com/xuexi/799.html 两个字女生网名:https://www.nanss.com/mingcheng/618.html 对感情失望的说说:https://www.nanss.com/wenan/1077.html 最好听的网名女:https://www.nanss.com/mingcheng/1467.html 简短优美句子摘抄大全:https://www.nanss.com/xuexi/1116.html 表达心情失落的句子:https://www.nanss.com/yulu/1269.html 值得用一辈子的网名:https://www.nanss.com/mingcheng/700.html 好听的小清新网名:https://www.nanss.com/mingcheng/1412.html 仙气的网名:https://www.nanss.com/mingcheng/882.html 恨一个人到极致的感觉句子:https://www.nanss.com/yulu/652.html 失恋的网名:https://www.nanss.com/mingcheng/642.html 蜘蛛开店续写故事:https://www.nanss.com/xuexi/576.html 最吸引人的抖音昵称:https://www.nanss.com/mingcheng/1429.html 开启美好一天的句子:https://www.nanss.com/yulu/1068.html 花木兰经典台词:https://www.nanss.com/shenghuo/1324.html 为了你我愿意的句子:https://www.nanss.com/wenan/1260.html 最有情商的生日祝福:https://www.nanss.com/yulu/1007.html 伤感的名字:https://www.nanss.com/mingcheng/641.html 简单高级的英文id:https://www.nanss.com/mingcheng/899.html
[url=http://www.t.com]t[/url]
权限问题 #find / -name ./.pg_hba .conf.swp 或 #locate ./.pg_hba .conf.swp yum 安装默认应该在/var/lib/pgsql/11/data 目录下 #ls -la 查看权限 -rwx------. 1 postgres postgres 24576 Aug 10 12:33 .postgresql.conf.swm -rwx------. 1 postgres postgres 16384 Aug 10 12:33 .postgresql.conf.swn -rwx------. 1 postgres postgres 20480 Aug 10 12:33 .postgresql.conf.swo -rwx------. 1 postgres postgres 24576 Aug 10 12:33 .postgresql.conf.swp 权限、所有者、所有组不对就修改下,重启服务之后会正常.
使用pg_basebackup遇到了个权限问题:pg_basebackup: starting background WAL receiver
24/27923 kB (100%), 1/1 tablespace
pg_basebackup: could not get transaction log end position from server: ERROR: could not open file "./.pg_hba.conf.swp": Permission denied