相关阅读: 王宇,公众号:IT那活儿pgbouncer的简单使用(上篇)
su - postgres /usr/local/pgbouncer/bin/pgbouncer -d usr/local/pgbouncer/conf/pgbouncer.ini [postgres@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 查看日志 tail -f /home/postgres/pgbouncer/pgbouncer.log |
命令介绍
-d 后台运行
-R 在线重启
-v 增加冗余信息,会消耗比较多的时间
-u 切换到指定的用户启动
-q 静默方式,不记录到stdout。请注意这并不影响记录详细,只是stdout是不被使用。在init.d脚本使用
-V 展示版本
-h 展示帮助命令
pgbouncer -d pgbouncer.ini -v ##加-v 可以打印出很多检查信息,对初使用者,是个不错的选择
psql -h 127.0.0.1 -p 6432 -u pgbouncer pgbouncer (-u 后面接管理员用户)
1. 使用客户端连接到pgbouncer:
[postgres@localhost pgbouncer]$ psql -h 127.0.0.1 -p 1999 -U u_kenyon f_gamePassword for user u_kenyon: psql (9.1.2)Type "help" for help.f_game=> \d List of relations Schema | Name | Type | Owner ------------+-------------+--------+------------- public | t_kenyon | table | postgres public | test | table | test public | test2 | table | postgres(3 rows)f_game=> \q参数使用例子 连接pgbouncer本身的DB[postgres@localhost config]$ psql -h 127.0.0.1 -p 1999 -U u_kenyon pgbouncer Password for user u_kenyon: psql (9.1.2, server 1.5.2/bouncer)WARNING: psql version 9.1, server version 1.5. Some psql features might not work.Type "help" for help.pgbouncer=# show help;NOTICE: Console usageDETAIL: SHOW HELP|CONFIG|DATABASES|POOLS|CLIENTS|SERVERS|VERSION SHOW STATS|FDS|SOCKETS|ACTIVE_SOCKETS|LISTS|MEM SHOW DNS_HOSTS|DNS_ZONES SET key = arg RELOAD PAUSE [ ] RESUME [ ] KILL SUSPEND SHUTDOWNSHOW
复制
2. 管理pgbouncer常用命令
1)SHOW HELP查看:
[postgres@localhost config]$ psql -h 127.0.0.1 -p 666 -U u_kenyon pgbouncer Password for user u_kenyon:psql (9.1.2, server 1.5.2/bouncer)WARNING: psql version 9.1, server version 1.5.Some psql features might not work.Type “help” for help.pgbouncer=# show help;NOTICE: Console usageDETAIL:SHOW HELP|CONFIG|DATABASES|POOLS|CLIENTS|SERVERS|VERSIONSHOW STATS|FDS|SOCKETS|ACTIVE_SOCKETS|LISTS|MEMSHOW DNS_HOSTS|DNS_ZONESSET key = argRELOADPAUSE [ ]RESUME [ ]KILL SUSPENDSHUTDOWNSHOW如果在pgbouncer.ini文件进行了参数修改,则使用以下命令重新加载即可:pgbouncer=# RELOAD;
复制
2) 查看配置文件:
pgbouncer=# show config; key | value | changeable ----------------------------------+------------------------------------------------------+------------ job_name | pgbouncer | no conffile | pgbouncer.ini | yes logfile | home/postgres/pgbouncer/pgbouncer.log | yes pidfile | home/postgres/pgbouncer/pgbouncer.pid | no listen_addr | 127.0.0.1 | no listen_port | 1999 | no listen_backlog | 128 | no unix_socket_dir | tmp | no unix_socket_mode | 511 | no unix_socket_group | | no auth_type | md5 | yes auth_file | home/postgres/pgbouncer/user.txt | yes pool_mode | transaction | yes max_client_conn | 100 | yes default_pool_size | 20 | yes min_pool_size | 0 | yes reserve_pool_size | 0 | yes reserve_pool_timeout | 5 | yes syslog | 0 | yes syslog_facility | daemon | yes syslog_ident | pgbouncer | yes user | | no autodb_idle_timeout | 3600 | yes server_reset_query | DISCARD ALL | yes server_check_query | select 1 | yes server_check_delay | 30 | yes query_timeout | 0 | yes query_wait_timeout | 0 | yes client_idle_timeout | 0 | yes client_login_timeout | 60 | yes idle_transaction_timeout | 0 | yes server_lifetime | 3600 | yes server_idle_timeout | 600 | yes server_connect_timeout | 15 | yes server_login_retry | 15 | yes server_round_robin | 0 | yes suspend_timeout | 10 | yes ignore_startup_parameters | | yes disable_pqexec | 0 | no dns_max_ttl | 15 | yes dns_zone_check_period | 0 | yes pkt_buf | 2048 | no sbuf_loopcnt | 5 | yes tcp_defer_accept | 1 | yes tcp_socket_buffer | 0 | yes tcp_keepalive | 1 | yes tcp_keepcnt | 0 | yes tcp_keepidle | 0 | yes tcp_keepintvl | 0 | yes verbose | 0 | yes admin_users | u_kenyon | yes stats_users | | yes stats_period | 60 | yes log_connections | 1 | yes log_disconnections | 1 | yes log_pooler_errors | 1 | yes(56 rows)
复制
pgbouncer=# reload;
RELOAD
pgbouncer=#
pgbouncer=# reload;
RELOAD
pgbouncer=#
3) 管理命令
在连接池端看连接数:

在DB端看连接数:
f_game=> select count(1) from pg_stat_activity;count--------------------- 2 (1 row)
复制
3. Java 连接 postgresql生效
通过pgbouncer数据库show clients; 和 server_main 数据库中select count (1) from pg_stat_activity;可以看到,DB的连接大幅下降了,转到pgbouncer了。
4. 其他

1. 通过admin用户连接pgbouncer查看配置:
psql -h 127.0.0.1 -p 6000 -U user pgbouncer
pgbouncer=# show config;
2. 通过admin用户连接pgbouncer查看运行情况:
pgbouncer=# show stats;
database :数据库名称
total_requests:pgbouncer汇聚SQL请求的总数
total_received: pgbouncer 接收网络请求总字节数
total_sent::pgbouncer 发送到网络的总字节数
total_query_time:pgbouncer 连接到PG数据库所使用的总时间数,单位微秒
avg_req:上一个时刻,每秒请求平均数
avg_recv:平均每秒钟在客户端那接收到的字节数
avg_sent:平均每秒发送到客户端的字节数
avg_query:每微秒平均查询数

#其余运行参数可以通过如下命令查看
pgbouncer=# show help;


3. 进程控制命令
PAUSE[db]:
这个命令尝试终止所有的到服务器端连接,首先会等待所有的查询都完成后,这个命令只有等到所有的查询都结束时才会返回,用于数据库重启。如果有指定[db] 则是终止指定的数据库;
KILL db:
刷新所有的套接字缓冲区,pgbouncer 将停止对其的监听,这个命令只有将所有的缓冲区都清空后才会返回。用于pgbouncer 在线重启
resume [db]:
这个命令配合 pause 跟 suspend 使用的,解除pgbouncer 的终止状态
shutdown:
关闭pgbouncer
reload:
重载配置文件,并更新所有的已更改的设置
DISABLE:
拒绝指定数据库上所有新客户端连接
ENALBLE:
4. PgBouncer连接类型
在每个查询结束之后,服务器的连接都会立即放回连接池。在这种模式下将不允许多语句的事务。否则,会给出如下提示,比如
test=# begin; ERROR: Long transactions not allowed server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request. The connection to the server was lost. Attempting reset: Succeeded.
复制
5. 客户端连接方式
$dbh = DBI -> connect('dbi:Pg:dbname=test;host=127.0.0.1;port=6000', 'postgres', ' ' ) or die
$DBI::errstr;
6. 连接参数配置及客户端状态

更多精彩干货分享
点击下方名片关注
IT那活儿
