server refused TLS connection ***.***.***.***:15432 (***.***.***.***): server er...
错误信息
status_code=500, status_code=500, failed to connect to `user=root database=new-api`:
***.***.***.***:15432 (***.***.***.***): tls error: server refused TLS connection
***.***.***.***:15432 (***.***.***.***): server error: FATAL: sorry, too many clients already (SQLSTATE 53300) (request id: 202607090645193561206258268d9d6KgWpcVjy)
AI 解决方案
1. 🌐 错误翻译
原文:status_code=500, status_code=500, failed to connect to `user=root database=new-api`:
***.***.***.***:15432 (***.***.***.***): tls error: server refused TLS connection
***.***.***.***:15432 (***.***.***.***): server error: FATAL: sorry, too many clients already (SQLSTATE 53300) (request id: 202607090645193561206258268d9d6KgWpcVjy)
译文:状态码=500,状态码=500,连接 `user=root database=new-api` 失败:
***.***.***.***:15432 (***.***.***.***):TLS错误:服务器拒绝TLS连接
***.***.***.***:15432 (***.***.***.***):服务器错误:致命错误,客户端连接数过多 (SQLSTATE 53300) (请求ID:202607090645193561206258268d9d6KgWpcVjy)
2. 🎯 原因分析
该报错是双重问题叠加导致:
too many clients already,是导致500状态码的直接原因。3. 💡 解决方案
需分两步解决问题:先解决TLS配置问题,再扩展连接容量,以下是具体操作步骤:
(1)修复TLS连接拒绝问题
调整客户端的TLS配置,确保与服务器的TLS要求一致。若服务器要求加密连接,需配置正确的TLS证书;若服务器关闭TLS,则需在客户端关闭TLS。
以下是不同场景的修复代码示例,选择对应场景执行:
#### 场景1:需要关闭客户端TLS(服务器未开启TLS)
# 以Python psycopg2为例,添加sslmode=disable关闭TLS
import psycopg2
try:
conn = psycopg2.connect(
host="***.***.***.***",
port=15432,
user="root",
database="new-api",
# 核心修复:关闭TLS加密,匹配服务器配置
sslmode="disable"
)
print("连接成功")
except Exception as e:
print(f"连接失败:{e}")
#### 场景2:需要配置TLS证书(服务器开启TLS,且要求客户端证书)
# 以Python psycopg2为例,指定证书路径
import psycopg2
try:
conn = psycopg2.connect(
host="***.***.***.***",
port=15432,
user="root",
database="new-api",
# 核心修复:配置TLS证书,匹配服务器加密要求
sslmode="verify-full",
sslcert="/path/to/client-cert.pem",
sslkey="/path/to/client-key.pem",
sslrootcert="/path/to/ca-cert.pem"
)
print("连接成功")
except Exception as e:
print(f"连接失败:{e}")
(2)解决“客户端连接数过多”问题
需由数据库管理员调整数据库的最大连接数配置,以下是PostgreSQL的调整方法:
-- 1. 临时调整最大连接数(重启数据库后失效)
ALTER SYSTEM SET max_connections = 200;
-- 2. 永久生效,需重启数据库
-- 在postgresql.conf配置文件中修改:
max_connections = 200
-- 3. 重启数据库使配置生效(具体命令按部署环境执行,如systemctl restart postgresql)
4. 🛡️ 预防措施
max_connections使用率、活跃连接数,设置阈值告警,提前发现连接数不足问题。