pgpool 主从流复制模式下的安装使用

pgpool-II 是一个位于 PostgreSQL 服务器和 PostgreSQL 数据库客户端之间的中间件,它提供以下功能:
 连接池
pgpool-II 保持已经连接到 PostgreSQL 服务器的连接,并在使用相同参数(例如:用户名,数据库,协议版本)连接进来时重用它们。它减少了连接开销,并增加了系统的总体吞吐量。
 复制
pgpool-II 可以管理多个 PostgreSQL 服务器。激活复制功能并使在2台或者更多 PostgreSQL 节点中建立一个实时备份成为可能,这样,如果其中一台节点失效,服务可以不被中断继续运行。
 负载均衡
如果数据库进行了复制,则在任何一台服务器中执行一个 SELECT 查询将返回相同的结果。pgpool-II 利用了复制的功能以降低每台 PostgreSQL 服务器的负载。它通过分发 SELECT 查询到所有可用的服务器中,增强了系统的整体吞吐量。在理想的情况下,读性能应该和 PostgreSQL 服务器的数量成正比。负载均衡功能在有大量用户同时执行很多只读查询的场景中工作的效果最好。
 并行查询
使用并行查询时,数据可以被分割到多台服务器上,所以一个查询可以在多台服务器上同时执行,以减少总体执行时间。并行查询在查询大规模数据的时候非常有效。
现有的数据库应用程序基本上可以不需要修改就可以使用 pgpool-II


现有环境:
主:pgtest01    192.168.12.251  pg9.4.5
从:pgtest02    192.168.12.252  pg9.4.5
安装目录 /pgtina
数据目录 /pgtina/data

=================安装pgpool===========
1、下载安装包:
http://www.pgpool.net/mediawiki/index.php/Downloads 
[root@pgtest01 package]# wget http://192.168.10.100/pgpool-II-3.4.3.tar.gz

2、解压安装
[root@pgtest01 package]# tar -zxvf pgpool-II-3.4.3.tar.gz
[root@pgtest02 /]# mkdir pgpool
[postgres@pgtest01 pgtina]$ cat ~/.bash_profile
export LD_LIBRARY_PATH=/pgtina/lib
export PATH=/pgtina/bin:$PATH

[root@pgtest02 pgpool-II-3.4.3]# pwd
/package/pgpool-II-3.4.3
[root@pgtest01 pgpool-II-3.4.3]# ./configure --prefix=/pgpool  -with-pgsql=path -with-pgsql=/pgtina  --with-openssl  默认情况下,pgpool-II将安装到 /usr/local 目录。

--编译报错:
checking openssl/ssl.h usability... no
checking openssl/ssl.h presence... no
checking for openssl/ssl.h... no
configure: error: header file <openssl/ssl.h> is required for SSL

[root@pgtest01 pgpool-II-3.4.3]# yum install openssl
[root@pgtest01 pgpool-II-3.4.3]# yum install openssl-devel   可以写成yum install -y  openssl openssl-devel

编译通过,进行安装
make
make install

3、安装pg_regclass 和pg_recovery(两节点操作)
如果你在使用 PostgreSQL 8.0 或之后的版本,强烈推荐在需要访问的 PostgreSQL 中安装 pgpool_regclass 函数,
因为它被 pgpool-II 内部使用。 如果不这样做,在不同的 schema 中处理相同的表名会出现问题(临时表不会出问题)。
[root@pgtest01 extension]# find / -name "pgpool*.sql"
/package/pgpool-II-3.4.3/src/sql/pgpool_adm/pgpool_adm--1.0.sql
/package/pgpool-II-3.4.3/src/sql/pgpool-recovery/pgpool_recovery--1.1.sql
/package/pgpool-II-3.4.3/src/sql/pgpool-regclass/pgpool_regclass--1.0.sql

[root@pgtest01 extension]# find / -name "pgpool*.control"
/package/pgpool-II-3.4.3/src/sql/pgpool_adm/pgpool_adm.control
/package/pgpool-II-3.4.3/src/sql/pgpool-recovery/pgpool_recovery.control
/package/pgpool-II-3.4.3/src/sql/pgpool-regclass/pgpool_regclass.control

template1=# create extension pgpool_regclass;
ERROR:  could not open extension control file "/pgtina/share/postgresql/extension/pgpool_regclass.control": No such file or directory

查看手册
http://www.pgpool.net/docs/latest/pgpool-zh_cn.html

正确的方式:
cd pgpool-II-x.x.x/sql/pgpool-regclass
make
make install
在这之后:
psql -f pgpool-regclass.sql template1
或者
psql template1
CREATE EXTENSION pgpool_regclass;

[postgres@pgtest01 ~]$ psql -f /pgtina/share/postgresql/extension/pgpool_regclass--1.0.sql  template1
Use "CREATE EXTENSION pgpool_regclass" to load this file.

[root@pgtest01 pgpool-regclass]# cd /package/pgpool-II-3.4.3/src/sql
[root@pgtest01 sql]# ll
total 24
-rw-rw-r--. 1 postgres postgres  617 Jul 24 13:47 insert_lock.sql
-rw-rw-r--. 1 postgres postgres 1407 Jul 24 13:47 Makefile
drwxrwsr-x. 2 postgres postgres 4096 Jul 24 13:47 pgpool_adm
drwxrwsr-x. 2 postgres postgres 4096 Jul 24 13:47 pgpool-recovery
drwxrwsr-x. 2 postgres postgres 4096 Jul 24 13:47 pgpool-regclass
-rw-rw-r--. 1 postgres postgres  834 Jul 24 13:47 system_db.sql
[root@pgtest01 sql]# make
make: pg_config: Command not found
make: *** No targets.  Stop.
[root@pgtest01 sql]# source /home/postgres/.bash_profile
[root@pgtest01 sql]# make
[root@pgtest01 sql]# make install

[root@pgtest01 sql]# cd /pgtina/share/postgresql/extension/    --果然有了这些文件
[root@pgtest01 extension]# ll
total 44
-rw-r--r--. 1 root     root     2664 Nov 19 14:09 pgpool_adm--1.0.sql
-rw-r--r--. 1 root     root      146 Nov 19 14:09 pgpool_adm.control
-rw-r--r--. 1 root     root     1002 Nov 19 14:09 pgpool_recovery--1.1.sql
-rw-r--r--. 1 root     root      178 Nov 19 14:09 pgpool_recovery.control
-rw-r--r--. 1 root     root      557 Nov 19 14:09 pgpool-recovery.sql
-rw-r--r--. 1 root     root      283 Nov 19 14:09 pgpool_regclass--1.0.sql
-rw-r--r--. 1 root     root      152 Nov 19 14:09 pgpool_regclass.control
-rw-r--r--. 1 root     root      142 Nov 19 14:09 pgpool-regclass.sql
-rw-r--r--. 1 postgres postgres  332 Nov 18 10:51 plpgsql--1.0.sql
-rw-r--r--. 1 postgres postgres  179 Nov 18 10:51 plpgsql.control
-rw-r--r--. 1 postgres postgres  381 Nov 18 10:51 plpgsql--unpackaged--1.0.sql

