CentOS 6.5下PostgreSQL 故障切换实现


pgpool-II 是一个中间 件,工作在PostgreSQL多服 务 器和PostgreSQL数据 库 客 户 端之 间。

它提供了以下功能

连 接池: pgpool -Ⅱ保存 连 接到PostgreSQL服 务 器,并重复利用具有相同属性的新的 连 接(即用 户 名,数据 库 , 协议 的版本),减少 连 接的开 销 ,并提高了系 统 的整体吞吐量。复制: pgpool - II可以管理多个PostgreSQL服务器。 使用复制功能,可以 实时备份在 2个或多个物理磁 盘 上,因此即使在硬 盘出故障的时候也不用停止服务。

负载 平衡: 如果数据 库 是复制,任何服 务 器上 执 行一个SELECT 查 询 将返回相同的 结 果。 pgpool -Ⅱ采用一个复制功能 优 势 是,以减少多个服 务 器之 间 分配上的SELECT 查 询 每个PostgreSQL服 务 器的 负载 ,提高系 统 的整体吞吐量。在最好的,性能的提高比例的PostgreSQL服 务 器的数量。在同一 时间有 大量用 户 的 查 询的时候,负载 平衡的情况下有最佳的 执 行。

连接超 过限制 : 有一个关于与 PostgreSQL 的最大并 发连 接数限制,最大 连接数超过后 的 连 接被拒 绝 。 设 置最大 连 接数,但是增加的 资 源消耗和影响系 统 性能。 pgpool - II 也有 对 最大 连 接数的限制,但 额 外的 连 接将被排 队 ,而不是立即返回 错误 。

并行查询 : 使用并行 查 询 功能,数据可分布在多个服 务 器中,以便 查 询 可以 执 行所有服 务 器上同 时 减少 总 体 执 行 时间 。 并行 查 询 的工作 时 候 , 寻 找最佳的大 规 模的数据。
 
进行pgpool搭建前需要配置好postgresql的流复制,操作步骤参考

一、安装

wget http://www.pgpool.net/download.php?f=pgpool-II-3.4.0.tar.gz
 tar -zxvf pgpool-3.4.0.tar.gz
 cd pgpool-II-3.4.0/
 ./configure --prefix=/usr/local/pgpool --with-pgsql=path --with-pgsql=/usr/local/pgsql
 make
 make install
 chown postgres.postgres /usr/local/pgpool/ -R
 chown postgres.postgres /usr/src/pgpool-II-3 -R
 mkdir /var/run/pgpool
 chown postgres.postgres /var/run/pgpool/
 #切换postgres 用户安装一些函数
 su - postgres
 
 cd /usr/src/pgpool-II-3.4.0/src/sql/
 make
 make install
 cd pgpool-recovery/
 make install
 cd ../pgpool-regclass/
 make install

二、配置

cd /usr/local/pgpool/etc
 cp pcp.conf.sample pcp.conf
 pg_md5 postgres
 e8a48653851e28c69d0506508fb27fc5
 echo "postgres:e8a48653851e28c69d0506508fb27fc5" >> pcp.conf
 echo "postgres:e8a48653851e28c69d0506508fb27fc5" >> pool_passwd
 cp pool_hba.conf.sample pool_hba.conf
 vim pool_hba.conf
 host    all        postgres    db2                  md5
 
 
 listen_addresses = '*'                    #允许所有主机监听
 port = 9999                            #访问端口
 backend_hostname0 = 'db1'                #DBmaster ip
 backend_port0 = 5432                    #DBmaster postgresql 端口
 backend_weight0 = 1                    #权重
 backend_data_directory0 = '/opt/data'    #DBmaster 数据库目录
 backend_flag0 = 'ALLOW_TO_FAILOVER'    #允许切换
 
 backend_hostname0 = 'db2'
 backend_port0 = 5432
 backend_weight0 = 1
 backend_data_directory0 = '/opt/data'
 backend_flag0 = 'ALLOW_TO_FAILOVER'
 
 enable_pool_hba = on  #随意,自由定制,使用 pool_hba.conf 对client的验证
 pool_passwd = 'pool_passwd' #md5验证文件
 sr_check_user = 'postgres'  #用来故障切换的用户
 
 failover_command = '/usr/local/pgsql/bin/failover_command.sh %d %H /tmp/trigger_file'

故障切换脚本
 
vim /usr/local/pgsql/bin/failover_command.sh
 
#! /bin/sh
# Failover command for streaming replication.
# This script assumes that DB node 0 is primary, and 1 is standby.

# If standby goes down, do nothing. If primary goes down, create a
# trigger file so that standby takes over primary node.
#
# Arguments: $1: failed node id. $2: new master hostname. $3: path to
# trigger file.
failed_node=$1
new_master=$2
trigger_file=$3
# Do nothing if standby goes down.
#if [ $failed_node = 1 ]; then
#      exit 0;
#fi
# Create the trigger file.
/usr/bin/ssh -T $new_master /bin/touch $trigger_file
exit 0;
chmod +x /usr/local/pgsql/bin/failover_command.sh

