官方文档:https://www.pgpool.net/docs/latest/en/html/index.html
pgpool-II 是一个位于 PostgreSQL 服务器和 PostgreSQL 数据库客户端之间的中间件
·提供以下功能:
▼连接池
▼连接限制
▼缓存
▼复制
▼负载均衡
▼看门狗
·Pgpool对服务器和应用来说几乎是透明的,现有的数据库应用程序基本上可以不需要更改就可以使用pgpool
pgpool工作进程
Pgpool的服务进程。
Pcp进程:向pgpool发送管理命令的
父进程:负责检查数据库健康
Pgpool子进程:接受 发送sql
Work进程:检查延迟情况。
Pgpool logger:记录日志
Health Check:检查pg存活状态进程
01.安装pgpool-II
cd /postgresql/soft
tar zxvf pgpool-II-4.1.1.tar.gz
cd /postgresql/soft/pgpool-II-4.1.1
./configure --prefix=/postgresql/pgpool --with-pgsql=/postgresql/pg12
make
make install
配置环境变量
vi ~/.bash_profile
/postgresql/pgpool/bin
02.配置pgpool
配置pgpool
cd /postgresql/pgpool/etc
cp pcp.conf.sample pcp.conf
cp pgpool.conf.sample pgpool.conf
cp pool_hba.conf.sample pool_hba.conf
vim pgpool.conf
listen_addresses= '*'
backend_hostname0 = '10.100.2.250'
backend_port0 = 5432
backend_weight0 = 1
backend_data_directory0 = '/postgresql/pgdata'
backend_flag0 = 'ALLOW_TO_FAILOVER'
backend_application_name0 = 'pg3'
backend_hostname1 = '10.100.2.57'
backend_port1 = 5432
backend_weight1 = 1
backend_data_directory1 = '/postgresql/pgdata'
backend_flag1 = 'ALLOW_TO_FAILOVER'
backend_application_name1 = 'pg2'
backend_hostname2 = '10.100.2.31'
backend_port2 = 5432
backend_weight2 = 1
backend_data_directory2 = '/postgresql/pgdata'
backend_flag2 = 'ALLOW_TO_FAILOVER'
backend_application_name2 = 'pg1'
enable_pool_hba = on
num_init_children = 128
log_destination = 'syslog'
log_connections = on
pid_file_name = '/postgresql/pgpool/pgpool.pid'
logdir = '/tmp'
load_balance_mode = on
master_slave_mode = on
03.配置pool_passwd密码文件
在主库创建健康检查用户(与pgpool.conf一样)
psql -h 127.0.0.1 -U postgres
create role nobody login encrypted password 'nobody123';
cd /postgresql/pgpool/etc
pool_passwd:
pg_md5 --md5auth --username=nobody "nobody123"
pg_md5 --md5auth --username=pgpool "pgpool123"
[pgsql@pg3:/postgresql/pgpool/etc]$more pool_passwd
nobody:md5b37c69bb745cf4a35ce4ce16a6f6d56f
pgpool:md541e61037ae2fa9abc8d79bcfc287f609
网络配置
cd /postgresql/pgpool/etc
pool_hba.conf
echo "host all all 0.0.0.0/0 md5" >> /postgresql/pgpool/etc/pool_hba.conf
在主从库配置hba.conf
/postgresql/pgdata/hba.conf
echo "host all nobody 0.0.0.0/0 md5" >> /postgresql/pgdata/hba.conf
加载
pg_ctl reload
pcp管理文件密码(管理pgpool的用户和密码,非数据库用户)
cd /postgresql/pgpool/etc
pg_md5 pgpool123
vi pcp.conf
pgpool:fa039bd52c3b2090d86b0904021a5e33
04.启动pgpool
pgpool -n -d > pgpool.log 2>&1 &
pgpool -m fast stop
pgpool reload
psql -U nobody -h 10.100.2.250 -p 9999
show pool_nodes;
postgres=> show pool_nodes;
node_id | hostname | port | status | lb_weight | role | select_cnt | load_balance_node | replication_delay | replication_state | replicatio
n_sync_state | last_status_change
---------+--------------+------+--------+-----------+---------+------------+-------------------+-------------------+-------------------+-----------
-------------+---------------------
0 | 10.100.2.250 | 5432 | up | 0.333333 | primary | 0 | true | 0 | |
| 2023-02-28 14:25:22
1 | 10.100.2.57 | 5433 | down | 0.333333 | standby | 0 | false | 0 | |
| 2023-02-28 14:26:11
2 | 10.100.2.31 | 5433 | down | 0.333333 | standby | 0 | false | 0 | |
| 2023-02-28 14:26:29
重启,加-C,刷新
pgpool -m fast stop
pgpool -C -D
pgpool -n -d > pgpool.log 2>&1 &
postgres=> show pool_nodes;
LOG: statement: show pool_nodes;
LOG: DB node id: 0 backend pid: 16048 statement: SELECT version()
LOG: pool_reuse_block: blockid: 0
node_id | hostname | port | status | lb_weight | role | select_cnt | load_balance_node | replication_delay | replication_state | replicatio
n_sync_state | last_status_change
---------+--------------+------+--------+-----------+---------+------------+-------------------+-------------------+-------------------+-----------
-------------+---------------------
0 | 10.100.2.250 | 5432 | up | 0.333333 | primary | 0 | true | 0 | |
| 2023-02-28 15:21:15
1 | 10.100.2.57 | 5432 | up | 0.333333 | standby | 0 | false | 0 | |
| 2023-02-28 15:21:15
2 | 10.100.2.31 | 5432 | up | 0.333333 | standby | 0 | false | 0 | |
| 2023-02-28 15:21:15
(3 rows)
测试读写分离