wget https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
1、安装libevent
$ cd libevent-2.0.21-stable
$ ./configure --prefix=/usr/local/libevent
$ make
$ make install
wget https://pgbouncer.github.io/downloads/files/1.7.2/pgbouncer-1.7.2.tar.gz
2、安装pgbouncer
$ cd pgbouncer-1.7.2
$ ./configure --prefix=/usr/local/pgbouncer/ --with-libevent=/usr/local/libevent/
$ make
$ make install
3、这个文件增加一下环境变量
vim /etc/profile
export LD_LIBRARY_PATH=/usr/local/libevent/lib:$LD_LIBRARY_PATH
4、pgbouncer配置
配置 这两个文件:pgbouncer.ini 和users.txt文件
cd /usr/local/pgbouncer
mkdir config
cp /usr/local/pgbouncer/share/doc/pgbouncer.ini config
cp /usr/local/pgbouncer/share/doc/users.txt config
pgbouncer.ini文件设置的内容如下:
[databases]
testdb = host=192.0.0.34 port=5001 user=testdb password=testdb dbname=testdb connect_query='SELECT 1'
[pgbouncer]
listen_port = 4001
listen_addr = *
auth_type = trust
auth_file = /usr/local/pgbouncer/conf/users.txt
logfile = /usr/local/pgbouncer/pgbouncer.log
pidfile = /usr/local/pgbouncer/pgbouncer.pid
admin_users = root
pool_mode = session
ignore_startup_parameters = extra_float_digits,application_name,geqo
max_client_conn = 3000
default_pool_size = 100
说明:
databases里面的数据库可以配置多个
testdb 设置为数据库实例名称,port=5001为数据库的端口
listen_port 设置为对外提供服务连接的端口
max_client_conn 是对外提供数据库连接的个数,根据现场的情况调整
default_pool_size是连接到数据库个数,这个参数现场的情况调整,不宜设置过多,默认为20
pool_mode 指明了连接池的模型,pgbouncer目前支持三种连接池模型。分别是session, transaction和statment三个级别。
a. session. 会话级链接。只有与当客户端的会话结束时,pgbouncer才会收回已分配的链接
b. transaction 事务级连接。当事务完成后,pgbouncer会回收已分配的链接。也就是说客户端只是在事务中才能独占此链接,非事务的对数据库的请求是没有独享的链接的。
c. statement 语句级链接。任何对数据库的请求完成后,pgbouncer都会回收链接。此种模式下,客户端不能使用事务,否则会造成数据的不一致。
pgbouncer的默认设置是session链接。
users.txt内容为:
"marko" "asdasd"
"postgres" "asdasd"
"pgbouncer" "fake"
6、启动pgbouncer
可以设置启动的用户和用户组,然后执行
/usr/local/pgbouncer/bin/pgbouncer -d /usr/local/pgbouncer/conf/pgbouncer.ini
缺省的配置示范文件为:
[root@INTPLE-NJ-34 etc]# more pgbouncer.ini
;; database name = connect string
;;
;; connect string params:
;; dbname= host= port= user= password=
;; client_encoding= datestyle= timezone=
;; pool_size= connect_query=
[databases]; foodb over unix socket
;foodb =; redirect bardb to bazdb on localhost
;bardb = host=localhost dbname=bazdb; access to dest database will go with single user
;forcedb = host=127.0.0.1 port=300 user=baz password=foo client_encoding=UNICODE datestyle=ISO connect_query='SELECT 1'; use custom pool sizes
;nondefaultdb = pool_size=50 reserve_pool_size=10; fallback connect string
;* = host=testserver;; Configuration section
[pgbouncer];;;
;;; Administrative settings
;;;logfile = /var/log/pgbouncer/pgbouncer.log
pidfile = /var/run/pgbouncer/pgbouncer.pid;;;
;;; Where to wait for clients
;;;; ip address or * which means all ip-s
listen_addr = 127.0.0.1
listen_port = 6432; unix socket is also used for -R.
; On debian it should be /var/run/postgresql
;unix_socket_dir = /tmp
;unix_socket_mode = 0777
;unix_socket_group =;;;
;;; TLS settings for accepring clients
;;;;; disable, allow, require, verify-ca, verify-full
;client_tls_sslmode = disable;; Path to file that contains trusted CA certs
;client_tls_ca_file =;; Private key and cert to present to clients.
;; Required for accepting TLS connections from clients.
;client_tls_key_file =
;client_tls_cert_file =;; fast, normal, secure, legacy,
;client_tls_ciphers = fast;; all, secure, tlsv1.0, tlsv1.1, tlsv1.2
;client_tls_protocols = all;; none, auto, legacy
;client_tls_dheparams = auto;; none, auto,
;client_tls_ecdhcurve = auto;;;
;;; TLS settings for connecting to backend databases
;;;;; disable, allow, require, verify-ca, verify-full
;server_tls_sslmode = disable;; Path to that contains trusted CA certs
;server_tls_ca_file =;; Private key and cert to present to backend.
;; Needed only if backend server require client cert.
;server_tls_key_file =
;server_tls_cert_file =;; all, secure, tlsv1.0, tlsv1.1, tlsv1.2
;server_tls_protocols = all;; fast, normal, secure, legacy,
;server_tls_ciphers = fast;;;
;;; Authentication settings
;;;; any, trust, plain, crypt, md5
auth_type = trust
;auth_file = /8.0/main/global/pg_auth
auth_file = /etc/pgbouncer/userlist.txt;; Path to HBA-style auth config
;auth_hba_file =;; Query to use to fetch password from database. Result
;; must have 2 columns - username and password hash.
;auth_query = SELECT usename, passwd FROM pg_shadow WHERE usename=$1;;;
;;; Users allowed into database 'pgbouncer'
;;;; comma-separated list of users, who are allowed to change settings
;admin_users = user2, someadmin, otheradmin; comma-separated list of users who are just allowed to use SHOW command
;stats_users = stats, root;;;
;;; Pooler personality questions
;;;; When server connection is released back to pool:
; session - after client disconnects
; transaction - after transaction finishes
; statement - after statement finishes
pool_mode = session;
; Query for cleaning connection immediately after releasing from client.
; No need to put ROLLBACK here, pgbouncer does not reuse connections
; where transaction is left open.
;
; Query for 8.3+:
; DISCARD ALL;
;
; Older versions:
; RESET ALL; SET SESSION AUTHORIZATION DEFAULT
;
; Empty if transaction pooling is in use.
;
server_reset_query = DISCARD ALL; Whether server_reset_query should run in all pooling modes.
; If it is off, server_reset_query is used only for session-pooling.
;server_reset_query_always = 0;
; Comma-separated list of parameters to ignore when given
; in startup packet. Newer JDBC versions require the
; extra_float_digits here.
;
;ignore_startup_parameters = extra_float_digits;
; When taking idle server into use, this query is ran first.
; SELECT 1
;
;server_check_query = select 1; If server was used more recently that this many seconds ago,
; skip the check query. Value 0 may or may not run in immediately.
;server_check_delay = 30;; Use
as application_name on server.
;application_name_add_host = 0;;;
;;; Connection limits
;;;; total number of clients that can connect
max_client_conn = 100; default pool size. 20 is good number when transaction pooling
; is in use, in session pooling it needs to be the number of
; max clients you want to handle at any moment
default_pool_size = 20;; Minimum number of server connections to keep in pool.
;min_pool_size = 0; how many additional connection to allow in case of trouble
;reserve_pool_size = 5; if a clients needs to wait more than this many seconds, use reserve pool
;reserve_pool_timeout = 3; how many total connections to a single database to allow from all pools
;max_db_connections = 50
;max_user_connections = 50; If off, then server connections are reused in LIFO manner
;server_round_robin = 0;;;
;;; Logging
;;;;; Syslog settings
;syslog = 0
;syslog_facility = daemon
;syslog_ident = pgbouncer; log if client connects or server connection is made
;log_connections = 1; log if and why connection was closed
;log_disconnections = 1; log error messages pooler sends to clients
;log_pooler_errors = 1;; Period for writing aggregated stats into log.
;stats_period = 60;; Logging verbosity. Same as -v switch on command line.
;verbose=0;;;
;;; Timeouts
;;;;; Close server connection if its been connected longer.
;server_lifetime = 1200;; Close server connection if its not been used in this time.
;; Allows to clean unnecessary connections from pool after peak.
;server_idle_timeout = 60;; Cancel connection attempt if server does not answer takes longer.
;server_connect_timeout = 15;; If server login failed (server_connect_timeout or auth failure)
;; then wait this many second.
;server_login_retry = 15;; Dangerous. Server connection is closed if query does not return
;; in this time. Should be used to survive network problems,
;; not as statement_timeout. (default: 0)
;query_timeout = 0;; Dangerous. Client connection is closed if the query is not assigned
;; to a server in this time. Should be used to limit the number of queued
;; queries in case of a database or network failure. (default: 120)
;query_wait_timeout = 120;; Dangerous. Client connection is closed if no activity in this time.
;; Should be used to survive network problems. (default: 0)
;client_idle_timeout = 0;; Disconnect clients who have not managed to log in after connecting
;; in this many seconds.
;client_login_timeout = 60;; Clean automatically created database entries (via "*") if they
;; stay unused in this many seconds.
; autodb_idle_timeout = 3600;; How long SUSPEND/-R waits for buffer flush before closing connection.
;suspend_timeout = 10;; Close connections which are in "IDLE in transaction" state longer than
;; this many seconds.
;idle_transaction_timeout = 0;;;
;;; Low-level tuning options
;;;;; buffer for streaming packets
;pkt_buf = 4096;; man 2 listen
;listen_backlog = 128;; Max number pkt_buf to process in one event loop.
;sbuf_loopcnt = 5;; Maximum Postgres protocol packet size.
;max_packet_size = 2147483647;; networking options, for info: man 7 tcp
;; Linux: notify program about new connection only if there
;; is also data received. (Seconds to wait.)
;; On Linux the default is 45, on other OS'es 0.
;tcp_defer_accept = 0;; In-kernel buffer size (Linux default: 4096)
;tcp_socket_buffer = 0;; whether tcp keepalive should be turned on (0/1)
;tcp_keepalive = 1;; following options are Linux-specific.
;; they also require tcp_keepalive=1;; count of keepaliva packets
;tcp_keepcnt = 0;; how long the connection can be idle,
;; before sending keepalive packets
;tcp_keepidle = 0;; The time between individual keepalive probes.
;tcp_keepintvl = 0;; DNS lookup caching time
;dns_max_ttl = 15;; DNS zone SOA lookup period
;dns_zone_check_period = 0;; DNS negative result caching time
;dns_nxdomain_ttl = 15;;;
;;; Random stuff
;;;;; Hackish security feature. Helps against SQL-injection - when PQexec is disabled,
;; multi-statement cannot be made.
;disable_pqexec=0;; Config file to use for next RELOAD/SIGHUP.
;; By default contains config file from command line.
;conffile;; Win32 service name to register as. job_name is alias for service_name,
;; used by some Skytools scripts.
;service_name = pgbouncer
;job_name = pgbouncer;; Read additional config from the /etc/pgbouncer/pgbouncer-other.ini file
;%include /etc/pgbouncer/pgbouncer-other.ini
简单的使用手册为:
Quick-start
===========Basic setup and usage as following.
Create a pgbouncer.ini file. Details in pgbouncer(5). Simple example::
[databases]
template1 = host=127.0.0.1 port=5432 dbname=template1[pgbouncer]
listen_port = 6543
listen_addr = 127.0.0.1
auth_type = md5
auth_file = users.txt
logfile = pgbouncer.log
pidfile = pgbouncer.pid
admin_users = someuserCreate
users.txt
file that contains users allowed in::"someuser" "same_password_as_in_server"
Launch pgbouncer::
$ pgbouncer -d pgbouncer.ini
Have your application (or the psql client) connect to
pgbouncer instead of directly to PostgreSQL server::$ psql -p 6543 -U someuser template1
Manage pgbouncer by connecting to the special administration
database pgbouncer and issuingshow help;
to begin::$ psql -p 6543 -U someuser pgbouncer
pgbouncer=# show help;
NOTICE: Console usage
DETAIL:
SHOW [HELP|CONFIG|DATABASES|FDS|POOLS|CLIENTS|SERVERS|SOCKETS|LISTS|VERSION]
SET key = arg
RELOAD
PAUSE
SUSPEND
RESUME
SHUTDOWNIf you made changes to the pgbouncer.ini file, you can reload it with::
pgbouncer=# RELOAD;
Command line switches
-d
Run in background. Without it the process will run in foreground.
Note: Does not work on Windows, pgbouncer need to run as service there.-R
Do an online restart. That means connecting to the running process,
loading the open sockets from it, and then using them. If there
is no active process, boot normally.
Note: Works only if OS supports Unix sockets and theunix_socket_dir
is not disabled in config. Does not work on Windows machines.
Does not work with TLS connections, they are dropped.-u user
Switch to the given user on startup.-v
Increase verbosity. Can be used multiple times.-q
Be quiet - do not log to stdout. Note this does not affect
logging verbosity, only that stdout is not to be used.
For use in init.d scripts.-V
Show version.-h
Show short help.--regservice
Win32: Register pgbouncer to run as Windows service. The service_name
config parameter value is used as name to register under.--unregservice
Win32: Unregister Windows service.Admin console
The console is available by connecting as normal to the
database pgbouncer::$ psql -p 6543 pgbouncer
Only users listed in configuration parameters admin_users or stats_users
are allowed to login to the console. (Except whenauth_mode=any
, then
any user is allowed in as a stats_user.)Additionally, the username pgbouncer is allowed to log in without password,
if the login comes via Unix socket and the client has same Unix user uid
as the running process.Show commands
The **SHOW** commands output information. Each command is described below. SHOW STATS; ----------- Shows statistics. database Statistics are presented per database. total_requests Total number of SQL requests pooled by **pgbouncer**. total_received Total volume in bytes of network traffic received by **pgbouncer**. total_sent Total volume in bytes of network traffic sent by **pgbouncer**. total_query_time Total number of microseconds spent by **pgbouncer** when actively connected to PostgreSQL. avg_req Average requests per second in last stat period. avg_recv Average received (from clients) bytes per second. avg_sent Average sent (to clients) bytes per second. avg_query Average query duration in microseconds. SHOW SERVERS; ------------- type S, for server. user Username **pgbouncer** uses to connect to server. database Database name. state State of the pgbouncer server connection, one of **active**, **used** or **idle**. addr IP address of PostgreSQL server. port Port of PostgreSQL server. local_addr Connection start address on local machine. local_port Connection start port on local machine. connect_time When the connection was made. request_time When last request was issued. ptr Address of internal object for this connection. Used as unique ID. link Address of client connection the server is paired with. remote_pid Pid of backend server process. In case connection is made over unix socket and OS supports getting process ID info, it's OS pid. Otherwise it's extracted from cancel packet server sent, which should be PID in case server is Postgres, but it's a random number in case server it another PgBouncer. SHOW CLIENTS; ------------- type C, for client. user Client connected user. database Database name. state State of the client connection, one of **active**, **used**, **waiting** or **idle**. addr IP address of client. port Port client is connected to. local_addr Connection end address on local machine. local_port Connection end port on local machine. connect_time Timestamp of connect time. request_time Timestamp of latest client request. ptr Address of internal object for this connection. Used as unique ID. link Address of server connection the client is paired with. remote_pid Process ID, in case client connects over UNIX socket and OS supports getting it. SHOW POOLS; ----------- A new pool entry is made for each couple of (database, user). database Database name. user User name. cl_active Client connections that are linked to server connection and can process queries. cl_waiting Client connections have sent queries but have not yet got a server connection. sv_active Server connections that linked to client. sv_idle Server connections that unused and immediately usable for client queries. sv_used Server connections that have been idle more than `server_check_delay`, so they needs `server_check_query` to run on it before it can be used. sv_tested Server connections that are currently running either `server_reset_query` or `server_check_query`. sv_login Server connections currently in logging in process. maxwait How long the first (oldest) client in queue has waited, in seconds. If this starts increasing, then the current pool of servers does not handle requests quick enough. Reason may be either overloaded server or just too small of a **pool_size** setting. pool_mode The pooling mode in use. SHOW LISTS; ----------- Show following internal information, in columns (not rows): databases Count of databases. users Count of users. pools Count of pools. free_clients Count of free clients. used_clients Count of used clients. login_clients Count of clients in **login** state. free_servers Count of free servers. used_servers Count of used servers. SHOW USERS; ----------- name The user name pool_mode The user's override pool_mode, or NULL if the default will be used instead. SHOW DATABASES; --------------- name Name of configured database entry. host Host pgbouncer connects to. port Port pgbouncer connects to. database Actual database name pgbouncer connects to. force_user When user is part of the connection string, the connection between pgbouncer and PostgreSQL is forced to the given user, whatever the client user. pool_size Maximum number of server connections. pool_mode The database's override pool_mode, or NULL if the default will be used instead. SHOW FDS; --------- Internal command - shows list of fds in use with internal state attached to them. When the connected user has username "pgbouncer", connects through Unix socket and has same UID as running process, the actual fds are passed over the connection. This mechanism is used to do an online restart. Note: This does not work on Windows machines. This command also blocks internal event loop, so it should not be used while PgBouncer is in use. fd File descriptor numeric value. task One of **pooler**, **client** or **server**. user User of the connection using the FD. database Database of the connection using the FD. addr IP address of the connection using the FD, **unix** if a unix socket is used. port Port used by the connection using the FD. cancel Cancel key for this connection. link fd for corresponding server/client. NULL if idle. SHOW CONFIG; ------------ Show the current configuration settings, one per row, with following columns: key Configuration variable name value Configuration value changeable Either **yes** or **no**, shows if the variable can be changed while running. If **no**, the variable can be changed only boot-time. SHOW DNS_HOSTS; --------------- Show hostnames in DNS cache. hostname Host name. ttl How meny seconds until next lookup. addrs Comma separated list of addresses. SHOW DNS_ZONES -------------- Show DNS zones in cache. zonename Zone name. serial Current serial. count Hostnames belonging to this zone. Process controlling commands
PAUSE [db];
PgBouncer tries to disconnect from all servers, first waiting for all queries
to complete. The command will not return before all queries are finished. To be used
at the time of database restart.If database name is given, only that database will be paused.
DISABLE db;
Reject all new client connections on the given database.
ENABLE db;
Allow new client connections after a previous DISABLE command.
KILL db;
Immediately drop all client and server connections on given database.
SUSPEND;
All socket buffers are flushed and PgBouncer stops listening for data on them.
The command will not return before all buffers are empty. To be used at the time
of PgBouncer online reboot.RESUME [db];
Resume work from previous PAUSE or SUSPEND command.
SHUTDOWN;
The PgBouncer process will exit.
RELOAD;
The PgBouncer process will reload its configuration file and update
changeable settings.Signals
SIGHUP Reload config. Same as issuing command **RELOAD;** on console. SIGINT Safe shutdown. Same as issuing **PAUSE;** and **SHUTDOWN;** on console. SIGTERM Immediate shutdown. Same as issuing **SHUTDOWN;** on console. Libevent settings
From libevent docs::
It is possible to disable support for epoll, kqueue, devpoll, poll
or select by setting the environment variable EVENT_NOEPOLL,
EVENT_NOKQUEUE, EVENT_NODEVPOLL, EVENT_NOPOLL or EVENT_NOSELECT,
respectively.By setting the environment variable EVENT_SHOW_METHOD, libevent
displays the kernel notification method that it uses.See also
pgbouncer(5) - manpage of configuration settings descriptions.
https://pgbouncer.github.io/
https://wiki.postgresql.org/wiki/PgBouncer