--再来创建:---成功
template1=# create extension pgpool_regclass;
CREATE EXTENSION
template1=# create extension pgpool_recovery;
CREATE EXTENSION

template1=# \df
                                                              List of functions
Schema |        Name         | Result data type |                               Argument data types                                |  Type 
--------+---------------------+------------------+----------------------------------------------------------------------------------+--------
public | pgpool_pgctl        | boolean          | action text, stop_mode text                                                      | normal
public | pgpool_recovery     | boolean          | script_name text, remote_host text, remote_data_directory text                   | normal
public | pgpool_recovery     | boolean          | script_name text, remote_host text, remote_data_directory text, remote_port text | normal
public | pgpool_remote_start | boolean          | remote_host text, remote_data_directory text                                     | normal
public | pgpool_switch_xlog  | text             | arcive_dir text                                                                  | normal
(5 rows)

备注: 每个库都需要安装 pgpool_regclass,为了方便在 template1 上安装 pgpool_regclass,以后新建的库都以 template1 为模板库。

5.配置 pgpool-II ( 两节点操作)
[root@pgtest02 sql]# cd /pgpool/etc/
[root@pgtest02 etc]# cp pcp.conf.sample pcp.conf

[root@pgtest01 etc]# find / -name "pg_md5"
/package/pgpool-II-3.4.3/src/tools/pgmd5/pg_md5
/pgpool/bin/pg_md5


[root@pgtest01 etc]# /pgpool/bin/pg_md5 -u postgres -p
password: test12
60474c9c10d7142b7508ce7a50acf414
备注: pgpool提供pcp接口,可以查看,管理pgpool的状态,并且可以远程操作pgpool,pcp.conf用来对pcp相关命令认证的文件,格式为USERID:MD5PASSWD。

--编写 pcp.conf 文件,写入以下
# USERID:MD5PASSWD
postgres:60474c9c10d7142b7508ce7a50acf414
pgpool:ba777e4c2f15c11ea8ac3be7e0440aa0

vi /etc/hosts
192.168.12.251 pgtest01
192.168.12.252 pgtest02

--配置 ifconfig, arping 执行权限
[root@pgtest02 etc]# chmod u+s /sbin/ifconfig
[root@pgtest02 etc]# chmod u+s /usr/sbin
备注:  以理普通用户能够执行以上命令,  failover_command 命令要用到。

--配置两节点信任关系
[root@pgtest01 etc]# su - postgres
[postgres@pgtest01 ~]$ ssh postgres@pgtest02   --配置后要求无密码登录
[postgres@pgtest01 ~]$ ssh-keygen
Enter file in which to save the key (/home/postgres/.ssh/id_rsa):
[postgres@pgtest01 ~]$ ssh-copy-id postgres@pgtest02
postgres@pgtest02's password:
Now try logging into the machine, with "ssh 'postgres@pgtest02'", and check in:
  .ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
[postgres@pgtest01 ~]$ ssh postgres@pgtest02   --成功

在另一台机器上也操作一下

--配置 pgpool.conf
[root@pgtest01 etc]# cd /pgpool/etc
[root@pgtest01 etc]# cp pgpool.conf.sample pgpool.conf

--开启日志
    在日志 /etc/rsyslog.conf  加入以下行
# pgpool
local0.*    /var/log/pgpool.log   
   
[root@pgtest01 etc]# vi /etc/rsyslog.conf
[root@pgtest01 etc]# /etc/init.d/rsyslog  restart
Shutting down system logger:                               [  OK  ]
Starting system logger:                                    [  OK  ]


--主节点的 pgpool.conf
[postgres@pgtest01 etc]$ grep ^[a-z] pgpool.conf
listen_addresses = '*'
port = 9999
socket_dir = '/tmp'
pcp_port = 9898
pcp_socket_dir = '/tmp'
backend_hostname0 = '192.168.12.251'   ##配置数据节点 db1
backend_port0 = 5432
backend_weight0 = 1
backend_flag0 = 'ALLOW_TO_FAILOVER'
backend_hostname1 = '192.168.12.252'   ##配置数据节点  db2
backend_port1 = 5432
backend_weight1 = 1
backend_flag1 = 'ALLOW_TO_FAILOVER'
enable_pool_hba = on
pool_passwd = 'pool_passwd'
authentication_timeout = 60
ssl = off
num_init_children = 32
max_pool = 4
child_life_time = 300
child_max_connections = 0
connection_life_time = 0
client_idle_limit = 0
log_destination = 'syslog'
print_timestamp = on
log_connections = on
log_hostname = on
log_statement = on
log_per_node_statement = off
log_standby_delay = 'none'
syslog_facility = 'LOCAL0'
syslog_ident = 'pgpool'
debug_level = 0
pid_file_name = '/pgpool.pid'
logdir = '/pgpool/log'
connection_cache = on
reset_query_list = 'ABORT; DISCARD ALL'
replication_mode = off
replicate_select = off
insert_lock = on
lobj_lock_table = ''
replication_stop_on_mismatch = off
failover_if_affected_tuples_mismatch = off
load_balance_mode = on
ignore_leading_white_space = on
white_function_list = ''
black_function_list = 'nextval,setval'
master_slave_mode = on                     # 设置流复制模式
master_slave_sub_mode = 'stream'   # 设置流复制模式
sr_check_period = 5
sr_check_user = 'repluser'
sr_check_password = 'repluser'
delay_threshold = 16000
follow_master_command = ''
---从这开始没有了
parallel_mode = off                
pgpool2_hostname = ''
system_db_hostname  = 'localhost'
system_db_port = 5432
system_db_dbname = 'pgpool'
system_db_schema = 'pgpool_catalog'
system_db_user = 'pgpool'
system_db_password = ''
----------------
health_check_period = 5
health_check_timeout = 20
health_check_user = 'repuser'
health_check_password = 'rep123us345er'
health_check_max_retries = 3
health_check_retry_delay = 1
failover_command = '/opt/pgpool/failover_stream.sh %H '  ## 配置 failover 脚本,脚本内容下面会贴出。
failback_command = ''
fail_over_on_backend_error = on
search_primary_node_timeout = 10
recovery_user = 'nobody'
recovery_password = ''
recovery_1st_stage_command = ''
recovery_2nd_stage_command = ''
recovery_timeout = 90
client_idle_limit_in_recovery = 0
use_watchdog = on             ---注意打开watchdog
trusted_servers = ''
ping_path = '/bin'
wd_hostname = '192.168.12.251'
wd_port = 9000
wd_authkey = ''
delegate_IP = '192.168.12.240'   ## 配置 pgpool 的 VIP,避免 pgpool 的单点故障
ifconfig_path = '/sbin'    ## 以下几个网卡命令不需要修改。
if_up_cmd = 'ifconfig eth0:0 inet $_IP_$ netmask 255.255.255.0'
if_down_cmd = 'ifconfig eth0:0 down'
arping_path = '/usr/sbin'           # arping command path
arping_cmd = 'arping -U $_IP_$ -w 1'
clear_memqcache_on_escalation = on
wd_escalation_command = ''
wd_lifecheck_method = 'heartbeat'
wd_interval = 10
wd_heartbeat_port = 9694
wd_heartbeat_keepalive = 2
wd_heartbeat_deadtime = 30
heartbeat_destination0 = '192.168.12.252'   # 配置对端的 hostname
heartbeat_destination_port0 = 9694
heartbeat_device0 = 'eth0'
wd_life_point = 3
wd_lifecheck_query = 'SELECT 1'
wd_lifecheck_dbname = 'template1'
wd_lifecheck_user = 'nobody'
wd_lifecheck_password = ''
other_pgpool_hostname0 = '192.168.12.252'   ## 配置对端的 pgpool
other_pgpool_port0 = 9999              ---注意这里
other_wd_port0 = 9000
relcache_expire = 0
relcache_size = 256
check_temp_table = on
memory_cache_enabled = off
memqcache_method = 'shmem'
memqcache_memcached_host = 'localhost'
memqcache_memcached_port = 11211
memqcache_total_size = 67108864
memqcache_max_num_cache = 1000000
memqcache_expire = 0
memqcache_auto_cache_invalidation = on
memqcache_maxcache = 409600
memqcache_cache_block_size = 1048576
memqcache_oiddir = '/var/log/pgpool/oiddir'
white_memqcache_table_list = ''
black_memqcache_table_list = ''

