安裝配置PgBouncer for PostgreSQL

PgBouncer鏈接池來自於PostgreSQL社區,它能夠爲多個數據庫管理鏈接池,而且這些數據庫能夠位於不一樣的PostgreSQL後端。PgBouncer會爲每一種數據庫用戶與數據庫的組合創建一個池。一個被池化的鏈接只能被來自於同一個用戶和數據庫的另外一個鏈接請求重用。
客戶端應用不須要作軟件修改,可是要鏈接到鏈接池的主機和端口而不是PostgreSQL的主機和端口。PgBouncer會建立新的數據庫鏈接或者重用一個已有的鏈接。當客戶端斷開鏈接時,該鏈接會被返回給鏈接池以備重用。git

一、安裝pgbounce

PgBounce的安裝有兩種方式,源代碼安裝和安裝rpm二進制軟件包。這裏使用rpm軟件安裝,以下:sql

[root@hdp06 ~]# yum -y install pgbouncer 
[root@hdp06 ~]# rpm -ql pgbouncer
/etc/logrotate.d/pgbouncer
/etc/pgbouncer
/etc/pgbouncer/mkauth.py
/etc/pgbouncer/mkauth.pyc
/etc/pgbouncer/mkauth.pyo
/etc/pgbouncer/pgbouncer.ini
/etc/sysconfig/pgbouncer
/usr/bin/pgbouncer
/usr/lib/systemd/system/pgbouncer.service
/usr/lib/tmpfiles.d/pgbouncer.conf
/usr/share/doc/pgbouncer
/usr/share/doc/pgbouncer/NEWS.rst
/usr/share/doc/pgbouncer/README.rst
/usr/share/doc/pgbouncer/pgbouncer.ini
/usr/share/doc/pgbouncer/userlist.txt
/usr/share/licenses/pgbouncer-1.9.0
/usr/share/licenses/pgbouncer-1.9.0/COPYRIGHT
/usr/share/man/man1/pgbouncer.1.gz
/usr/share/man/man5/pgbouncer.5.gz
/var/run/pgbouncer

二、配置pgbounce

編輯配置文件,修改如下內容:數據庫

[databases]
kkdb= host=192.168.120.149 port=5432 user=kkuser password=redhat
postgres= host=192.168.120.149 port=5432 user=postgres password=redhat
[pgbouncer]
logfile = /var/log/pgbouncer/pgbouncer.log
pidfile = /var/run/pgbouncer/pgbouncer.pid
listen_addr = *
listen_port = 6432
auth_type = md5
auth_file = /etc/pgbouncer/userlist.txt
admin_users = postgres,kkuser
pool_mode = session
server_reset_query = DISCARD ALL
ignore_startup_parameters = extra_float_digits
max_client_conn = 1000
default_pool_size = 200

上面的認證模式md5,因此指定認證文件的路徑,並加入如下內容:後端

[root@hdp06 pgbouncer]# vi userlist.txt 
"postgres" "md5fe4b5cfa57349c59d9b18d6920de5f59"
"kkuser" "md5ad9501a851743f17648b943395f791e0"

其中的md5值,能夠在數據庫中經過md5將密碼進行編碼,以下:session

postgres=# SELECT 'md5'||md5('redhat'||'postgres');

安裝配置PgBouncer for PostgreSQL
若是安裝的是rpm二進制軟件包,則使用下面的方法:tcp

[root@hdp06 ~]# cd /etc/pgbouncer
[root@hdp06 pgbouncer]# ./mkauth.py userlist.txt "host=192.168.120.149 port=5432 dbname=kkdb user=kkuser password=redhat"
[root@hdp06 pgbouncer]# chown pgbouncer:pgbouncer userlist.txt
--或者直接查詢數據庫
postgres=# select usename, passwd from pg_shadow order by 1;

安裝配置PgBouncer for PostgreSQL
或者使用下面的命令直接生成,刪除不須要的行便可:ide

postgres=# copy (select '"' || rolname|| '" "'|| coalesce(rolpassword,'')||'"' from pg_authid) to '/tmp/userlist.txt';

三、啓動pgbouncer

若是經過源代碼安裝,只能使用非root用戶啓動。post

[root@hdp06 pgbouncer]# systemctl enable pgbouncer
[root@hdp06 pgbouncer]# systemctl start pgbouncer
[root@hdp06 pgbouncer]# systemctl status pgbouncer
● pgbouncer.service - A lightweight connection pooler for PostgreSQL
   Loaded: loaded (/usr/lib/systemd/system/pgbouncer.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2018-12-13 17:13:07 CST; 5s ago
  Process: 21134 ExecStart=/usr/bin/pgbouncer -d -q ${BOUNCERCONF} (code=exited, status=0/SUCCESS)
 Main PID: 21136 (pgbouncer)
   CGroup: /system.slice/pgbouncer.service
           └─21136 /usr/bin/pgbouncer -d -q /etc/pgbouncer/pgbouncer.ini

Dec 13 17:13:07 hdp06 systemd[1]: Starting A lightweight connection pooler for PostgreSQL...
Dec 13 17:13:07 hdp06 systemd[1]: PID file /var/run/pgbouncer/pgbouncer.pid not readable (yet?) after start.
Dec 13 17:13:07 hdp06 systemd[1]: Started A lightweight connection pooler for PostgreSQL.
[root@hdp06 pgbouncer]# netstat -antpl|grep 6432
tcp        0      0 0.0.0.0:6432            0.0.0.0:*               LISTEN      21136/pgbouncer     
tcp6       0      0 :::6432                 :::*                    LISTEN      21136/pgbouncer

四、測試pgbouncer

[postgres@pg07 ~]$ psql -Upostgres -dpostgres -p6432 -h192.168.120.104
Password for user postgres: 
psql (10.6)
Type "help" for help.
postgres=# \l

安裝配置PgBouncer for PostgreSQL

五、管理PgBouncer

PgBouncer有一個管理控制檯,能夠登陸到pgbouncer虛擬數據庫來訪問它。該控制檯接受類SQL命令,這些命令容許用戶監控、從新配置和管理PgBouncer。測試

[postgres@pg07 ~]$ psql -Upostgres -dpgbouncer -p6432 -h192.168.120.104
Password for user postgres: 
psql (10.6, server 1.9.0/bouncer)
Type "help" for help.

pgbouncer=# show clients;
pgbouncer=# show servers;

安裝配置PgBouncer for PostgreSQL
若是變動了pgbouncer配置文件,不用重啓,直接reload下就ok。編碼

pgbouncer=# reload;

更多pgbouncer管理,請參考官方文檔。

相關文章
相關標籤/搜索