pgpool-II 源码下载地址: https://pgpool.net/mediawiki/index.php/Downloads https://www.cnblogs.com/yickel/p/11161915.html
https://blog.csdn.net/silenceray/article/details/53760916
编译依赖 zlib* readline*
1介绍 pgpool-II 是一个位于 PostgreSQL 服务器和 PostgreSQL 数据库客户端之间的中间件,它提供以下功能:
连接池 pgpool-II 保持已经连接到 PostgreSQL 服务器的连接, 并在使用相同参数(例如:用户名,数据库,协议版本) 连接进来时重用它们。 它减少了连接开销,并增加了系统的总体吞吐量。
复制 pgpool-II 可以管理多个 PostgreSQL 服务器。 激活复制功能并使在2台或者更多 PostgreSQL 节点中建立一个实时备份成为可能, 这样,如果其中一台节点失效,服务可以不被中断继续运行。
负载均衡 如果数据库进行了复制(可能运行在复制模式或者主备模式下), 则在任何一台服务器中执行一个 SELECT 查询将返回相同的结果。 pgpool-II 利用了复制的功能以降低每台 PostgreSQL 服务器的负载。 它通过分发 SELECT 查询到所有可用的服务器中,增强了系统的整体吞吐量。 在理想的情况下,读性能应该和 PostgreSQL 服务器的数量成正比。 负载均很功能在有大量用户同时执行很多只读查询的场景中工作的效果最好。
限制超过限度的连接 PostgreSQL 会限制当前的最大连接数,当到达这个数量时,新的连接将被拒绝。 增加这个最大连接数会增加资源消耗并且对系统的全局性能有一定的负面影响。 pgpoo-II 也支持限制最大连接数,但它的做法是将连接放入队列,而不是立即返回一个错误。
pgpool-II 使用 PostgreSQL 的前后台程序之间的协议,并且在前后台之间传递消息。 因此,一个(前端的)数据库应用程序认为 pgpool-II 就是实际的 PostgreSQL 数据库, 而后端的服务进程则认为 pgpool-II 是它的一个客户端。 因为 pgpool-II 对于服务器和客户端来说是透明的, 现有的数据库应用程序基本上可以不需要修改就可以使用 pgpool-II 了。
2安装pgpool-II 2.1编译安装 官方文档:
https://www.pgpool.net/docs/pgpool-II-3.5.4/doc/pgpool-zh_cn.html#install
./configure 选项 –prefix=path pgpool-II 的二进制程序和文档将被安装到这个目录。默认值为 /usr/local –with-pgsql=path PostgreSQL 的客户端库安装的顶层目录。默认值由 pg_config 提供 –with-openssl pgpool-II 程序将提供 OpenSSL 支持。默认是禁用 OpenSSL 支持的。 –enable-sequence-lock 在 pgpool-II 3.0 系列中使用 insert_lock 兼容。pgpool-II 针对序列表中的一行进行加锁。PostgreSQL 8.2 或2011年六月以后发布的版本无法使用这种加锁方法。 –enable-table-lock
在 pgpool-II 2.2 和 2.3 系列中使用 insert_lock 兼容。pgpool-II 针对被插入的表进行加锁。这种锁因为和 VACUUM 冲突,已被废弃。 –with-memcached=path pgpool-II 的二进制程序将使用 memcached 作为 基于内存的查询缓存。你必须先安装 libmemcached。 2.2环境准备 2.2.1 免密登录 配置密钥使master和slave1这两台虚拟机的postgres用户能免密连接
Master与slave之间的免密码登陆:
#在master上切换至postgres用户,生成密钥 su postgres ssh-keygen -t rsa #然后全输入回车 #切换到postgres用户: su postgres ssh-copy-id -i /var/lib/pgsql/.ssh/id_rsa 192.168.200.91 #然后ssh 192.168.200.91 成功,实现master到slave的免密码登陆。 #同理,完成对slave到mastere的免密登录。 2.2.2 安装postgrsql 安装好postgresql,确保能正常使用,并且可以通过网络连接。
2.3 安装pgpool-II
1、源码安装 [root@localhost ~]# tar xvf pgpool-II-3.3.3.tar.gz [root@localhost ~]#mkdir /opt/pgpool [root@localhost ~]#chown -R postgres:postgres /opt/* [postgres@localhost~]$ cd /opt/pgpool-II-3.3.3 [postgres@localhost pgpool-II-3.3.3] . / c o n f i g u r e − − p r e f i x = / o p t / p g p o o l [ p o s t g r e s @ l o c a l h o s t p g p o o l − I I − 3.3.3 ] ./configure --prefix=/opt/pgpool [postgres@localhost pgpool-II-3.3.3] ./configure−−prefix=/opt/pgpool[postgres@localhostpgpool−II−3.3.3]make [postgres@localhost pgpool-II-3.3.3]$make install
2、安装pgpool_regclass 和pgpool-recovery [postgres@localhost pgpool-II-3.3.3]$ cd /pgpool-II-3.3.3/sql/pgpool_regclass [postgres@localhost pgpool_regclass]KaTeX parse error: Expected 'EOF', got '&' at position 6: make &̲& make install …psql -f pgpool-regclass.sql template1 [postgres@localhost pgpool_regclass]$cd /pgpool-II-3.3.3/sql/pgpool-recovery [postgres@localhost pgpool-recovery] KaTeX parse error: Expected 'EOF', got '&' at position 6: make &̲& make install …psql -f pgpool-recovery.sql template1 3、建立inert_lock表 [postgres@localhost pgpool-recovery] c d / p g p o o l − I I − 3.3.3 / s q l / [ p o s t g r e s @ l o c a l h o s t s q l ] cd /pgpool-II-3.3.3/sql/ [postgres@localhost sql] cd/pgpool−II−3.3.3/sql/[postgres@localhostsql]psql -f insert_lock.sql template1
4、配置即使用 查看配置文件 [postgres@localhost pgpool-II-3.3.3]$ cd /opt/pgpool/etc/ [postgres@localhost etc]$ ll total 180 -rw-r–r--. 1 postgres postgres 858 Dec 19 13:28 pcp.conf.sample -rw-r–r--. 1 postgres postgres 30979 Dec 19 13:28 pgpool.conf.sample -rw-r–r--. 1 postgres postgres 30669 Dec 19 13:28 pgpool.conf.sample-master-slave -rw-r–r--. 1 postgres postgres 30651 Dec 19 13:28 pgpool.conf.sample-replication -rw-r–r--. 1 postgres postgres 30690 Dec 19 13:28 pgpool.conf.sample-stream -rw-r–r--. 1 postgres postgres 3200 Dec 19 13:28 pool_hba.conf.sample -rw-rw-r–. 1 postgres postgres 43 Dec 19 13:53 pool_passwd [postgres@localhost etc]$ cp pgpool.conf.sample pgpool.conf [postgres@localhost etc]$ cp pcp.conf.sample pcp.conf [postgres@localhost etc]$ pool_hba.conf.sample pool_hba.conf pg_pool默认只接受端口的本地连接。如果想从其他主机接受连接,请设置listen_address=’’ ,相对应在pgpool.conf中配置修改如下: listen_address=’’ port =9999 因为不是安装在默认的“/usr/local/”目录下的,所以要指定目录的配置项, pid_file_name =’/opt/pgpool/run/pgpool.pid’ 创建相对应目录的命令 [postgres@localhost etc]KaTeX parse error: Expected 'EOF', got '#' at position 43: …据库,pgpool.conf #̲- Backend Conne…pg_md5 -m -p -u postgres pool_passwd passwd: 这样就生成了pool_passwd文件,使用cat查看文件内容: postgres:md5279d84d3239474da07235e5a6555c73b 启动pgpool [postgres@localhost etc] p g p o o l 如 果 想 让 p g p o o l 在 前 台 运 行 , 可 以 加 “ − n ” 参 数 [ p o s t g r e s @ l o c a l h o s t e t c ] pgpool 如果想让pgpool在前台运行,可以加“-n”参数 [postgres@localhost etc] pgpool如果想让pgpool在前台运行,可以加“−n”参数[postgres@localhostetc]pgpool -n 如果想让日志打印到一个文件。使用一下命令 [postgres@localhost etc]KaTeX parse error: Expected 'EOF', got '&' at position 30: …p/pgpool.log 2>&̲1 & 如果想打印调度信息,加…pgpool -n -d > /tmp/pgpool.log 2>&1 & 停止pgpool [postgres@localhost etc] p g p o o l s t o p 也 可 以 加 上 参 数 [ p o s t g r e s @ l o c a l h o s t e t c ] pgpool stop 也可以加上参数 [postgres@localhost etc] pgpoolstop也可以加上参数[postgres@localhostetc]pgpool -m fast stop
5、复制和负载均衡的示例 在pgpool.conf中配置以下内容: replication_mode = true load_balance_mode = true
backend_hostname0 = ‘192.168.200.90’ backend_port0 = 5432 backend_weight0 = 1 backend_data_directory0 = ‘/opt/pgsql/data’ backend_flag0 = ‘ALLOW_TO_FAILOVER’ backend_hostname1 = ‘192.168.200.91’ backend_port1 = 5432 backend_weight1 = 1 backend_data_directory1 = ‘/opt/pgsql/data’ backend_flag1 = ‘ALLOW_TO_FAILOVER’
启动pgpool pgpool -f /opt/pgpool/etc/pgpool.conf
在data1上连接pgpool的端口9999,然后创建一张表。并插入数据 [postgres@localhost etc]$ psql -h 192.168.200.90 -d postgres -p 9999 -U postgres psql (9.4.4) Type “help” for help.
postgres=# \d List of relations Schema | Name | Type | Owner ----------------±-----±-----±------- oracle_catalog | dual | view | postgres (1 row)
postgres=# create table aaa (a int); CREATE TABLE highgo=# \d List of relations Schema | Name | Type | Owner ----------------±-----±------±------- oracle_catalog | dual | view | postgres public | aaa | table | postgres (2 rows)
1 2 3 (3 rows) 在data2上可以看到,表和数据都弄好了,而且数据完全一样 [postgres@localhost data]$ psql Password: psql (9.4.4) Type “help” for help.
postgres=# \d List of relations Schema | Name | Type | Owner ----------------±-----±------±------- oracle_catalog | dual | view | postgres public | aaa | table | postgres (2 rows)
1 2 3 (3 rows)
pgpool有四个主要的配置文件,分别是
pcp.conf 用于管理查、看节点信息,如加入新节点。该文件主要是存储用户名及md5形式的密码。 pgpool.conf 用于设置pgpool的模式,主次数据库的相关信息等。 pool_hba.conf 用于认证用户登录方式,如客户端IP限制等,类似于postgresql的pg_hba.conf文件。 pool_passwd 用于保存相应客户端登录帐号名及md5密码。 1、 配置pgpool.conf
listen_addresses = ‘*’ # rtm用于pgpool监听地址,控制哪些地址可以通过pgpool 连接,*表示接受所有连接
port = 9999 # rtm pgpool 监听的端口
pcp_listen_addresses = ‘*’ # rtm
pcp_port = 9898 # rtm
backend_hostname0 = ‘10.10.10.1’ # rtm 配置后端postgreSQL 数据库地址,此处为主库
backend_port0 = 5432 # rtm 后端postgreSQL 数据库端口
backend_weight0 = 1 # rtm 权重,用于负载均衡
backend_data_directory0 = ‘/pgdata/ha/masterdata’ # rtm 后端postgreSQL 数据库实例目录
backend_flag0 = ‘ALLOW_TO_FAILOVER’ # rtm 允许故障自动切换
backend_hostname1 = ‘10.10.10.2’ # rtm 此处为备库1数据库地址
backend_port1 = 5432 # rtm
backend_weight1 = 1 # rtm
backend_data_directory1 = ‘/pgdata/ha/slavedata’ # rtm
backend_flag1 = ‘ALLOW_TO_FAILOVER’ # rtm
backend_hostname2 = ‘10.10.10.3’ # rtm 此处为备库2数据库地址
backend_port2 = 5432 # rtm
backend_weight2 = 1 # rtm
backend_data_directory2 = ‘/pgdata/ha/slavedata’ # rtm
backend_flag2 = ‘ALLOW_TO_FAILOVER’ # rtm
enable_pool_hba = on # rtm 开启pgpool认证,需要通过 pool_passwd 文件对连接到数据库的用户进行md5认证
pool_passwd = ‘pool_passwd’ # rtm 认证文件
log_destination = ‘stderr,syslog’ # rtm 日志级别,标注错误输出和系统日志级别
log_line_prefix = '%t: pid %p: ’ # rtm 日志输出格式
log_connections = on # rtm 开启日志
log_hostname = on # rtm 打印主机名称
#log_statement = all # rtm 取消注释则打印sql 语句
#log_per_node_statement = on # rtm 取消注释则开启打印sql负载均衡日志,记录sql负载到每个节点的执行情况
#client_min_messages = log # rtm 日志
#log_min_messages = info # rtm # 日志级别
pid_file_name = ‘/opt/pgpool-3/run/pgpool/pgpool.pid’ # rtm pgpool的运行目录,若不存在则先创建
logdir = ‘/opt/pgpool-3/log/pgpool’ # rtm 指定日志输出的目录
replication_mode = off # rtm 关闭pgpool的复制模式
load_balance_mode = on # rtm 开启负载均衡
master_slave_mode = on # rtm 开启主从模式
master_slave_sub_mode = ‘stream’ # rtm设置主从为流复制模式
sr_check_period = 10 # rtm 流复制的延迟检测的时间间隔
sr_check_user = ‘pgcheck’ # rtm Specifiy replication delay check user and password,该用户需要在pg数据库中存在,且拥有查询权限
sr_check_password = ‘123456’ # rtm Pgpool-II 4.0开始,如果这些参数为空,Pgpool-II将首先尝试从sr_check_password文件中获取指定用户的密码
sr_check_database = ‘postgres’ # rtm 流复制检查的数据库名称
delay_threshold = 10000000 # rtm 设置允许主备流复制最大延迟字节数,单位为kb。定义slave库能够接收读请求所允许的最大延迟时间。比如:设置为1024,slave库只允许滞后master库1KB 的XLOG;否则,slave库将不会接收到请求。
health_check_period = 10 # rtm pg数据库检查检查间隔时间。定义系统应该多久检查一次哪些XLOG位置,以弄清楚是否是延迟太高或太低。
health_check_timeout = 20 # rtm
health_check_user = ‘pgcheck’ # rtm 健康检查用户,需pg数据库中存在。连接到primary来检查当前XLOG的位置的用户名。
health_check_password = ‘123456’ # rtm 设置方法同sr_check_password
health_check_database = ‘postgres’ # rtm 健康检查的数据库名称
health_check_max_retries = 3 # rtm 健康检查最大重试次数
health_check_retry_delay = 3 # rtm 重试次数间隔
failover_command = ‘/etc/pgpool-II/failover.sh %d %h %p %D %m %H %M %P %r %R’ # rtm 在failover_command参数中指定failover后需要执行的failover.sh脚本
follow_master_command = ‘/etc/pgpool-II/follow_master.sh %d %h %p %D %m %M %H %P %r %R’ # rtm如果使用3台PostgreSQL服务器,需要在主节点切换后指定follow_master_command运行,如果是两PostgreSQL服务器,则不需要设置 follow_master_command。
fail_over_on_backend_error = off # rtm 如果设置了health_check_max_retries次数,则关闭该参数
use_watchdog = on # rtm 开启看门狗,用于监控pgpool 集群健康状态
wd_hostname = ‘10.10.10.1’ # rtm 本地看门狗地址,配置为当前库的IP
wd_port = 9000 # rtm
wd_priority = 1 # rtm 看门狗优先级,用于pgpool 集群中master选举
delegate_IP = ‘10.10.10.101’ # rtm 在三个库上指定接受客户端连接的虚拟IP地址。
if_up_cmd = ‘ip addr add KaTeX parse error: Expected group after '_' at position 4: _IP_̲/24 dev eth0 label eth0:0’ # rtm 配置虚拟IP到本地网卡
if_down_cmd = ‘ip addr del KaTeX parse error: Expected group after '_' at position 4: _IP_̲/24 dev eth0’ # rtm
wd_lifecheck_method = ‘heartbeat’ # rtm 看门狗健康检测方法
wd_heartbeat_port = 9694 # rtm 看门狗心跳端口,用于pgpool 集群健康状态通信
wd_heartbeat_keepalive = 2 # rtm 看门狗心跳检测间隔
wd_heartbeat_deadtime = 30 # rtm
heartbeat_destination0 = ‘10.10.10.2’ # rtm 配置需要监测健康心跳的IP地址,非本地地址,即互相监控,配置对端的IP地址
heartbeat_destination_port0 = 9694 # rtm 监听的端口
heartbeat_device0 = ‘eth0’ # rtm 监听的网卡名称
heartbeat_destination1 = ‘10.10.10.3’ # rtm 配置需要监测健康心跳的IP地址,非本地地址,即互相监控,配置对端的IP地址
heartbeat_destination_port1 = 9694 # rtm 监听的端口
heartbeat_device1 = ‘eth0’ # rtm
wd_life_point = 3 # rtm 生命检测失败后重试次数
wd_lifecheck_query = ‘SELECT 1’ # rtm 用于检查 pgpool-II 的查询语句。默认为“SELECT 1”。
wd_lifecheck_dbname = ‘postgres’ # rtm 检查健康状态的数据库名称
wd_lifecheck_user = ‘pgcheck’ # rtm 检查数据库的用户,该用户需要在Postgres数据库存在,且有查询权限
wd_lifecheck_password = ‘123456’ # rtm 看门狗健康检查用户密码
other_pgpool_hostname0 = ‘10.10.10.2’ # rtm 指定被监控的 pgpool-II 服务器的主机名
other_pgpool_port0 = 9999 # rtm 指定被监控的 pgpool-II 服务器的端口号
other_wd_port0 = 9000 # rtm 指定 pgpool-II 服务器上的需要被监控的看门狗的端口号
other_pgpool_hostname1 = ‘10.10.10.3’ # rtm 指定被监控的 pgpool-II 服务器的主机名
other_pgpool_port0 = 9999 # rtm 指定被监控的 pgpool-II 服务器的端口号
other_wd_port0 = 9000 # rtm 指定 pgpool-II 服务器上的需要被监控的看门狗的端口号
2、 配置pool_passwd,该文件用于配置哪些用户可以访问pgpool。使用如下命令生成:
pg_md5 -p -m -u postgres pool_passwd //执行后输入密码
通过如上命令,设置数据库的postgre用户名和密码,及pgpool进行健康检查的pgcheck用户名和密码。
3、 配置.pgpass。使用pgpool-II进行故障库自动切换(failover)、或在线恢复(online recovery)(在线恢复:主库故障后切换,原主库恢复后变更为备库。注意是Online recovery,而不是自动恢复,需要手工执行命令恢复),需要能够无密码SSH访问其他postgreSQL服务器。为了满足此条件,我们需要在每个postgreSQL服务器上,在postgres用户的home file下创建了.pgpass文件,并修改器文件权限为600。
[all servers]# su - postgres
[all servers]$ vi /var/lib/pgsql/.pgpass
10.10.10.1:5432:replication:repl:
10.10.10.2:5432:replication:repl:
10.10.10.3:5432:replication:repl:
[all servers]$ chmod 600 /var/lib/pgsql/.pgpass
4、 配置pcp.conf。由于pcp命令需要用户认证,该在文件中指定配置pcp命令的pgpool用户的用户名、密码。格式:
用户名:密码MD5编码
注:密码的MD5编码看通过“pg_md5” 密码命令生成5、 配置pcp的.pcppass。需要follow_master_command脚本情况下,由于此脚本必须在不输入密码的情况下执行pcp命令,所以我们在Pgpool-II用户(root用户)的home directory下创建.pcppass:
# echo ‘localhost:9898:pgpool:pgpool’ > ~/.pcppass
# chmod 600 ~/.pcppass
6、 配置pool_hba.conf认证文件,类似于PostgreSQL的pg_hba.conf文件。
步骤四:备库配置pgpool-II
类同主库配置pgpool-II步骤:
1、 配置pgpool.conf。类同主库pgpool-II配置。以备库1配置pgpool.conf为例:修改配置中主库、备库2的IP:
use_watchdog = on # rtm 开启看门狗,用于监控pgpool 集群健康状态
wd_hostname = ‘10.10.10.2’ # rtm 本地看门狗地址,配置为当前库地址
wd_port = 9000 # rtm
wd_priority = 1 # rtm 看门狗优先级,用于pgpool 集群中master选举
wd_lifecheck_method = ‘heartbeat’ # rtm 看门狗健康检测方法
wd_heartbeat_port = 9694 # rtm 看门狗心跳端口,用于pgpool 集群健康状态通信
wd_heartbeat_keepalive = 2 # rtm 看门狗心跳检测间隔
wd_heartbeat_deadtime = 30 # rtm
heartbeat_destination0 = ‘10.10.10.1’ # rtm 配置需要监测健康心跳的IP地址,非本地地址,即互相监控,配置对端的IP地址
heartbeat_destination_port0 = 9694 # rtm 监听的端口
heartbeat_device0 = ‘eth0’ # rtm 监听的网卡名称
heartbeat_destination1 = ‘10.10.10.3’ # rtm 配置需要监测健康心跳的IP地址,非本地地址,即互相监控,配置对端的IP地址
other_pgpool_hostname0 = ‘10.10.10.1’ # rtm 指定被监控的 pgpool-II 服务器的主机名
other_pgpool_port0 = 9999 # rtm 指定被监控的 pgpool-II 服务器的端口号
other_wd_port0 = 9000 # rtm 指定 pgpool-II 服务器上的需要被监控的看门狗的端口号
other_pgpool_hostname1 = ‘10.10.10.3’ # rtm 指定被监控的 pgpool-II 服务器的主机名
other_pgpool_port0 = 9999 # rtm 指定被监控的 pgpool-II 服务器的端口号
other_wd_port0 = 9000 # rtm 指定 pgpool-II 服务器上的需要被监控的看门狗的端口号
2、 其他部署类同主库配置。
步骤:启动pgpool-II
注:
启动pgpool-II前,必须先启动PostgreSQL;同理,停止PostgreSQL前,必须先停止pgpool-II. 上面步骤中,配置的三个库的优先级一致(backend_weight)。所以Pgpool-II节点角色取决于Pgpool-II启动的先后顺序 分别在主库、备库1、备库2执行如下命令启动pgpool-II: pgpool -n -d > pgpool.log 2>&1 &
1、 pgpool-II先启动的库,如下打印
显示角色是主库。看门狗向另外两个库发送心跳,也接收另外另个库的响应。
DEBUG: STATE MACHINE INVOKED WITH EVENT = STATE CHANGED Current State = MASTER
DEBUG: watchdog heartbeat: send heartbeat signal to 10.10.10.2:9694
DEBUG: watchdog heartbeat: send heartbeat signal to 10.10.10.3:9694
DEBUG: received heartbeat signal from 。。。。
2、 pgpool-II后启动的库,如下打印
显示角色是备库。看门狗向另外两个库发送心跳,也接收另外另个库的响应。
DEBUG: STATE MACHINE INVOKED WITH EVENT = PACKET RECEIVED Current State = STANDBY
DEBUG: watchdog heartbeat: send heartbeat signal to 10.10.10.1:9694
DEBUG: watchdog heartbeat: send heartbeat signal to 10.10.10.3:9694
DEBUG: received heartbeat signal from “10.10.10.1(10.10.10.1):9999” node:10.10.10.1:9999 Linux CLWDB3
DEBUG: received heartbeat signal from “10.10.10.3(10.10.10.3):9999” node:Not_Set
3、 查看VIP
在主库上执行:#ip addr,看到10.10.10.101虚拟IP绑定在主库的eth0上。
步骤六:查看pgpool集群状态
1、 通过pgpool命令查看pgpool中库状态(IP为pgpool的VIP)
执行:psql -h 10.10.56.87 -p 9999 -U postgres pgpool
其他命令:
查看pgpool配置:# show pool_status;
查看pgpool连接池:# show pool_pools;
2、 通过pcp管理pgpool。Pcp是管理pgpool的linux命令。
(1) 查看pgpool集群状态(IP为pgpool的VIP)
# pcp_watchdog_info -h 10.10.10.101 -p 9898 -U pgcheck -v
可以看到集群Node的IP、Port、状态,虚拟IP绑定的Node等信息。
(2) 查看pgpool集群Node数量(IP为pgpool的VIP)
# pcp_node_count -h 10.10.10.101 -p 9898 -U pgcheck -v
(3) 查看pgpool集群配置(IP为pgpool的VIP)
# pcp_pool_status -h 10.10.10.101 -p 9898 -U pgcheck -v
(4) 查看pgpool processer进程状态连接池(IP为pgpool的VIP)
#pcp_proc_count -h 10.10.10.101 -p 9898 -U pgcheck -v