CentOS 6.5下PostgreSQL 故障切换实现(一)

2015-07-16 12:07:25 · 作者: · 浏览: 5

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


它提供了以下功能


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


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


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


一、安装


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