--备节点的 pgpool.conf
[postgres@pgtest02 etc]$ grep ^[a-z] pgpool.conf
listen_addresses = '*'
port = 9999
socket_dir = '/tmp'
pcp_port = 9898
pcp_socket_dir = '/tmp'
backend_hostname0 = '192.168.12.251'
backend_port0 = 5432
backend_weight0 = 1
backend_flag0 = 'ALLOW_TO_FAILOVER'
backend_hostname1 = '192.168.12.252'
backend_port1 = 5432
backend_weight1 = 1
backend_flag1 = 'ALLOW_TO_FAILOVER'
enable_pool_hba = on
pool_passwd = 'pool_passwd'
authentication_timeout = 60
ssl = off
num_init_children = 32
max_pool = 4
child_life_time = 300
child_max_connections = 0
connection_life_time = 0
client_idle_limit = 0
log_destination = 'syslog'
print_timestamp = on
log_connections = on
log_hostname = on
log_statement = on
log_per_node_statement = off
log_standby_delay = 'none'
syslog_facility = 'LOCAL0'
syslog_ident = 'pgpool'
debug_level = 0
pid_file_name = '/opt/pgpool/pgpool.pid'
logdir = '/pgpool/log'
connection_cache = on
reset_query_list = 'ABORT; DISCARD ALL'
replication_mode = off
replicate_select = off
insert_lock = on
lobj_lock_table = ''
replication_stop_on_mismatch = off
failover_if_affected_tuples_mismatch = off
load_balance_mode = on
ignore_leading_white_space = on
white_function_list = ''
black_function_list = 'nextval,setval'
master_slave_mode = on
master_slave_sub_mode = 'stream'
sr_check_period = 0
sr_check_user = 'repluser'
sr_check_password = 'repluser'
delay_threshold = 16000
follow_master_command = ''
-----以下没有---
parallel_mode = off
pgpool2_hostname = ''
system_db_hostname  = 'localhost'
system_db_port = 5432
system_db_dbname = 'pgpool'
system_db_schema = 'pgpool_catalog'
system_db_user = 'pgpool'
system_db_password = ''
------------------------------
health_check_period = 0
health_check_timeout = 20
health_check_user = 'nobody'
health_check_password = ''
health_check_max_retries = 0
health_check_retry_delay = 1
failover_command = '/opt/pgpool/failover_stream.sh %H '
failback_command = ''
fail_over_on_backend_error = on
search_primary_node_timeout = 10
recovery_user = 'nobody'
recovery_password = ''
recovery_1st_stage_command = ''
recovery_2nd_stage_command = ''
recovery_timeout = 90
client_idle_limit_in_recovery = 0
use_watchdog = on
trusted_servers = ''
ping_path = '/bin'
wd_hostname = '192.168.12.252'
wd_port = 9000
wd_authkey = ''
delegate_IP = '192.168.12.240'
ifconfig_path = '/sbin'
if_up_cmd = 'ifconfig eth0:0 inet $_IP_$ netmask 255.255.255.0'
if_down_cmd = 'ifconfig eth0:0 down'
arping_path = '/usr/sbin'           # arping command path
arping_cmd = 'arping -U $_IP_$ -w 1'
clear_memqcache_on_escalation = on
wd_escalation_command = ''
wd_lifecheck_method = 'heartbeat'
wd_interval = 10
wd_heartbeat_port = 9694
wd_heartbeat_keepalive = 2
wd_heartbeat_deadtime = 30
heartbeat_destination0 = '192.168.12.251'
heartbeat_destination_port0 = 9694
heartbeat_device0 = 'eth0'
wd_life_point = 3
wd_lifecheck_query = 'SELECT 1'
wd_lifecheck_dbname = 'template1'
wd_lifecheck_user = 'nobody'
wd_lifecheck_password = ''
other_pgpool_hostname0 = '192.168.12.251'
other_pgpool_port0 = 9999
other_wd_port0 = 9000
relcache_expire = 0
relcache_size = 256
check_temp_table = on
memory_cache_enabled = off
memqcache_method = 'shmem'
memqcache_memcached_host = 'localhost'
memqcache_memcached_port = 11211
memqcache_total_size = 67108864
memqcache_max_num_cache = 1000000
memqcache_expire = 0
memqcache_auto_cache_invalidation = on
memqcache_maxcache = 409600
memqcache_cache_block_size = 1048576
memqcache_oiddir = '/var/log/pgpool/oiddir'
white_memqcache_table_list = ''
black_memqcache_table_list = ''

--/opt/pgpool/failover_stream.sh 脚本内容
[pg93@db1 etc]$ cat /opt/pgpool/failover_stream.sh
#! /bin/sh
# Failover command for streaming replication.
# Arguments: $1: new master hostname.

new_master=$1
trigger_command="$PGHOME/bin/pg_ctl promote -D $PGDATA"

# Prompte standby database.
/usr/bin/ssh -T $new_master $trigger_command

exit 0;
备注: 我这里定义的 failover 脚本和 pgpool 手册上的脚本不同,这里使用了 pg_ctl promote 的切换方式,
一方面以文件触发的形式个人觉得不是很好。另一方面:当以 trigger file 形式实现 HA 时会遇到不能来回切
换的问题。

如果仔细看,可以看到这个切换脚本并不严谨,每当有节点离线时,它都会触发一次,也就是说如果当前掉线的
是备节点,它也会到对端主库执行一次 failover_command 命令,不过没关系,并不影响。


--启动 pgpool(两边都要启动)
[pg93@db2 etc]$ pgpool
备注: 此时可以查看 /var/log/pgpool.log  日志了,注意两节点都启动。

启动失败:
[postgres@pgtest01 bin]$ /pgpool/bin/pgpool
2015-11-19 15:33:30: pid 24712: WARNING:  failed while loading hba configuration from file:"/pgpool/etc/pool_hba.conf"
2015-11-19 15:33:30: pid 24712: DETAIL:  fopen failed with error: "No such file or directory"

cp /pgpool/etc/pool_hba.conf.sample   pool_hba.conf

[postgres@pgtest01 etc]$ ps -ef|grep pgpool
postgres 24713     1  0 15:33 ?        00:00:00 /pgpool/bin/pgpool
postgres 24718 24713  0 15:33 ?        00:00:00 pgpool: watchdog 
postgres 24719 24713  0 15:33 ?        00:00:00 pgpool: heartbeat receiver
postgres 24720 24713  0 15:33 ?        00:00:00 pgpool: heartbeat sender
postgres 24721 24713  0 15:33 ?        00:00:00 pgpool: lifecheck
postgres 24781 24713  0 15:33 ?        00:00:00 pgpool: wait for connection request
postgres 24782 24713  0 15:33 ?        00:00:00 pgpool: wait for connection request
postgres 24783 24713  0 15:33 ?        00:00:00 pgpool: wait for connection request
postgres 24784 24713  0 15:33 ?        00:00:00 pgpool: wait for connection request
postgres 24785 24713  0 15:33 ?        00:00:00 pgpool: wait for connection request
postgres 24786 24713  0 15:33 ?        00:00:00 pgpool: wait for connection request
postgres 24787 24713  0 15:33 ?        00:00:00 pgpool: wait for connection request
postgres 24788 24713  0 15:33 ?        00:00:00 pgpool: wait for connection request
postgres 24789 24713  0 15:33 ?        00:00:00 pgpool: wait for connection request
postgres 24790 24713  0 15:33 ?        00:00:00 pgpool: wait for connection request
postgres 24791 24713  0 15:33 ?        00:00:00 pgpool: wait for connection request
postgres 24792 24713  0 15:33 ?        00:00:00 pgpool: PCP: wait for connection request
postgres 24794 24713  0 15:33 ?        00:00:00 pgpool: worker process
postgres 24939 24680  0 15:38 pts/2    00:00:00 grep pgpool

查看后台日志:
[root@pgtest01 log]# tail -f /var/log/pgpool.log
Nov 19 15:33:38 pgtest01 pgpool[24713]: [96-1] 2015-11-19 15:33:38: pid 24713: LOG:  child process with pid: 24751 exits with status 0
Nov 19 15:33:38 pgtest01 pgpool[24713]: [97-1] 2015-11-19 15:33:38: pid 24713: LOG:  child process with pid: 24751 exited with success and will not be restarted
Nov 19 15:33:38 pgtest01 pgpool[24713]: [98-1] 2015-11-19 15:33:38: pid 24713: LOG:  child process with pid: 24752 exits with status 0

当从库启动后log更新了下面两条:
Nov 19 15:40:34 pgtest01 pgpool[24718]: [10-1] 2015-11-19 15:40:34: pid 24718: LOG:  sending watchdog response
Nov 19 15:40:34 pgtest01 pgpool[24718]: [10-2] 2015-11-19 15:40:34: pid 24718: DETAIL:  receive add request from 192.168.12.252:9999 and accept it

以上的命令不打印日志信息,因为 pgpool 脱离终端了。如果你想显示 pgpool 日志信息,你需要传递 -n 到 pgpool 命令。此时 pgpool-II 作为非守护进程模式运行,也就不会脱离终端了。
$ pgpool -n &
日志消息会打印到终端,所以推荐使用如下的选项。
$  /pgpool/bin/pgpool -n -d > /tmp/pgpool.log 2>&1 &    --命令都要带详细路径。
-d 选项启用调试信息生成。
pgpool -d -n   #
-d 模式Debug下log
-n 是不使用后台模式


以上命令持续追加日志消息到 /tmp/pgpool.log 中。如果你需要切换日志文件,可以将日志传递到一个支持日志轮
换功能的外部命令。例如,你可以使用 Apache2 带的 rotatelogs 工具。

--pgpool 关闭命令
[pg93@db2 etc]$ pgpool -m fast stop

--pgpool reload 命令
[pg93@db1 etc]$ pgpool reload


登录查看,发现这个参数没办法显示:
[postgres@pgtest01 etc]$ psql -h 192.168.12.251 -p 5432 -U postgres -d postgres
psql (9.4.5)
Type "help" for help.

postgres=# show pool_nodes;
ERROR:  unrecognized configuration parameter "pool_nodes"

[postgres@pgtest01 etc]$ psql -h 192.168.12.251 -p 9999 -U postgres -d postgres   --注意端口
psql (9.4.5)
Type "help" for help.

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

备注: db2 为 primary, db1 为 standby,status 为 2 ,表示正常连接; 关于 status 状态,如下
    0 - This state is only used during the initialization. PCP will never display it.
    1 - Node is up. No connections yet.
    2 - Node is up. Connections are pooled.
    3 - Node is down.

参数里面有一个写错了,写成了5433,修改后重启pgpool,standby的status依然是3
[postgres@pgtest01 etc]$ /pgpool/bin/pgpool -m fast stop
2015-11-19 15:54:42: pid 25448: LOG:  stop request sent to pgpool. waiting for termination...
.....done.
[postgres@pgtest01 etc]$ /pgpool/bin/pgpool
[postgres@pgtest01 etc]$

在从库上只看到一条主库的信息
[postgres@pgtest02 pgpool]$ psql -h 192.168.12.252 -p 9999 -U postgres postgres
psql (9.4.5)
Type "help" for help.

postgres=# show pool_nodes;
node_id |    hostname    | port | status | lb_weight |  role  
---------+----------------+------+--------+-----------+---------
0       | 192.168.12.251 | 5432 | 2      | 1.000000  | primary
(1 row)


2015-11-19 16:12:38: pid 25943: DEBUG:  watchdog heartbeat: send heartbeat signal to 192.168.12.252:9694
2015-11-19 16:12:38: pid 25942: DEBUG:  watchdog heartbeat: received heartbeat signal from "192.168.12.252:9999"

ov 19 16:15:56 pgtest02 pgpool[17626]: [149-1] 2015-11-19 16:15:56: pid 17626: DEBUG:  watchdog heartbeat: received heartbeat signal from "192.168.12.251:9999"
Nov 19 16:15:56 pgtest02 pgpool[17628]: [99-1] 2015-11-19 16:15:56: pid 17628: DEBUG:  watchdog life checking by heartbeat
Nov 19 16:15:56 pgtest02 pgpool[17628]: [99-2] 2015-11-19 16:15:56: pid 17628: DETAIL:  checking pgpool 0 (192.168.12.252:9999)
Nov 19 16:15:56 pgtest02 pgpool[17628]: [100-1] 2015-11-19 16:15:56: pid 17628: DEBUG:  watchdog life checking by heartbeat
Nov 19 16:15:56 pgtest02 pgpool[17628]: [100-2] 2015-11-19 16:15:56: pid 17628: DETAIL:  OK; status 3

