当前位置: 首页 > 工具软件 > PGpool > 使用案例 >

pgpool-II PostgreSQL 负载均衡中间件

别开诚
2023-12-01

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言


一、pgpool-II是什么?

pgpool-II 是运行于 PostgreSQL 数据库服务器和客户端之间的一个中间件,提供的功能包括:连接池、复制、负载均衡、客户端限制和并行查询等。

二、环境

软件版本要求
postgresql14.7
pgpool-II4.4.2
CentOS7能访问互联网

安装postgresql 14.7

wget https://ftp.postgresql.org/pub/source/v14.7/postgresql-14.7.tar.bz2 --no-check-certificate #下载源码包

yum install readline-devel  #安装依赖库

tar xjvf postgresql*.bz2 #解压至一个目录

cd postgresql-14.7

./configure --prefix=/opt/pgsql #拟安装至/opt/pgsql

make world

make install-world

adduser postgres #增加新用户,系统提示要给定新用户密码(

mkdir /opt/pgsql/data #创建数据库目录

chown -R postgres:postgres /opt/pgsql/data

su - postgres #使用postgres帐号操作

/opt/pgsql/bin/initdb -D /opt/pgsql/data #初始化数据库

/opt/pgsql/bin/pg_ctl -D /opt/pgsql/data -l logfile start #启动数据库(在用户主目录)

/opt/pgsql/bin/createdb genericdb #假定数据库名为gerericdb)

/opt/pgsql/bin/psql genericdb # (进入数据库内部)

将访问端口开放给本机外的IP

配置文件目录:/opt/pgsql/data/
修改pg_hba.conf文件中关于ipv4的127.0.0.1/32 md5 改为0.0.0.0/0 trust
或者直接追加一条配置:

host    all             all             0.0.0.0/0               md5

修改postgresql.conf文件中关于listen_address中’localhost’为’*’

将密码编码改为用md5格式

alter system set password_encryption = 'md5';

select pg_reload_conf();

修改了配置后,需要reload并且重启方可生效:

postgres=# select pg_reload_conf();

/opt/pgsql/bin/pg_ctl -D /opt/pgsql/data stop

/opt/pgsql/bin/pg_ctl -D /opt/pgsql/data -l logfile start

设置postgres账户的密码

ALTER USER postgres WITH PASSWORD '123aA@';

安装pgpool

查找pgsql的socket文件目录(后面配置pgpool会用到)

/opt/pgsql/bin/psql -c “SHOW unix_socket_directories;”

安装pgpool

切换到root账号

wget https://www.pgpool.net/yum/rpms/4.4/redhat/rhel-7-x86_64/pgpool-II-pg14-4.4.2-1pgdg.rhel7.x86_64.rpm

rpn -ivh pgpool-II-pg14-4.4.2-1pgdg.rhel7.x86_64.rpm 或者 yum install pgpool-II-pg14-4.4.2-1pgdg.rhel7.x86_64.rpm

/etc/pgpool-II

配置pgpool.conf

配置pool_passwd

pg_md5 -u postgres -p # 查看postgres账户的密码md5编码

或者到数据库里查询密码:

postgres=# select passwd from pg_shadow where pg_shadow.usename = 'postgres'; 
               passwd                
-------------------------------------
 md5cc66260c9d14df84057fe629086ffffaa
(1 row)

写入到/etc/pgpool-II/pool_passwd文件,格式如下:

postgres:md5cc66260c9d14df84057fe62908fffffaa

配置/etc/pgpool-II/pgpool.conf

  35 listen_addresses = '*'
  
  43 unix_socket_directories = '/tmp'
  
  75 pcp_socket_dir = '/tmp

  88 # - Backend Connection Settings -
  
  90 backend_hostname0 = '192.168.1.20'
  92 backend_port0 = 5432
  94 backend_weight0 = 1
  96 backend_data_directory0 = '/data'
  98 backend_flag0 = 'ALLOW_TO_FAILOVER'
 102 backend_application_name0 = 'server0'

 104 backend_hostname1 = '192.168.1.21'
 105 backend_port1 = 5432
 106 backend_weight1 = 1
 107 backend_data_directory1 = '/data'
 108 backend_flag1 = 'ALLOW_TO_FAILOVER'
 109 backend_application_name1 = 'server1'
 110 
 111 # - Authentication -
 112 
 113 enable_pool_hba = on
 114                                    # Use pool_hba.conf for client authentication
 115 pool_passwd = 'pool_passwd'
 116                                    # File name of pool_passwd for md5 authentication.
 117                                    # "" disables pool_passwd.
 118                                    # (change requires restart)

启动pgpool

pgpool -n -d > /tmp/pgpool.log 2>&1 &

pgpool关机

pgpool stop

通过pgpool连接数据库

/opt/pgsql/bin/psql -p 9999 -U postgres -W

postgres=# show pool_nodes;
 node_id |  hostname   | port | status | pg_status | lb_weight |  role   | pg_role | select_cnt | load_balance_node | replication_delay | replication_state | replication_sync_state | las
t_status_change  
---------+-------------+------+--------+-----------+-----------+---------+---------+------------+-------------------+-------------------+-------------------+------------------------+----
-----------------
 0       | 192.168.1.20 | 5432 | up     | unknown   | 0.500000  | standby | unknown | 2          | true              | 0                 |                   |                        | 202
3-03-31 22:43:27
 1       | 192.168.1.21 | 5432 | unused | unknown   | 0.500000  | standby | unknown | 0          | false             | 0                 |                   |                        | 202
3-03-31 22:43:27
(2 rows)

总结

提示:这里对文章进行总结:
TODO continue;;

 类似资料: