HGDB-pgbouncer配置

本文档详细介绍了如何安装、配置及启动PGBouncer,一个轻量级的数据库连接池器,用于优化数据库连接管理。通过设置连接参数、配置用户密码文件、调整连接模式,实现高效管理数据库连接,降低数据库负载。
摘要由CSDN通过智能技术生成
实验步骤:
一.安装
1.下载pgbouncer的安装包
http://pgfoundry.org/frs/?group_id=1000258&release_id=1952,本次下载的安装包是pgbouncer-1.5.2.tar.gz


2.下载libevent包
http://monkey.org/~provos/libevent/,见pgbouncer源码包中的Readme
这玩意也是个好东西,在很多地方用到


3.安装libevent
$ cd libevent-2.0.19-stable
$ ./configure --prefix=/opt/pgbouncer/libevent
$ make
$ make install


加载libevent动态库
cd /etc/ld.so.conf.d/
vim libevent2.0.21.conf
/usr/local/libevent/lib
ldconfig


4.安装pgbouncer
$ cd pgbouncer-1.5.2
$ ./configure --prefix=/opt/pgbouncer/pgbouncer --with-libevent=/opt/pgbouncer/libevent/
$ make 
$ make install


 
配置运行环境变量
vim ~/.bash_profile
PATH=/opt/pgbouncer/pgbouncer/bin:$PATH
export PATH
执行生效
source ~/.bash_profile
查看pgbouncer是否安装成功,可以通过查看config.log中最后的返回值exit来确认,0是成功1是失败. 


二.配置 
1.配置pgbouncer的cfg文件
[highgo@sourcedb config]$ pwd
/opt/pgbouncer/pgbouncer/config        ----可以手动创建此目录和文件
$ more pgbouncer.ini        ----修改该文件内容后,需要重启pgbouncer后才生效 ----重启pgbouncer命令(pgbouncer -R -d pgbouncer.ini)
[databases]
aaa = host=127.0.0.1 port=5866 dbname=highgo user=highgo password=highgo123


[pgbouncer]
listen_port = 1999 
listen_addr = 127.0.0.1
auth_type = md5
auth_file = /opt/pgbouncer/pgbouncer/users.txt
logfile = /opt/pgbouncer/pgbouncer/pgbouncer.log     
pidfile = /opt/pgbouncer/pgbouncer/pgbouncer.pid
admin_users = highgo       
pool_mode = Transaction
2.配置用户密码文件users.txt
[highgo@sourcedb pgbouncer]$ pwd
/opt/pgbouncer/pgbouncer
$ cat users.txt 
"highgo" "highgo123"






三.启动 
1.启动命令
[postgres9.6@localhost config]$ pgbouncer -d pgbouncer.ini 
2012-08-21 00:29:55.573 4247 LOG File descriptor limit: 1024 (H:1024), max_client_conn: 100, max fds possible: 130
2.查看日志 
$ tail -f /opt/pgbouncer/pgbouncer/pgbouncer.log 






[highgo@sourcedb pgbouncer]$ psql -h 127.0.0.1 -p 5866 -U highgo highgo
Password for user highgo: 
psql (3.1.4)
Type "help" for help.


highgo=# 


[highgo@sourcedb config]$ psql -h 127.0.0.1 -p 1999  -d aaa -U highgo
Password for user highgo: 
psql (3.1.4)
Type "help" for help.


aaa=# select current_database();
 current_database 
------------------
 highgo
(1 row)
aaa=# \l
                                  List of databases
     Name     |    Owner     | Encoding |  Collate   |   Ctype    | Access privileges 
--------------+--------------+----------+------------+------------+-------------------
 benchmarksql | benchmarksql | UTF8     | zh_CN.utf8 | zh_CN.utf8 | 
 highgo       | highgo       | UTF8     | zh_CN.utf8 | zh_CN.utf8 | 
 template0    | highgo       | UTF8     | zh_CN.utf8 | zh_CN.utf8 | =c/highgo        +
              |              |          |            |            | highgo=CTc/highgo
 template1    | highgo       | UTF8     | zh_CN.utf8 | zh_CN.utf8 | =c/highgo        +
              |              |          |            |            | highgo=CTc/highgo
(4 rows)


aaa=# 
aaa=# \q
[highgo@sourcedb config]$ psql -h 127.0.0.1 -p 1999  -U highgo -d pgbouncer
Password for user highgo: 
psql (3.1.4, server 1.5.4/bouncer)
Type "help" for help.


pgbouncer=# \l
ERROR:  invalid command 'SELECT d.datname as "Name",
       pg_catalog.pg_get_userbyid(d.datdba) as "Owner",
       pg_catalog.pg_encoding_to_char(d.encoding) as "Encoding",
       pg_catalog.array_to_string(d.datacl, '\n') AS "Access privileges"
FROM pg_catalog.pg_database d
ORDER BY 1;', use SHOW HELP;


pgbouncer=# show config
pgbouncer-# ;
            key            |                 value                  | changeable 
---------------------------+----------------------------------------+------------
 job_name                  | pgbouncer                              | no
 conffile                  | pgbouncer.ini                          | yes
 logfile                   | /opt/pgbouncer/pgbouncer/pgbouncer.log | yes
 pidfile                   | /opt/pgbouncer/pgbouncer/pgbouncer.pid | no
 listen_addr               | *            
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PostgreSQL连接池是一种用于管理和复用数据库连接的技术,它可以提高应用程序与数据库之间的性能和效率。连接池允许应用程序在需要时从连接池中获取数据库连接,并在使用完毕后将连接返回给连接池,而不是每次都重新创建和关闭连接。 在使用PostgreSQL连接池之前,需要确保已经安装了PostgreSQL数据库,并且已经创建了相应的数据库和用户。 下面是一个使用Spring Boot配置PostgreSQL连接池的示例: 1. 在Spring Boot项目的配置文件(例如application.yml或application.properties)中添加以下配置: ```yaml spring: datasource: url: jdbc:postgresql://pgbouncer-ip:pgbouncer端口/数据库?prepareThreshold=0 driverClassName: org.postgresql.Driver username: postgres(pgbouncer中数据库对应的用户名) password: postgres(pgbouncer中数据库对应的密码) ``` 2. 确保替换上述配置中的以下参数: - pgbouncer-ip:PGbouncer的IP地址 - pgbouncer端口:PGbouncer的端口号 - 数据库:要连接的数据库名称 - pgbouncer中数据库对应的用户名和密码 通过以上配置,Spring Boot将会自动创建一个连接池,并在需要时从连接池中获取连接。你可以在应用程序中使用这些连接来执行数据库操作。 请注意,上述示例中的配置是基于Spring Boot的方式,如果你使用的是其他框架或纯Java代码,你需要根据相应的框架或库的文档进行配置
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值