热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Postgres的服务控制pgbouncer的安装与配置

参考文档:PostgreSQL9AdministrationCookbook(第二版)中文版--pg的关闭(pt_ctl&

参考文档: PostgreSQL 9 Administration Cookbook (第二版)中文版

-- pg的关闭 (pt_ctl,具体可以查看帮助)

pg_ctl -D datadir -m fast stop
pg_ctl -D datadir stop -m immediate -- 相当O的shutdown abort

-- 重新加载配置文件

pg_ctl -D datadir reload
select pg_reload_conf()

-- 查看修改的部分参数

select name,setting,unit,(source='default') as is_default from pg_settings
where context='sighup'
and (name like '%delay' or name like '%timeout')
and setting != '0';mydb=# select name,setting,unit,(source='default') as is_default from pg_settings
mydb-# where context='sighup'
mydb-# and (name like '%delay' or name like '%timeout')
mydb-# and setting != '0';name | setting | unit | is_default
------------------------------+---------+------+------------authentication_timeout | 60 | s | tautovacuum_vacuum_cost_delay | 20 | ms | tbgwriter_delay | 200 | ms | tcheckpoint_timeout | 300 | s | tmax_standby_archive_delay | 30000 | ms | tmax_standby_streaming_delay | 30000 | ms | twal_receiver_timeout | 60000 | ms | twal_sender_timeout | 60000 | ms | twal_writer_delay | 200 | ms | t
(9 rows)mydb=#

-- 快速重启服务,先做一个checkpoint,将shared_buffer里面的脏数据刷新到数据文件中

psql -c "checkpoint";
pg_ctl -D datadir restart -m fast

-- 阻止新的连接
通过设置连接限制为0来限制指定的数据库的连接数为0 ,-1 解除限制

alter database foo_db connection limit 0;

通过设置连接数限制为0,来限制指定的用户的连接数为0

alter user foo connection limit 0;

或者设置基于主机的认证文件pg_hba.conf

限制用户的连接数为1 ,-1 解除限制

alter role fred connection limit 1;
mydb=# alter database mydb connection limit -1;
ALTER DATABASE
mydb=# select rolconnlimit from pg_roles where rolname ='xxx'; -- 查看连接限制
select * from pg_stat_activity where usename ='postgres'; -- 查看连接数

-- 断开用户连接 ,使用pg提供的函数pg_terminate_backend()

select pg_terminate_backend(pid) from pg_stat_activity; -- 会断开当前的链接
postgres=# select pg_terminate_backend(pid) from pg_stat_activity;
FATAL: terminating connection due to administrator command
server closed the connection unexpectedlyThis probably means the server terminated abnormallybefore or while processing the request.
The connection to the server was lost. Attempting reset: Succeeded.
postgres=#

一个比较好的办法,是通过视图pg_stat_activity 找到pid,然后使用pg_terminate_backend(pid)终结会话

select * from pg_stat_activity -- 找到pid是21044
select pg_terminate_backend(21044); -- 该语句kill掉该会话postgres=# select now();now
-------------------------------2021-01-12 14:29:17.877438+08
(1 row)postgres=# select now();
FATAL: terminating connection due to administrator command
server closed the connection unexpectedlyThis probably means the server terminated abnormallybefore or while processing the request.
The connection to the server was lost. Attempting reset: Succeeded.
postgres=# -- 或者这样
select count(pg_terminate_backend(pid)) from pg_stat_activity where usename not in
(select usename from pg_user where usesuper);

-- 使用多个模式
可以把一组表划分到他们自己的“命名空间”,也就是PG的模式(schema)。很多情况下,被认为类似目录。

create schema finance;
create schema sales;mydb=# create table finance.t1(id integer,name varchar(10)); -- 创建一个表,在schema finance下
CREATE TABLE
mydb=# mydb=# select current_schema; -- 查看当前的schema,是publiccurrent_schema
----------------public
(1 row)mydb=#

-- 不写schema,查询不到t1,如果想让用户能够访问某组表,需要修改他的search_path

mydb=# select * from t1;
ERROR: relation "t1" does not exist
LINE 1: select * from t1;^
mydb=# mydb=# alter role postgres set search_path='finance';
ALTER ROLE
mydb=# grant all on schema finance to postgres;
GRANT
mydb=# select * from t1;id | name
----+------
(0 rows)mydb=#
mydb=# select * from t1;id | name
----+------
(0 rows)mydb=# select current_schema;current_schema
----------------finance
(1 row)mydb=# show search_path
mydb-# ;search_path
-------------finance
(1 row)mydb=#
mydb=# set search_path="$user", public;
SET
mydb=# show search_path;search_path
-----------------"$user", public
(1 row)mydb=#

-- 单独给用户分配数据库

create user fred;
create database fred owner = fred ;

这个时候,切换到fred数据库还是可以的,提示使用postgres用户连接到了fred数据库

mydb=# \c fred
You are now connected to database "fred" as user "postgres".
fred=#

收回权限,尝试以postgres来连接数据库.或者,建立一个用户bob,使用bob访问fred数据库,没有权限。只能使用有权限的用户fred连接到数据库。

revoke connect on database fred from public;
grant connect on database fred to fred ;fred=# revoke connect on database fred from public;
REVOKE
fred=# grant connect on database fred to fred ;
GRANT
fred=# create user bob ;
alter user bob with password 'oracle';
psql -U bob fred postgres=# alter user bob with password 'oracle';
ALTER ROLE-bash-4.2$ psql -d fred -U bob
Password for user bob:
psql.bin: FATAL: permission denied for database "fred"
DETAIL: User does not have CONNECT privilege.
-bash-4.2$ -bash-4.2$ psql -U fred -d fred
Password for user fred:
psql.bin (10.15)
Type "help" for help.fred=>

-- 配置连接池  pgbouncer  
https://www.pgbouncer.org/install.html

http://rpm.pbone.net/info_idpl_68340409_distro_redhatel7_com_pgbouncer-1.9.0-3.rhel7.x86_64.rpm.html
--

-- pgbouncer的安装

yum install pgbouncer-1.9.0-3.rhel7.x86_64.rpmInstalled:pgbouncer.x86_64 0:1.9.0-3.rhel7 Dependency Installed:c-ares.x86_64 0:1.10.0-3.el7 postgresql-libs.x86_64 0:9.2.24-1.el7_5 python-psycopg2.x86_64 0:2.5.1-3.el7Complete![root@asm12c tmp]# which pgbouncer
/usr/bin/pgbouncer
[root@asm12c tmp]#

-- 安装后的位置,在/etc/pgbouncer下 ,可以通过 rpm -ql | grep pgbouncer 来查询

[root@asm12c ~]# 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
/run/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
[root@asm12c ~]#

-- 编辑pgbouncer.ini文件

[databases]
mydb= host=127.0.0.1 port=5432 user=postgres password=oracle
postgres= host=127.0.0.1 port=5432 user=postgres password=oracle
korean= host=127.0.0.1 port=5432 user=postgres password=oracle
fred= host=127.0.0.1 port=5432 user=fred password=oracle
[pgbouncer]
logfile = /postgres/10/data/log/pgbouncer_log/pgbouncer.log
pidfile = /tmp/pgbouncer.pid
listen_addr = *
listen_port = 6432
auth_type = trust -- 如果是md5,需要通过pg_shadow 查询到用户的md5值,或者使用MD5函数计算出加密后的值
auth_file = /etc/pgbouncer/userlist.txt
admin_users = postgres,fred
pool_mode = session
server_reset_query = DISCARD ALL
ignore_startup_parameters = extra_float_digits
max_client_conn = 1000
default_pool_size = 200

-- 计算MD5值

\o users.txt
\t
SELECT '"'||rolname||'" "'||rolpassword||'"'
FROM pg_authid;
\qpostgres=# \o users.txt
postgres=# \t
Tuples only is on.
postgres=# SELECT '"'||rolname||'" "'||rolpassword||'"'
postgres-# FROM pg_authid;
postgres=# \q-bash-4.2$ more users.txt
?column? |
---------+-------------------------------------------------
?column? |
---------+-------------------------------------------------
?column? |
---------+-------------------------------------------------
?column? |
---------+-------------------------------------------------
?column? |
---------+-------------------------------------------------
?column? | "postgres" "md57e1a83ffb47df2396c5eafd05bac4ade"
---------+-------------------------------------------------
?column? | "bob" "md535bb865af345b47dcbebab163fe54390"
---------+-------------------------------------------------
?column? | "fred" "md569361356a5bde6d88405a0f45d28bb30"-bash-4.2$

-- 编辑userlist.txt, 因为上面认证使用的是trust,所以文件中写明文密码了。(生产不建议)

[root@asm12c ~]# more /etc/pgbouncer/userlist.txt
"postgres" "oracle"
"fred" "oracle"
[root@asm12c ~]#

-- 启动pgbouncer

su - postgres
pgbouncer -d /etc/pgbouncer/pgbouncer.ini[root@asm12c ~]# netstat -anlpt | grep 6432
tcp 0 0 0.0.0.0:6432 0.0.0.0:* LISTEN 12514/pgbouncer
tcp 0 0 127.0.0.1:9637 127.0.0.1:6432 ESTABLISHED 12759/psql.bin
tcp 0 0 127.0.0.1:6432 127.0.0.1:9637 ESTABLISHED 12514/pgbouncer
tcp6 0 0 :::6432 :::* LISTEN 12514/pgbouncer
[root@asm12c ~]#

-- 登录到pgbouncer 

psql -Upostgres -dpgbouncer -p6432 -h127.0.0.1pgbouncer=# \x
Expanded display is on.
pgbouncer=# show clients;
-[ RECORD 1 ]+--------------------
type | C
user | postgres
database | pgbouncer
state | active
addr | 127.0.0.1
port | 9637
local_addr | 127.0.0.1
local_port | 6432
connect_time | 2021-01-13 15:32:45
request_time | 2021-01-13 15:48:53
wait | 964
wait_us | 397897
close_needed | 0
ptr | 0x18d7730
link |
remote_pid | 0
tls | pgbouncer=#

-- 查看连接池

pgbouncer=# show pools;database | user | cl_active | cl_waiting | sv_active | sv_idle | sv_used | sv_tested | sv_login | maxwait | maxwait_us | pool_mode
-----------+-----------+-----------+------------+-----------+---------+---------+-----------+----------+---------+------------+-----------korean | postgres | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | sessionmydb | postgres | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | sessionpgbouncer | pgbouncer | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | statementpostgres | postgres | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | session
(4 rows)pgbouncer=#

-- 列出数据库,报错,难道pgbouncer版本和postgres版本不兼容?

pgbouncer=# \l
ERROR: invalid command 'SELECT d.datname as "Name",pg_catalog.pg_get_userbyid(d.datdba) as "Owner",pg_catalog.pg_encoding_to_char(d.encoding) as "Encoding",pg_catalog.array_to_string(d.datacl, '\n') AS "Access privileges"
FROM pg_catalog.pg_database d
ORDER BY 1;', use SHOW HELP;
pgbouncer=#

-- 重新加载pgbouncer的配置信息

pgbouncer=# reload
pgbouncer-# ;
RELOAD
pgbouncer=#

-- 重启pgbouncer

su - postgres
pgbouncer -R /etc/pgbouncer/pgbouncer.ini

-- 关闭pgbouncer

cat /tmp/pgbouncer.pid | xargs kill -9 -- 这个是网上看到的
psql -p 6432 pgbouncer -c "SHUTDOWN"

-- 查看帮助

pgbouncer=# show help;
NOTICE: Console usage
DETAIL: SHOW HELP|CONFIG|DATABASES|POOLS|CLIENTS|SERVERS|VERSIONSHOW FDS|SOCKETS|ACTIVE_SOCKETS|LISTS|MEMSHOW DNS_HOSTS|DNS_ZONESSHOW STATS|STATS_TOTALS|STATS_AVERAGESSET key = argRELOADPAUSE []RESUME []DISABLE ENABLE RECONNECT []KILL SUSPENDSHUTDOWNSHOW
pgbouncer=#

-- pgadmin的连接,可以连接。

END


推荐阅读
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • springmvc学习笔记(十):控制器业务方法中通过注解实现封装Javabean接收表单提交的数据
    本文介绍了在springmvc学习笔记系列的第十篇中,控制器的业务方法中如何通过注解实现封装Javabean来接收表单提交的数据。同时还讨论了当有多个注册表单且字段完全相同时,如何将其交给同一个控制器处理。 ... [详细]
  • 本文介绍了南邮ctf-web的writeup,包括签到题和md5 collision。在CTF比赛和渗透测试中,可以通过查看源代码、代码注释、页面隐藏元素、超链接和HTTP响应头部来寻找flag或提示信息。利用PHP弱类型,可以发现md5('QNKCDZO')='0e830400451993494058024219903391'和md5('240610708')='0e462097431906509019562988736854'。 ... [详细]
  • 本文介绍了在MFC下利用C++和MFC的特性动态创建窗口的方法,包括继承现有的MFC类并加以改造、插入工具栏和状态栏对象的声明等。同时还提到了窗口销毁的处理方法。本文详细介绍了实现方法并给出了相关注意事项。 ... [详细]
  • Postgresql备份和恢复的方法及命令行操作步骤
    本文介绍了使用Postgresql进行备份和恢复的方法及命令行操作步骤。通过使用pg_dump命令进行备份,pg_restore命令进行恢复,并设置-h localhost选项,可以完成数据的备份和恢复操作。此外,本文还提供了参考链接以获取更多详细信息。 ... [详细]
  • REVERT权限切换的操作步骤和注意事项
    本文介绍了在SQL Server中进行REVERT权限切换的操作步骤和注意事项。首先登录到SQL Server,其中包括一个具有很小权限的普通用户和一个系统管理员角色中的成员。然后通过添加Windows登录到SQL Server,并将其添加到AdventureWorks数据库中的用户列表中。最后通过REVERT命令切换权限。在操作过程中需要注意的是,确保登录名和数据库名的正确性,并遵循安全措施,以防止权限泄露和数据损坏。 ... [详细]
  • MySQL数据库锁机制及其应用(数据库锁的概念)
    本文介绍了MySQL数据库锁机制及其应用。数据库锁是计算机协调多个进程或线程并发访问某一资源的机制,在数据库中,数据是一种供许多用户共享的资源,如何保证数据并发访问的一致性和有效性是数据库必须解决的问题。MySQL的锁机制相对简单,不同的存储引擎支持不同的锁机制,主要包括表级锁、行级锁和页面锁。本文详细介绍了MySQL表级锁的锁模式和特点,以及行级锁和页面锁的特点和应用场景。同时还讨论了锁冲突对数据库并发访问性能的影响。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 怎么在PHP项目中实现一个HTTP断点续传功能发布时间:2021-01-1916:26:06来源:亿速云阅读:96作者:Le ... [详细]
author-avatar
驴友团的新家处_273
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有