如何处理数据库连接数满

文章目录

环境

系统平台:

版本:4.1.1

症状

数据库无法访问,根据应用程序提供的错误信息,如下图所示:

问题原因

数据库设置的连接数已满,无法建立新的连接。

可能原因如下:

1.数据库设置连接数过小(常见于数据库安装完后,未修改连接数,使用的默认值100)。

2.应用程序异常占用。

解决方案

一、查询当前数据库连接数设置

sql 复制代码
--管理员用户可以连进数据库时,连入数据库查询(普通用户连接数被占满时,默认保留3个连接给管理员使用)



highgo=# show max_connections;

 max_connections

-----------------

 500

(1 row)



--管理员用户无法连进数据库时,通过查询配置配置文件确认参数值设置(postgresql.auto.conf覆盖postgresql.conf设置值)

[root@localhost ~]# cat $PGDATA/postgresql.conf|grep max_connections

max_connections = 100                   # (change requires restart)

[root@localhost ~]# cat $PGDATA/postgresql.auto.conf|grep max_connections

max_connections = 500

二、数据库设置连接数过小

与客户说明情况,调整连接数需重启数据库,征得客户同意后,适当增大连接数。

sql 复制代码
--管理员用户连进数据库,修改连接数

highgo=# alter system set max_connections = 800;

ALTER SYSTEM



--重启数据库

[root@localhost ~]# pg_ctl restart

waiting for server to shut down.... done

server stopped

waiting for server to start....2023-09-25 10:31:07.064 CST [2334] LOG:  data encryption performed by sm4

2023-09-25 10:31:07.066 CST [2334] LOG:  Password detection module is disabled

2023-09-25 10:31:07.066 CST [2334] LOG:  starting HighGo Security Enterprise Edition Database System 4.5.8 on x86_64,build on 20220708

2023-09-25 10:31:07.066 CST [2334] LOG:  listening on IPv4 address "0.0.0.0", port 5866

2023-09-25 10:31:07.066 CST [2334] LOG:  listening on IPv6 address "::", port 5866

2023-09-25 10:31:07.067 CST [2334] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5866"

2023-09-25 10:31:07.099 CST [2334] LOG:  redirecting log output to logging collector process

2023-09-25 10:31:07.099 CST [2334] HINT:  Future log output will appear in directory "hgdb_log".

 done

server started



--查询连接数

highgo=# show max_connections;

 max_connections

-----------------

 800

(1 row)

管理员用户无法连进数据库时,将postgresql.auto.conf配置文件中的max_connections值修改后,重启数据库。

条件允许时,建议重启数据库前先停止应用程序,重启完成后,再启动应用程序。

三、应用程序异常占用

1.查询占用连接数较多的数据库及用户

sql 复制代码
select now(),state,count(*),usename,datname from pg_stat_activity group by 1,2,4,5 order by count desc;

无法连入数据库查询时,在操作系统层查看进程信息进行判别

ps -ef|grep post

2.联系对应的应用人员释放异常占用连接

应用无法释放时,征得应用人员同意后,可使用管理员用户连入数据库执行如下sql协助其断开连接

sql 复制代码
--断开数据库所有空闲连接

select pg_terminate_backend(pg_stat_activity.pid)

from pg_stat_activity

where datname='数据库名' and pid<>pg_backend_pid() and state='idle';




--断开数据库所有连接

select pg_terminate_backend(pg_stat_activity.pid)

from pg_stat_activity

where datname='数据库名' and pid<>pg_backend_pid();

管理员用户无法连入数据库时,可先在系统层kill几个空闲连接后,再尝试连入,若kill后,应用仍不断重复发起连接,可让应用人员先停止相关应用程序后再进行操作。kill操作示例如下:

sql 复制代码
[root@localhost ~]# ps -ef|grep postgres|grep idle

root       8531   3754  0 13:25 ?        00:00:00 postgres: sysdba highgo [local] idle

root       8650   3754  0 13:32 ?        00:00:00 postgres: sysdba highgo x.x.100.27(52972) idle

root       8653   3754  0 13:32 ?        00:00:00 postgres: sysdba highgo x.x.100.27(52976) idle

root       8656   3754  0 13:32 ?        00:00:00 postgres: sysdba highgo x.x.100.27(52980) idle

[root@localhost ~]# kill 8650

[root@localhost ~]# ps -ef|grep postgres|grep idle

root       8531   3754  0 13:25 ?        00:00:00 postgres: sysdba highgo [local] idle

root       8653   3754  0 13:32 ?        00:00:00 postgres: sysdba highgo x.x.100.27(52976) idle

root       8656   3754  0 13:32 ?        00:00:00 postgres: sysdba highgo x.x.100.27(52980) idle

必要时,征得客户同意后,可通过重启数据库释放连接。

相关推荐
Coder_Boy_3 小时前
技术让开发更轻松的底层矛盾
java·大数据·数据库·人工智能·深度学习
helloworldandy3 小时前
使用Pandas进行数据分析:从数据清洗到可视化
jvm·数据库·python
数据知道5 小时前
PostgreSQL 故障排查:如何找出数据库中最耗时的 SQL 语句
数据库·sql·postgresql
qq_12498707535 小时前
基于SSM的动物保护系统的设计与实现(源码+论文+部署+安装)
java·数据库·spring boot·毕业设计·ssm·计算机毕业设计
枷锁—sha5 小时前
【SRC】SQL注入WAF 绕过应对策略(二)
网络·数据库·python·sql·安全·网络安全
Coder_Boy_5 小时前
基于SpringAI的在线考试系统-考试系统开发流程案例
java·数据库·人工智能·spring boot·后端
Gain_chance5 小时前
35-学习笔记尚硅谷数仓搭建-DWS层最近n日汇总表及历史至今汇总表建表语句
数据库·数据仓库·hive·笔记·学习
此生只爱蛋5 小时前
【Redis】主从复制
数据库·redis
马猴烧酒.5 小时前
【面试八股|JAVA多线程】JAVA多线程常考面试题详解
java·服务器·数据库
天天爱吃肉82186 小时前
跟着创意天才周杰伦学新能源汽车研发测试!3年从工程师到领域专家的成长秘籍!
数据库·python·算法·分类·汽车