hillpig的个人博客分享 http://blog.sciencenet.cn/u/hillpig 畅想ing,思考ing,前行ing Email:bluevaley@gmail.com

博文

PostgreSQL9.1 Hot-Standby ---之基于拷贝WAL文件的方法

已有 9681 次阅读 2012-2-26 12:35 |个人分类:postgresql|系统分类:科研笔记| Hot-Standby

本教程是PostgreSQL Cluster系列教程的一部分,该系列包括:

  1. PostgreSQL9.1 PITR示例  (该教程主要阐述DBA如何基于WAL日志做备份恢复)

  2. PostgreSQL9.1 Warm-Standby ---之基于拷贝WAL文件的方法        (file-based log shipping)

  3. PostgreSQL9.1 Warm-Standby ---之基于流复制的方法 (streaming        replication)

  4. PostgreSQL9.1 Warm-Standby ---之基于同步复制的方法 (Synchronous Replication)

  5. PostgreSQL9.1 Hot-Standby ---之基于拷贝WAL文件的方法

  6. PostgreSQL9.1 Hot-Standby ---之基于流复制的方法

  7. PostgreSQL9.1 Hot-Standby ---之基于同步复制的方法

  8. PG9.1+pgpool-II3.1--之HA (Hot-Standby+Streaming Replication)

  9. PG9.1+pgpool-II3.1--之Load Balancing (when meeting large amounts of requests)

  10. PG9.1+pgpool-II3.1--之Parallel Query (when meeting large amounts of data)

  11. PostgreSQL9.1 HA --- 之Slony

实际上有了第2个教程PostgreSQL9.1 Warm-Standby ---之基于拷贝WAL文件的方法,这里我们谈如何基于拷贝WAL文件的方法设置Hot-Standby 就非常容易了,简单的来讲,只需要修改/增加一些参数就可以了。
先说说warm-standby和hot-standby的区别,根据2.4.2 Warm Standby,:

  • The Standby is offline (in "recovery mode") and not available for any query workload. This allows the Standby to be brought up to full operation very quickly.  Warm Standby has been available since version 8.3,...

和2.4.3 Hot Standby

  • Hot Standby is identical to Warm Standby, except that the Standby is available to run read-only queries.  This offers all of the advantages of Warm Standby, plus the ability to distribute some business workload to the Standby server(s).  Hot Standby by itself requires Log Shipping.
    Hot Standby is used both for database failover, and can also be used for load-balancing.

以及根据25. High Availability,        Load Balancing, and Replication

  • A standby server that  cannot be connected to until it is promoted to a master server is  called a warm standby server, and one  that can accept connections and serves read-only queries is  called a hot standby server.

我们知道,Warm-Standby在正常情况下是不允许客户端连接的,只能在Failover时提升为master时才可以允许客户端链接,而Hot-Standby在正常情况下是允许客户端连接的,故Hot-Standby正常运行下可以用来做负载均衡。根据Getting ready for PostgreSQL 9.1,第31页,我们上张图:

好,有了hot-standby可以做什么事情呢?我大概总结了几点(有新的应用方式的朋友可以告诉我):

  1. load balancing是最基本的了,但是需要前端有个调度器(关于调度器,可否有朋友专门写写,postgresql的manual没好好写,2ndQuadrant的人可否多写写)

  2. 不方便把master给第三方用户直接操作,让hot-standby给第三方只读访问,这可作为数据共享

  3. 让hot-standby和master分离,在hot-standby上做数据分析和数据挖掘,做决策支持

特别是本文,由于采用拷贝WAL日志文件的方式,Master和Hot-standby有不小的延迟(当填满WAL段文件的时候才有效),适合做第2和第3类应用。
为让初学者能够顺利的上机实验,本教程还是从安装说起,先说机器配置,建议你都先创建好:

  1. /home/postgres/db/master/pgsql 目录是master数据库的目录,端口为5432

  2. /home/postgres/db/standby/pgsql 目录是一台standy数据库的目录,端口为6432

  3. /home/postgres/archive 是master->standby的WAL日志中转地点

  4. /home/postgres/base 是master->standby的基础备份库的目录