status = 2 正常在使用中,status=3 被移除需要恢复,启动pg_pool时加入 -D 移除以前的数据库状态。
/pgpool/bin/pgpool -n -d -D  > /tmp/pgpool.log 2>&1 &   两边都重启一下,带上-D参数,然后两台机器看到都正常了。
postgres=# show pool_nodes;
node_id |    hostname    | port | status | lb_weight |  role  
---------+----------------+------+--------+-----------+---------
0       | 192.168.12.251 | 5432 | 2      | 0.500000  | primary
1       | 192.168.12.252 | 5432 | 2      | 0.500000  | standby
(2 rows)


六: HA 验证
根据上面 show_nodes 输出, 此时192.168.12.251节点为主库,192.168.12.252节点为备库,并且  pgpool 目前跑在192.168.12.251上

[root@pgtest01 pgpool]# chmod +x failover_stream.sh
[root@pgtest01 pgpool]# cat failover_stream.sh
#! /bin/sh
# Failover command for streaming replication.
# Arguments: $1: new master hostname.
new_master=$1
trigger_command="$PGHOME/bin/pg_ctl promote -D $PGDATA"
# Prompte standby database.
/usr/bin/ssh -T $new_master $trigger_command
exit 0;
[root@pgtest01 pgpool]# echo $PGHOME
添加环境变量
[root@pgtest01 pgpool]# su - postgres
[postgres@pgtest01 ~]$ echo $PGHOME

[postgres@pgtest01 ~]$ vi .bash_profile
export PATH=$PATH:$HOME/bin
export LD_LIBRARY_PATH=/pgtina/lib
export PATH=/pgtina/bin:$PATH
export PGHOME=/pgtina
export PGDATA=/pgtina/data

1 关 12.251上的数据库
12.251关闭数据库
[postgres@pgtest01 ~]$ pg_ctl -m fast stop
waiting for server to shut down.... done
server stopped

12.251上查看
[postgres@pgtest01 ~]$ pg_controldata |grep cluster
Database cluster state:               shut down
12.252上查看
[postgres@pgtest02 ~]$ pg_controldata |grep cluster
Database cluster state:               in archive recovery

postgres=# show pool_nodes;
node_id |    hostname    | port | status | lb_weight |  role  
---------+----------------+------+--------+-----------+---------
0       | 192.168.12.251 | 5432 | 3      | 0.500000  | standby
1       | 192.168.12.252 | 5432 | 2      | 0.500000  | standby    ---没有变成主库,可能跟我设定了trigger_file有关系   #trigger_file = '/tmp/pg.trigger.252' 注释掉

重新启动了主库12.251,发现状态没变化,但主库可以同步,现在再重启一次pgpool试试。加上-D
[postgres@pgtest01 ~]$ /pgpool/bin/pgpool -n -d -D> /tmp/pgpool.log 2>&1 &    恢复了状态

再来试试切换:
[postgres@pgtest01 ~]$ pg_ctl -D /pgtina/data stop
waiting for server to shut down....... done
server stopped
[postgres@pgtest01 ~]$ pg_controldata |grep cluster
Database cluster state:               shut down
[postgres@pgtest02 data]$ pg_controldata |grep cluster  --12.252
Database cluster state:               in production


[pg93@db1 etc]$ ll /pgtina/data/recovery.done
备注:可以看到12.252节点已完成从 standby 角色切换到 primary ,并且 $PGDATA/recovery.conf 文件变成 recovery.done.

查看 pgpool 状态
[postgres@pgtest01 pgpool]$ psql -h 192.168.12.240 -p 9999 -U postgres postgres   ---使用vip连接pgpool
psql (9.4.5)
Type "help" for help.
postgres=# show pool_nodes;
node_id |    hostname    | port | status | lb_weight |  role  
---------+----------------+------+--------+-----------+---------
0       | 192.168.12.251 | 5432 | 3      | 0.500000  | standby
1       | 192.168.12.252 | 5432 | 2      | 0.500000  | primary
(2 rows)
备注:  12.252已转换成  primary 角色。 12.251状态为 3 ,表示 down 状态。

接下来以 standby 身份启动12.251    --注意两边的recovery.conf配置内容不一样哟
[postgres@pgtest01 data]$ mv recovery.done recovery.conf
[postgres@pgtest01 data]$ vi recovery.conf
primary_conninfo = 'host=192.168.12.252 port=5432 user=repluser  password=repluser application_name=standby2'

启动新备库12.151
[postgres@pgtest01 data]$ pg_ctl -D /pgtina/data start
server starting

12.252上操作,添加12.251-新从节点信息
[postgres@pgtest02 bin]$ /pgpool/bin/pcp_attach_node -d 5 pgtest01 9898 pgpool pgpool 1   --这里用户密码错了postgres和密码123456由之前pcp.conf中设置所得。
Format:
pcp_attach_node  _timeout_  _host_  _port_  _userid_  _passwd_  _nodeid_

把给定的节点加入到 pgpool-II。
DEBUG: send: tos="R", len=44
DEBUG: recv: tos="r", len=63, data=AuthenticationFailed
DEBUG: authentication failed. reason=AuthenticationFailed
AuthorizationError

认证失败: 用户和密码,还有端口id都不对,要注意看show pool_nodes和pcp.conf中设置的用户密码。
[postgres@pgtest02 bin]$ /pgpool/bin/pcp_attach_node -d 5 pgtest01 9898 postgres test12 0
DEBUG: send: tos="R", len=46
DEBUG: recv: tos="r", len=21, data=AuthenticationOK
DEBUG: send: tos="D", len=6
DEBUG: recv: tos="c", len=20, data=CommandComplete
DEBUG: send: tos="X", len=4

查看状态,12.251变成了1,显然还是有问题:--重新连接进去就ok了。
postgres=# show pool_nodes;
node_id |    hostname    | port | status | lb_weight |  role  
---------+----------------+------+--------+-----------+---------
0       | 192.168.12.251 | 5432 | 2      | 0.500000  | standby
1       | 192.168.12.252 | 5432 | 2      | 0.500000  | primary
(2 rows)

但是在12.252这边看又正常了。
postgres=# show pool_nodes;   ------12.252显示251是主库,但12.251显示252是主库呀!!!,检查问题!
node_id |    hostname    | port | status | lb_weight |  role  
---------+----------------+------+--------+-----------+---------
0       | 192.168.12.251 | 5432 | 2      | 0.500000  | primary
1       | 192.168.12.252 | 5432 | 2      | 0.500000  | standby
(2 rows)

12.251的日志报错:连不上主库
FATAL:  could not connect to the primary server: FATAL:  no pg_hba.conf entry for replication connection from host "192.168.12.251", user "repluser"

