连接 Refused
错误信息
连接 refused
快速摘要
数据库服务器是 不运行, 不 listening on expected port, or 不接受连接从 你的 host.
为什么发生
数据库服务器是 不运行
服务器是 listening on 不同 port than 配置
firewall 是 blocking 连接
host 地址是 错误 (e.g., localhost 对比 remote 服务器)
服务器有 reached 它的 max_connections 限制
最小示例
✗ 错误代码
# Connecting to a server that is not running
psql -h localhost -p 5432 -U postgres
# → connection refused✓ 修复代码
# Start the server first, then connect
pg_ctl start
psql -h localhost -p 5432 -U postgres服务器是 不运行. 开始它 首先允许连接到 succeed.
如何诊断
检查如果数据库 service 是运行:
pg_isreadyfor PostgreSQL ormysqladmin pingfor MySQL验证 host and port in 你的连接字符串
检查 firewall 规则 and network connectivity
检查服务器's max_connections and current 连接数量
如何修复
开始数据库服务器
更新你的连接字符串使用正确 host and port
打开必需 port in firewall
Increase max_connections or 关闭 idle 连接
如何预防
Use 连接 pooling 使用 health 检查
Monitor 数据库服务器 uptime and alert on downtime
Keep 连接配置 in 环境变量, 不 hardcoded
相关资源
相关课程
- 连接 Pooling
学习关于数据库连接.