我们说说怎么配置:
1. Master和Hot-standby数据库服务器的安装:
master安装并插入100万条数据(本文不用ssl,故在configure时不用--with-openssl,本文不再使用pg_standby扩展,也不许要make world):
cd /home/postgres/develop/
rm -fr postgresql-9.1.2
tar zxf postgresql-9.1.2.tar.gz
cd postgresql-9.1.2
./configure --prefix=/home/postgres/db/master/pgsql --with-includes=/usr/local/readline/include --with-libraries=/usr/local/readline/lib
make
make install
/home/postgres/db/master/pgsql/bin/initdb -D /home/postgres/db/master/pgsql/data
/home/postgres/db/master/pgsql/bin/postmaster -D /home/postgres/db/master/pgsql/data
/home/postgres/db/master/pgsql/bin/createdb mydb
/home/postgres/db/master/pgsql/bin/psql mydb
mydb=# create table foo(id bigint);
mydb=# insert into foo select * from generate_series(1,1000000);  


Hot-standby:
cd /home/postgres/develop/
rm -fr postgresql-9.1.2
tar zxf postgresql-9.1.2.tar.gz
cd postgresql-9.1.2
./configure --prefix=/home/postgres/db/standby/pgsql --with-includes=/usr/local/readline/include --with-libraries=/usr/local/readline/lib --with-pgport=6432
make
make install
/home/postgres/db/standby/pgsql/bin/initdb -D /home/postgres/db/standby/pgsql/data
然后修改postgresql.conf:port = 6432,保存。
/home/postgres/db/standby/pgsql/bin/postmaster -D /home/postgres/db/standby/pgsql/data
/home/postgres/db/standby/pgsql/bin/createdb mydb --port=6432
好,最后查看一下是否安装成功:
/home/postgres/db/standby/pgsql/bin/psql mydb --port=6432


2. 配置Master,并做一次base backup
编辑 postgresql.conf 以支持 WAL archiving:
wal_level = hot_standby
archive_mode = on
archive_command = 'cp %p /home/postgres/archive/%f'

然后做一次基础备份(后面的Hot-standby主要使用data目录下文件):
mydb=# SELECT pg_start_backup('bak20120228');
        [postgres@localhost pgsql]$ cd /home/postgres/db/master/pgsql/
        [postgres@localhost pgsql]$ tar czvf /home/postgres/base/base_data.tar.gz data/
mydb=# SELECT pg_stop_backup();


3.配置Hot-standby,并启动
把上面基础备份的打包文件覆盖standby的data目录:
cd /home/postgres/db/standby/pgsql/
mv data data_bk
tar -xzvf /home/postgres/base/base_data.tar.gz
rm -r data/pg_xlog/
mkdir -p data/pg_xlog/archive_status
rm data/postmaster.pid
cp /home/postgres/db/standby/pgsql/share/recovery.conf.sample /home/postgres/db/standby/pgsql/data/recovery.conf
cp /home/postgres/db/standby/pgsql/data_bk/postgresql.conf /home/postgres/db/standby/pgsql/data/postgresql.conf

cp /home/postgres/db/standby/pgsql/data_bk/pg_hba.conf /home/postgres/db/standby/pgsql/data/pg_hba.conf
然后编辑recovery.conf:
standby_mode = on
restore_command = 'cp /home/postgres/archive/%f %p'

然后编辑pstgresql.conf:
hot_standby = on
好,启动Hot-Standby:
/home/postgres/db/standby/pgsql/bin/postmaster -D /home/postgres/db/standby/pgsql/data --port=6432

4. 冒烟测试(Smoke test,即配置好了系统首次进行测试):
在Master里插入一些新数据,看看是不是在standby里在进行恢复:
mydb=# insert into foo select * from generate_series(1,1000000);  
然后启动一个standby的客户端psql,查看是不是新数据过来了:
mydb=# select count(*) from foo
如果还没过来,请到master的客户端psql里敲入:
mydb=# select pg_switch_xlog();
再看一下是不是过来了?

我认为Hot-standby的配置是一方,关于如何用Hot-standby却也是好好去琢磨的,例如从25.5. Hot Standby这里我们知道,在Hot-standby上有许多操作不能做,以及query cancel的事情,大家可以自行去研究,此文不再探讨。
至此结束。


加我私人微信,交流技术。




https://blog.sciencenet.cn/blog-419883-541510.html

上一篇:滴答
下一篇:PostgreSQL9.1 Hot-Standby ---之基于流复制的方法
收藏 IP: 223.72.72.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-28 16:57

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部