原来12.252的pg_hba.conf文件中没设置权限:
host    replication    repluser         192.168.12.251/32       md5


把12.252的pgpool重启一次,加上-D参数
发现显示终于正常了:
postgres=# show pool_nodes;
node_id |    hostname    | port | status | lb_weight |  role  
---------+----------------+------+--------+-----------+---------
0       | 192.168.12.251 | 5432 | 2      | 0.500000  | standby
1       | 192.168.12.252 | 5432 | 2      | 0.500000  | primary
(2 rows)
备注:切换成功!!!!!


----------------------------------切换步骤总结--------------------------
都是在同一台机器上的操作:

1、确保触发文件被注释掉
#trigger_file

2、关闭主库db
pg_ctl -m fast stop
pg_controldata |grep cluster  --查看状态(主 shut down ,从 in production--变成了新主)
psql -h 192.168.12.240 -p 9999 -U pgpool pgpool
show pool_nodes;
node_id |    hostname    | port | status | lb_weight |  role  
---------+----------------+------+--------+-----------+---------
0       | 192.168.12.251 | 5432 | 2      | 0.500000  | primary  --新主
1       | 192.168.12.252 | 5432 | 3      | 0.500000  | standby   --新从
(2 rows)


3、将新从添加到pgpool
mv recovery.done recovery.conf   --修改文件名
pg_ctl -D /pgtina/data start    --启动新从库
/pgpool/bin/pcp_attach_node -d 5 pgtest02 9898 postgres test12 1  --注意最后的nodeid和主机名都要对应修改(0,1)
/pgpool/bin/pcp_attach_node -d 5 pgtest01 9898 postgres test12 0

正常情况下,这样就基本切换成功。vip已经可以自己漂移了

4、如果pgpool状态不正常,那么一定要重启一下pgpool
/pgpool/bin/pgpool -m fast stop
/pgpool/bin/pgpool -n -d -D> /tmp/pgpool.log 2>&1 &


[postgres@pgtest02 ~]$ The authenticity of host '192.168.12.252 (192.168.12.252)' can't be established.  --切换总是会遇到这个报错:
RSA key fingerprint is ad:09:f3:ce:a7:95:c1:e6:39:20:cb:4c:92:13:c0:d2.
Are you sure you want to continue connecting (yes/no)? ------一定要保证root和pg用户都能ssh到自身和对方主机


-----------------------------------------------------------------------
2 关pgpool master,测试vip的漂移

注意:此时12.252是主库,12.251是从库,我们去看看之前pgadmin的那个连接,是否能读写,看连的是主是从,设置的192.168.12.240---5432
insert into t1 values(999,00,00)   ---报错:ERROR: cannot execute INSERT in a read-only transaction   --说明没有自动漂移到12.252上面


说明:1、主从的切换,是停主库pg_ctl -D /pgtina/data stop,pgpool会把从库自动变成主库,再对主库做pcp_attach_node。
      2、vip的漂移,是将新从库上的pgpool关闭重启,使它漂移到新主库上去。
     
[postgres@pgtest01 pg_log]$ /pgpool/bin/pgpool -m fast stop

从12.29连接过来,发现两个端口都可以连接到数据库操作。
[root@antiywh-5NMQMH1 ~]# psql -h 192.168.12.240 -U postgres postgres -p 9999  --使用这个端口连接的是pgpool控制台,使用5432就会连接到数据库操作
tina=# insert into t1 values (9,10,10);
INSERT 0 1

[postgres@pgtest01 data]$ grep ^[a-z] pg_hba.conf
local   all             all                                     trust
host    all             all             127.0.0.1/32            trust
host    all             all             ::1/128                 trust
host    all             all             192.168.12.252/24       trust   ---限定192.168.12开头的都可以连接进来,要让192.168.12.240可以连接才行。
host    all             all             0.0.0.0/0               md5
host    all             all             192.168.10.100/32       md5
host    replication     repluser        192.168.12.252/32        md5

pgpool VIP :192.168.12.240  从12.251飘到12.252,注意此时仅 pgpool 的 VIP 飘移, db1,db2 上的数据库角色不变, pgpool 的切换非常容易,直接关闭新备库的pgpool


[postgres@pgtest02 data]$ psql -h 192.168.12.240 -p 9999 -U postgres postgres
psql: ERROR:  unable to read message length
DETAIL:  message length (12) in slot 1 does not match with slot 0(8)    ---因为ip被限制了,连接不上。

3  db1 掉电 power off
这里通过 vmwaer 执行 power off 模拟断电的情况, 在 db1(现在的 Primary 节点) 执行  power off ,发现 pgpool  VIP 能切换到 db1 ,同时 db1 上的数据库切换成 primary ,切换成功。

    第二种,第三种情况就不贴详细日志了。
   
   
===========================用户密码认证登陆测试===================
psql: ERROR:  MD5 authentication is unsupported in replication, master-slave and parallel modes.   ---需要对12.252完全开放
HINT:  check pg_hba.conf

注意以下三个文件:
/pgtina/data/pg_hba.conf
/pgpool/etc/pool_hba.conf
/pgpool/etc/pcp.conf
/pgpool/etc/pool_passwd
[postgres@pgtest02 data]$ grep ^[a-z] pg_hba.conf
local   all             all                                     trust  ---本地不要使用md5,因为需要进行cron的一些任务
host    all             all             127.0.0.1/32            trust
host    all             all             ::1/128                 trust
host    all             all             0.0.0.0/0               md5 --全网都必须通过md5认证连进db
host    replication    repluser         192.168.12.251/32       md5

[postgres@pgtest02 etc]$ grep ^[a-z] pool_hba.conf
local   all         all                               trust
host    all         all         127.0.0.1/32          trust
host    all         all         ::1/128               trust
host    all         all         0.0.0.0/0             md5   ----全网都必须通过md5认证连进pgpool

[postgres@pgtest02 etc]$ grep ^[a-z] pcp.conf   ----/pgpool/bin/pg_md5 -u postgres -p
postgres:60474c9c10d7142b7508ce7a50acf414
sqluser:d4f1362367d199a64af12b86efee4a09
fenxi:e020da2d9ffa07b8cbaef98663b48fc6
pgpool:fa039bd52c3b2090d86b0904021a5e33

[postgres@pgtest01 etc]$ /pgpool/bin/pg_md5 -m -u postgres -p   ---输入数据库密码
password:
[postgres@pgtest01 etc]$ tail pool_passwd   --自动写入的
postgres:md503416eb55cdf9cd532c638f12c1918ea  