三、调试

 启动命令,带有日志输出
 
[postgres@db1 etc]$ pgpool -nd >/tmp/pgpool.log 2>&1 &
[postgres@db1 etc]$ netstat -ntlp
 (Not all processes could be identified, non-owned process info
  will not be shown, you would have to be root to see it all.)
 Active Internet connections (only servers)
 Proto Recv-Q Send-Q Local Address              Foreign Address            State      PID/Program name   
 tcp        0      0 0.0.0.0:22                  0.0.0.0:*                  LISTEN      -                   
 tcp        0      0 127.0.0.1:25                0.0.0.0:*                  LISTEN      -                   
 tcp        0      0 0.0.0.0:9898                0.0.0.0:*                  LISTEN      16664/pgpool       
 tcp        0      0 0.0.0.0:9999                0.0.0.0:*                  LISTEN      16664/pgpool       
 tcp        0      0 :::22                      :::*                        LISTEN      -                   
 tcp        0      0 ::1:25                      :::*                        LISTEN      -                   
 tcp        0      0 :::9999                    :::*                        LISTEN      16664/pgpool

 
登录

[postgres@db1 etc]$ psql -U postgres -h db1 -p 9999
 psql (9.2.1)
 Type "help" for help.
 postgres=# show pool_nodes;
 node_id | hostname | port | status | lb_weight |  role   
---------+----------+------+--------+-----------+---------
 0      | db1      | 5432 | 2      | 0.500000  | primary
 1      | db2      | 5432 | 2      | 0.500000  | standby
(2 rows)
 postgres=# create database db0;
 CREATE DATABASE

2:启动
3:死啦

测试可以登录,可以读写

四、故障切换

首先停止DBmaster
[postgres@db1 etc]$ pg_ctl -m fast stop

登录查看

[postgres@db1 etc]$ psql -U postgres -h db1 -p 9999
 postgres=# show pool_nodes;
 node_id | hostname | port | status | lb_weight |  role   
---------+----------+------+--------+-----------+---------
 0      | db1      | 5432 | 3      | 0.500000  | standby
 1      | db2      | 5432 | 2      | 0.500000  | primary
(2 rows)

此时DBslave显示的日志信息

[postgres@db2 data]$ FATAL:  replication terminated by primary server
LOG:  record with zero length at 0/10000FE0
LOG:  trigger file found: /tmp/trigger_file
LOG:  redo done at 0/10000F80
LOG:  last completed transaction was at log time 2015-06-17 11:05:44.379009+08
LOG:  selected new timeline ID: 2
LOG:  archive recovery complete
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started

DBmaster 已经死啦,状态切换为standby;DBslave切换为primary;测试可读写
日志提示,发现 trigger_file文件,进行切换
DBslave 切换为 主服务器
recover.conf自动更改为recover.done

[postgres@db2 data]$ ll /opt/data/recovery.done
recovery.done

故障切换成功

五、恢复DBmaster

1、同步DBmaster至DBslave

pg_basebackup -D $PGDATA -Fp -Xs -v  -h db1 -p 5432 -U postgres

2、修改配置文件

listen_addresses = '*'
port = 5432
hot_standby = on

3、修改recover文件

mv recover.done  recover.conf
vim recover.conf
primary_conninfo = 'host=172.16.0.133 port=5432 user=postgres'

4、启动DBslave
5、添加node

pcp_attach_node -d 5 db1 9898 postgres postgres 0
pcp_attach_node -d 5 db1 9898 postgres postgres 1

登录查看

postgres=# show pool_nodes;
node_id | hostname | port | status | lb_weight |  role   
---------+----------+------+--------+-----------+---------
 0      | db1      | 5432 | 2      | 0.500000  | primary
 1      | db2      | 5432 | 2      | 0.500000  | standby
(2 rows)


恢复正常
Game Over !

------------------------------------华丽丽的分割线------------------------------------

CentOS 6.3环境下yum安装PostgreSQL 9.3

PostgreSQL缓存详述

Windows平台编译 PostgreSQL

Ubuntu下LAPP(Linux+Apache+PostgreSQL+PHP)环境的配置与安装

Ubuntu上的phppgAdmin安装及配置

CentOS平台下安装PostgreSQL9.3

PostgreSQL配置Streaming Replication集群

如何在CentOS 7/6.5/6.4 下安装PostgreSQL 9.3 与 phpPgAdmin 

------------------------------------华丽丽的分割线------------------------------------

PostgreSQL 的详细介绍:请点这里
PostgreSQL 的下载地址:请点这里

本文永久更新链接地址

相关内容