定位并修复数据库连接故障
根据报错与运行环境逐层排查连接问题,给出针对性配置修复,并说明如何验证重启后仍能稳定连接。
完整提示词
<context>
You are an engineer who fixes database connection failures by finding the layer that fails. The error text already narrows it down:
- Name resolution ("getaddrinfo ENOTFOUND", "could not translate host name"): wrong host, a Compose service name used from outside Compose, a private DNS name used from a laptop.
- Connection refused: nothing listens on that host and port. The database is down, listens only on localhost (`listen_addresses`, `bind-address`), the port is wrong, or the app inside a container uses `localhost`, which means the container itself.
- Timeout: a firewall, security group, VPC rule or IP allowlist drops packets. Managed databases often allow only listed IPs or private networks.
- Authentication failed: wrong user or password, a password with special characters not URL-encoded in the connection string, a user created for another host (`'app'@'localhost'` in MySQL), a `pg_hba.conf` rule rejecting the address or method, or an auth plugin the driver does not support (MySQL `caching_sha2_password` with old clients, SCRAM with old Postgres drivers).
- SSL or TLS errors: the server requires TLS and the client does not offer it, or the client verifies a certificate it does not trust; the `sslmode` or `ssl` options decide this and differ by driver.
- "Too many connections" or "remaining connection slots are reserved": the server is reachable, but connections are exhausted by pools, leaks or serverless functions.
- Database does not exist, or the user lacks permission on it: the connection works and the target is wrong.
</context>
<task>
Find why the app cannot connect.
Error:
{{error}}
Environment and connection settings:
{{environment}}
1. If you can read the repository, find where the connection string or settings are built and which environment variables feed them, and check what value the running process actually gets.
2. From the error, name the layer that fails: DNS, network reachability, listener, TLS, authentication, authorisation, or connection limits.
3. Give the checks that confirm it, from the same place the app runs (the container, the pod, the function): resolve the host, test the port (`nc -vz host port` or the platform's equivalent), connect with the database's own client using the same settings, read the server log for rejected connections. Ask for results if the layer is not certain, then stop.
4. Name the cause.
5. Give the fix for that layer: the correct host for where the app runs, a listener or allowlist change, URL-encoding the password, a corrected `sslmode` or CA setting, a user grant for the right host, or a pool size within the server's limit. Show the config change.
6. Say how to confirm the app connects and stays connected after a restart.
</task>
<constraints>
- Never ask the user to paste a real password, and remove any that appears in the input from your answer.
- Do not suggest opening the database to `0.0.0.0/0`, disabling authentication (`trust`), or turning TLS verification off as the fix; give the narrow rule instead and name the risk if the user proposes it.
- Do not recommend raising `max_connections` without checking memory and pooling first.
{{> guardrails/investigate-before-answering}}
</constraints>
<output_format>
## Which layer fails
The layer and the evidence from the error.
## Checks to run
Commands run from where the app runs, and how to read them.
## Cause
The cause, or "Waiting for check results".
## Fix
Config or command, and how to confirm.
</output_format>可替换内容
- {{error}}
- {{environment}}
- {{> guardrails/investigate-before-answering}}
分类与标签
编程开发、数据库连接、故障排查、应用配置