---终于成功使用密码登陆,无密码拒绝访问192.168.12.240:9999  或者251:9999   252:9999
---无密码拒绝访问251:5432   252:5432   但是12.240:5432却可以无密码连接进来!!!(不知道是不是因为5432是db的端口,9999是pgpool的端口

添加一些其他用户:sqluser  fenxi  pgpool
/pgpool/bin/pg_md5 -m -u fenxi -p
/pgpool/bin/pg_md5 -m -u sqluser -p
=========================================================================================================================================
su postgres
createuser -p 5432 pgpool          //在本地的postgresql数据库创建pgpool用户
tina=# create user pgpool login encrypted password 'pgpool123' NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION;
createdb -p 5432 -O pgpool pgpool  //在本地的postgresql数据库创建pgpool数据库,所有者为pgpool

[postgres@pgtest02 etc]$ createdb -p 5432 -O pgpool pgpool
[postgres@pgtest02 etc]$ psql -f /pgpool/share/pgpool-II/system_db.sql -U pgpool pgpool
psql:/pgpool/share/pgpool-II/system_db.sql:3: ERROR:  schema "pgpool_catalog" does not exist
psql:/pgpool/share/pgpool-II/system_db.sql:4: ERROR:  schema "pgpool_catalog" does not exist
psql:/pgpool/share/pgpool-II/system_db.sql:5: ERROR:  schema "pgpool_catalog" does not exist
psql:/pgpool/share/pgpool-II/system_db.sql:6: ERROR:  schema "pgpool_catalog" does not exist
CREATE SCHEMA
CREATE TABLE
CREATE TABLE
CREATE TABLE

pgpool=# select tablename from pg_tables where schemaname='pgpool_catalog';
   tablename  
---------------
replicate_def
dist_def
query_cache

///////////////////////////////////////////////////////
///////////////////////////////////////////////////////

使用触发文件进行主从切换-----可能这种方式不太严谨,而且也不利于来回切换:
主库故障后,备库切换成主库的触发文件如下:
[root@pgtest6 pgpool-II-pg93]# more failover_stream.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;

因此,在pg的postgresql.conf中要贺pgpool参数文件的定义( /postgres/data/trigger.file)一致

   
不指定-h ,只指定-p 9999 就可以连上主库,而不是本地,指定了5432,那就连到本地只读库了。 
[postgres@pgtest01 ~]$ psql -p 5432 -U postgres -d postgres
psql (9.4.5)
Type "help" for help.

postgres=# \c tina
You are now connected to database "tina" as user "postgres".
tina=# insert into t1 values(100,100,100);
ERROR:  cannot execute INSERT in a read-only transaction   
   
   
   
   
//////////////////////////////////////////部分参数含义////////////////////////////////////////////
我们需要设置用于 pgpool-II 的后台 PostgreSQL 服务器了。这些服务器可以与 pgpool-II 在同一台主机上,也可以在独立的主机上。
如果你决定将所有服务器都放在同一台主机上,必须为每个服务分配不同的端口。如果服务器被安置在不同的机器上,他 们必须被正确以便可以通过网络接受 pgpool-II 的连接。
在本教程中,我们将三台服务器放在与 pgpool-II 相同的机器上,分别给它们分配端口号 5432,5433,5434。要配置 pgpool-II,请如下编辑 pgpool.conf。
backend_hostname0 = 'localhost'
backend_port0 = 5432
backend_weight0 = 1
backend_hostname1 = 'localhost'
backend_port1 = 5433
backend_weight1 = 1
backend_hostname2 = 'localhost'
backend_port2 = 5434
backend_weight2 = 1
分别为 backend_hostname,backend_port,backend_weight 设置节点的主机名,端口号和负载均衡系数。在每个参数串的后面,
必须通过添加从0开始(例如 0,1,2,…)的整数来指出节点编号。
backend_weight 参数都为 1 ,这意味着 SELECT 查询被平均分配到三台服务器上。


$ pgpool -n 2>&1 | /usr/local/apache2/bin/rotatelogs \ -l -f /var/log/pgpool/pgpool.log.%A 86400 &
这将生成名称类似于 “pgpool.log.Thursday” 的日志文件,然后在每天午夜 00:00 轮换日志文件。如果日志文件已经存在,
rotatelogs 将追加日志到这个文件中。如果想在轮换前删除旧日志文件,你可以使用 cron:
55 23 * * * /usr/bin/find /var/log/pgpool -type f -mtime +5 -exec /bin/rm -f '{}' \;



--------------------------
pgpool-II 通过 SHOW 命令提供一些信息。SHOW 是一个真实的 SQL 语句, 但是如果该命令查询 pgpool-II 信息的话,pgpool-II 解释了该命令。可选项如下:

pool_status, 获取配置
pool_nodes, 获取节点信息
pool_processes, 获取pgPool-II 进程信息
pool_pools, 获取pgPool-II 所有的连接池信息
pool_version, 获取pgPool_II 版本信息
注意:术语 'pool' 指的是一个 pgpool 进程所拥有的 PostgreSQL 会话池,并非指所有的 pgpool 所拥有的会话。

SQL语句中的 "pool_status" 在以前的版本中已经存在,但是其它可选项在 3.0 中才出现。




-----------------------------------------
pgpool的日志---增长太快,需要轮换,以便删除

APACHE的安装
软件:httpd-2.0.64.tar.bz2                 系统:RHEL5.5
apache现在分为两个版本1.x和2.x,下载地址:http://httpd.apache.org/
解压:
[root@localhost soft]# tar xvf httpd-2.0.64.tar.bz2
配置:
[root@localhost httpd-2.0.64]# ./configure --prefix=/opt/apache2   
/opt/apache2为安装目录
安装:
[root@localhost httpd-2.0.64]# make && make install
启动:
[root@localhost /]# /opt/apache2/bin/apachectl start
停止:
[root@localhost /]# /opt/apache2/bin/apachectl stop

如果你需要切换日志文件,可以将日志传递到一个支持日志轮换功能的外部命令。例如,你可以使用 Apache2 带的 rotatelogs 工具。
$ pgpool -n 2>&1 | /usr/local/apache2/bin/rotatelogs \ -l -f /var/log/pgpool/pgpool.log.%A 86400 &
-f 选项让 rotatelogs 在启动的时候生成一个日志文件,这个功能随 apache 2.2.9 或更高版本提供。
这将生成名称类似于 “pgpool.log.Thursday” 的日志文件,然后在每天午夜 00:00 轮换日志文件。如果日志文件已经存在,rotatelogs 将追加日志到这个文件中。

如果想在轮换前删除旧日志文件,你可以使用 cron:
55 23 * * * /usr/bin/find /var/log/pgpool -type f -mtime +5 -exec /bin/rm -f '{}' \;

apache中的cronolog 对你可能有用。
$ pgpool -n 2>&1 | /usr/sbin/cronolog \ --hardlink=/var/log/pgsql/pgpool.log \ '/var/log/pgsql/%Y-%m-%d-pgpool.log' &

可以新建一个专门存放poollog的目录,以便删除:(并不需要启动apache,只是利用这个自带工具而已) ---最新的pgpool启动命令
/pgpool/bin/pgpool -n -d 2>&1 | /opt/apache2/bin/rotatelogs -l /tmp/pgpool.log.%Y%m%d 86400 &

最后生成的日志格式:/tmp/pgpool.log.20151124


?转义符        描述
%a        简写的星期名
%A        全写的星期名
%b        简写的月名
%B        全写的月名
%c        日期与时间
%d        一月中的天,01-31
%H        时,00-23
%I        12小时时钟表示的小时,01-12
%j        一年中的天,001-366
%m        一年中的月,01-12
%M        分,00-59
%P        a.m或p.m
%S        秒,00-61
%u        星期中的天,1-7(星期1为1)
%U        一年中的星期,01-53(星期日为一个星期的第一天)
%V        一年中的星期,01-53(星期一为一个星期的第一天)
%w        一个星期中的天,0-6(星期日为0)
%x        本地格式日期
%X        本地格式时间
%y        小于1900的年号
%Y        年
%Z        时区名字
%%        A%字符
---------------------------

PCP 命令列表

PCP 命令是UNIX命令,通过网络操作pgpool-II。

* pcp_node_count        - 获取节点数量
* pcp_node_info         - 获取节点信息
* pcp_proc_count        - 获取进程列表
* pcp_proc_info         - 获取进程信息
* pcp_systemdb_info     - 获取System DB信息
* pcp_detach_node       - 从pgpool-II分离一个节点
* pcp_attach_node       - 给pgpool-II关联一个节点
* pcp_promote_node      - 给pgpool-II提升一个新的master节点
* pcp_stop_pgpool       - 停止 pgpool-II



其中一个点的参数设置:
listen_addresses = '*'
port = 9999
socket_dir = '/tmp'
listen_backlog_multiplier = 2
pcp_listen_addresses = '*'
pcp_port = 9898
pcp_socket_dir = '/tmp'
backend_hostname0 = '192.168.12.251'
backend_port0 = 5432
backend_weight0 = 1
backend_flag0 = 'ALLOW_TO_FAILOVER'
backend_hostname1 = '192.168.12.252'
backend_port1 = 5432
backend_weight1 = 1
backend_flag1 = 'ALLOW_TO_FAILOVER'
enable_pool_hba = on
pool_passwd = 'pool_passwd'
authentication_timeout = 60
ssl = off
num_init_children = 32
max_pool = 4
child_life_time = 300
child_max_connections = 0
connection_life_time = 0
client_idle_limit = 0
log_destination = 'syslog'
log_line_prefix = '%t: pid %p: '   # printf-style string to output at beginning of each log line.
log_connections = on
log_hostname = on
log_statement = on
log_per_node_statement = off
log_standby_delay = 'none'
syslog_facility = 'LOCAL0'
syslog_ident = 'pgpool'
debug_level = 0
pid_file_name = '/pgpool/pgpool.pid'
logdir = '/pgpool/log'
connection_cache = on
reset_query_list = 'ABORT; DISCARD ALL'
replication_mode = off
replicate_select = off
insert_lock = on
lobj_lock_table = ''
replication_stop_on_mismatch = off
failover_if_affected_tuples_mismatch = off
load_balance_mode = on
ignore_leading_white_space = on
white_function_list = ''
black_function_list = 'nextval,setval,nextval,setval'
database_redirect_preference_list = ''
app_name_redirect_preference_list = ''
allow_sql_comments = off
master_slave_mode = on
master_slave_sub_mode = 'stream'
sr_check_period = 5
sr_check_user = 'repluser'
sr_check_password = 'repluser'
delay_threshold = 16000
follow_master_command = ''
health_check_period = 5
health_check_timeout = 20
health_check_user = 'repluser'
health_check_password = 'repluser'
health_check_max_retries = 3
health_check_retry_delay = 1
connect_timeout = 10000
failover_command = '/pgpool/failover_stream.sh %H '
failback_command = ''
fail_over_on_backend_error = on
search_primary_node_timeout = 10
recovery_user = 'nobody'
recovery_password = ''
recovery_1st_stage_command = ''
recovery_2nd_stage_command = ''
recovery_timeout = 90
client_idle_limit_in_recovery = 0
use_watchdog = on
trusted_servers = ''
ping_path = '/bin'
wd_hostname = '192.168.12.251'
wd_port = 9000
wd_authkey = ''
delegate_IP = '192.168.12.240'
ifconfig_path = '/sbin'
if_up_cmd = 'ifconfig eth0:0 inet $_IP_$ netmask 255.255.255.0'
if_down_cmd = 'ifconfig eth0:0 down'
arping_path = '/usr/sbin'           # arping command path
arping_cmd = 'arping -U $_IP_$ -w 1'
clear_memqcache_on_escalation = on
wd_escalation_command = ''
wd_lifecheck_method = 'heartbeat'
wd_interval = 10
wd_heartbeat_port = 9694
wd_heartbeat_keepalive = 2
wd_heartbeat_deadtime = 30
heartbeat_destination0 = '192.168.12.252'
heartbeat_destination_port0 = 9694
heartbeat_device0 = 'eth0'
wd_life_point = 3
wd_lifecheck_query = 'SELECT 1'
wd_lifecheck_dbname = 'template1'
wd_lifecheck_user = 'nobody'
wd_lifecheck_password = ''
other_pgpool_hostname0 = '192.168.12.252'
other_pgpool_port0 = 9999
other_wd_port0 = 9000
relcache_expire = 0
relcache_size = 256
check_temp_table = on
check_unlogged_table = on
memory_cache_enabled = off
memqcache_method = 'shmem'
memqcache_memcached_host = 'localhost'
memqcache_memcached_port = 11211
memqcache_total_size = 67108864
memqcache_max_num_cache = 1000000
memqcache_expire = 0
memqcache_auto_cache_invalidation = on
memqcache_maxcache = 409600
memqcache_cache_block_size = 1048576
memqcache_oiddir = '/var/log/pgpool/oiddir'
white_memqcache_table_list = ''
black_memqcache_table_list = ''
parallel_mode = off
pgpool2_hostname = ''
system_db_hostname  = 'localhost'
system_db_port = 5432
system_db_dbname = 'pgpool'
system_db_schema = 'pgpool_catalog'
system_db_user = 'pgpool'
system_db_password = 'pgpool'

做了一个pgbouncer和pgpool的测试,过程省略

测试总结:
1)Pgbouncer的优点:  
pgbouncer非常简单,也易于修改,当不需要使用的时候,直接停下pgbouncer,
修改pg的pg_hba.conf 允许所有主机通过md5方式连接db即可。
Host  all    all   0.0.0.0/0  md5
几乎对db无任何影响,它独立存在,安装使用时也不需要进行停库等操作,只需要在安装好之后,通知大家修改端口。
pgbouncer对性能的损耗也相对较小。

2)Pgbouncer的缺点:
Pgbouncer的功能单一,只有连接池一个功能,不能负载均衡。

3)pgpool的优点:
HA,可以在主节点挂掉的情况下,自动迁移,用户无感知;负载均衡,可以减轻主节点的压力,连接池,可以避免连接数过多造成db hang住,复制和并行暂不介绍

4)pgpool的缺点:
性能损耗严重,连接池的功能不如pgbouncer,不算稳定,出问题的概率比较大

猜你喜欢

转载自906179271.iteye.com/blog/2263226