花了一上午時間將pgbouncer的參數通讀了一遍,對他有個大體的瞭解:
1.配置分爲鏈接池和pgbouncer兩個部分[database]\[pgbouncer ]。
2.一條記錄對應建立一個鏈接池,鏈接池的大小能夠在這條配置中設置,若是不設置,則使用pgbouncer部分中配置的默認值20,固然也能夠修改。
3.驗證模式使用md5,則須要配置user.txt文件,即密碼文件;若是使用hba,則須要指定對應的pg_hba.conf文件(便可以使用PostgreSQL的驗證模式)。
4.admin_user是容許哪些用戶能夠在控制檯鏈接pgbouncer這個管理數據庫,並進行修改。
5.在database配置鏈接池中,又分爲配置user/password和auth_user,這裏還有點迷糊,後面實驗了再來講,下面是解釋:
user, password If user= is set, all connections to the destination database will be done with the specified user, meaning that there will be only one pool for this database. Otherwise PgBouncer tries to log into the destination database with client username, meaning that there will be one pool per user. auth_user If auth_user is set, any user not specified in auth_file will be queried from pg_shadow in the database using auth_user. Auth_user’s password will be taken from auth_file. Direct access to pg_shadow requires admin rights. It’s preferable to use non-admin user that calls SECURITY DEFINER function instead.
6.超時時間中server_lifetime和server_idle_timeout表示不是很明白,超時後第一個直接close掉,第二個直接drop掉,是何意思?
server_lifetime The pooler will try to close server connections that have been connected longer than this. Setting it to 0 means the connection is to be used only once, then closed. [seconds] Default: 3600.0 server_idle_timeout If a server connection has been idle more than this many seconds it will be dropped. If 0 then timeout is disabled. [seconds] Default: 600.0
進入正題,安裝過程當中遇到了一個比較蛋疼的問題,openssl安裝了,可是configure時提示沒有找到。查了一些資料都是胡扯,最後發現是少了對應的庫:
checking for OpenSSL... configure: error: not found
緣由:
The OpenSSL library is usually already installed, but you have to install the header files. Depending on your Linux distribution, you'll need these packages:sql
openssl-devel
libssl-dev
openssl
yum install openssl-devel -y
下面是具體安裝步驟了,官網的:
$ ./configure --prefix=/usr/local --with-libevent=libevent-prefix $ make $ make install
配置文件:數據庫
-bash-4.1$ cat pgbouncer.ini [databases] mydb = host=127.0.0.1 port=5433 dbname=mydb user=postgres password=postgres postgres = host=127.0.0.1 port=5433 dbname=postgres user=postgres password=postgres [pgbouncer] listen_port = 5432 listen_addr = * auth_type = md5 auth_file = /var/lib/pgsql/pgbouncer/config/user.txt logfile = /var/lib/pgsql/pgbouncer/config/pgbouncer.log pidfile = /var/lib/pgsql/pgbouncer/config/pgbouncer.pid admin_users = postgres pool_mode = session
因爲配置的是md5,所以這裏要用user.txt文件:bash
-bash-4.1$ cat user.txt "postgres" "postgres"
啓動:服務器
$pgbouncer -d pgbouncer.ini
-bash-4.1$ ps -ef|grep pgbouncer
postgres 29377 1 0 16:19 ? 00:00:00 pgbouncer -d pgbouncer.ini
postgres 31574 29031 0 16:28 pts/0 00:00:00 grep pgbouncersession
使用:dom
-bash-4.1$ psql -p 5432 psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"? -bash-4.1$ psql -p 5432 -h localhost Password: psql (9.5.6) Type "help" for help. postgres=# postgres=# \d No relations found. postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres (3 rows)
進入後臺,查看鏈接和重載參數:socket
-bash-4.1$ psql -p 5432 -h localhost pgbouncer Password: psql (9.5.6, server 1.7.2/bouncer) Type "help" for help. pgbouncer=# pgbouncer=# show help; NOTICE: Console usage DETAIL: 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 [<db>] RESUME [<db>] DISABLE <db> ENABLE <db> KILL <db> SUSPEND SHUTDOWN SHOW pgbouncer=# reload; RELOAD pgbouncer=# show clients; type | user | database | state | addr | port | local_addr | local_port | connect_time | request_time | ptr | link | remote_pid | tls ------+----------+-----------+--------+------+-------+------------+------------+---------------------+---------------------+----------+------+------------+----- C | postgres | pgbouncer | active | ::1 | 25196 | ::1 | 5432 | 2017-10-31 16:38:00 | 2017-10-31 16:40:30 | 0xebe780 | | 0 | (1 row) pgbouncer=# show fds; fd | task | user | database | addr | port | cancel | link | client_encoding | std_strings | datestyle | timezone | password ----+--------+----------+----------+-----------+------+---------------+------+-----------------+-------------+-----------+----------+---------- 8 | pooler | | | 0.0.0.0 | 5432 | 0 | 0 | | | | | 9 | pooler | | | :: | 5432 | 0 | 0 | | | | | 10 | pooler | | | unix | 5432 | 0 | 0 | | | | | 12 | server | postgres | postgres | 127.0.0.1 | 5433 | 3020587147775 | 0 | UTF8 | on | ISO, MDY | PRC | (4 rows)
其餘說明:
在[database]中,能夠指定其餘服務器的數據庫,並且若是數據庫名字重複,會之後一條的數據庫生效:
[databases]
kdb = host=127.0.0.1 port=5433 dbname=mydb user=postgres password=postgres
postgres = host=127.0.0.1 port=5433 dbname=postgres user=postgres password=postgres
;;postgres = host=10.9.5.20 port=5433 dbname=postgres user=postgres password=postgres