花了一上午时间将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:
Red Hat, Fedora, CentOS - openssl-devel
Debian, Ubuntu - libssl-dev
Arch - 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-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 pgbouncer
使用:
-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)
进入后台,查看连接和重载参数:
-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
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com