From 4a46e7d2f52e2202c88c8413454432ef49aa5c5d Mon Sep 17 00:00:00 2001 From: chenqi Date: Sat, 28 Feb 2026 17:53:30 +0800 Subject: [PATCH] =?UTF-8?q?fix(backend):=20=E4=BF=AE=E6=AD=A3=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E6=95=B0=E6=8D=AE=E5=BA=93=E7=94=A8=E6=88=B7=E5=90=8D?= =?UTF-8?q?=E5=8F=8A=E8=B0=83=E8=AF=95=E6=A8=A1=E5=BC=8F=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将默认数据库用户名从 your_user 改为 postgresql,明确用户名字段说明 - 修改 .env.example 中 DEBUG 默认为 True,方便本地调试 - 更新 deploy-docker-compose.sh 脚本中 DEBUG 默认值为 True,保持一致性 - 保持数据库连接字符串格式正确,确保环境变量配置一致 --- backend/.env.example | 5 ++- backend/test_db_connection.py | 77 +++++++++++++++++++++++++++++++++++ spug/deploy-docker-compose.sh | 2 +- 3 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 backend/test_db_connection.py diff --git a/backend/.env.example b/backend/.env.example index fecc8b3..0219fb4 100644 --- a/backend/.env.example +++ b/backend/.env.example @@ -3,10 +3,11 @@ # 数据库配置 (PostgreSQL) # 生产环境请修改为实际的数据库连接信息 # 格式: postgresql+asyncpg://用户名:密码@主机:端口/数据库名 -DATABASE_URL=postgresql+asyncpg://your_user:your_password@your_host:15432/hospital_performance +# 注意: 用户名是 postgresql 不是 postgres +DATABASE_URL=postgresql+asyncpg://postgresql:your_password@your_host:15432/hospital_performance # JWT 配置 SECRET_KEY=your-secret-key-change-in-production-min-32-characters # 调试模式 -DEBUG=False +DEBUG=True diff --git a/backend/test_db_connection.py b/backend/test_db_connection.py new file mode 100644 index 0000000..ff337aa --- /dev/null +++ b/backend/test_db_connection.py @@ -0,0 +1,77 @@ +""" +数据库连接测试脚本 +""" +import asyncio +import os +import sys +from pathlib import Path + +# 添加项目根目录到路径 +sys.path.insert(0, str(Path(__file__).parent)) + +from sqlalchemy import text +from sqlalchemy.ext.asyncio import create_async_engine + + +async def test_connection(): + """测试数据库连接""" + print("=" * 50) + print("数据库连接诊断") + print("=" * 50) + + # 1. 显示当前工作目录 + print(f"\n1. 当前工作目录: {os.getcwd()}") + + # 2. 检查 .env 文件 + env_path = Path(__file__).parent / ".env" + print(f"2. .env 文件路径: {env_path}") + print(f" .env 文件存在: {env_path.exists()}") + + if env_path.exists(): + print(f" .env 文件内容:") + with open(env_path) as f: + for line in f: + if "PASSWORD" in line.upper() or "SECRET" in line.upper(): + # 隐藏敏感信息 + print(f" {line.strip()[:30]}***") + else: + print(f" {line.strip()}") + + # 3. 加载配置 + print("\n3. 加载配置:") + from app.core.config import settings + print(f" DATABASE_URL: {settings.DATABASE_URL[:50]}***") + print(f" DEBUG: {settings.DEBUG}") + + # 4. 测试数据库连接 + print("\n4. 测试数据库连接:") + try: + engine = create_async_engine(settings.DATABASE_URL, echo=True) + async with engine.connect() as conn: + result = await conn.execute(text("SELECT 1")) + print(" ✅ 数据库连接成功!") + await engine.dispose() + except Exception as e: + print(f" ❌ 数据库连接失败: {type(e).__name__}: {e}") + + # 5. 测试其他可能的连接字符串 + print("\n5. 测试备选连接字符串:") + test_urls = [ + "postgresql+asyncpg://postgresql:Jchl1528@192.168.110.252:15432/hospital_performance", + "postgresql+asyncpg://postgres:Jchl1528@192.168.110.252:15432/hospital_performance", + ] + + for url in test_urls: + try: + engine = create_async_engine(url) + async with engine.connect() as conn: + result = await conn.execute(text("SELECT 1")) + print(f" ✅ 连接成功: {url[:40]}***") + await engine.dispose() + break + except Exception as e: + print(f" ❌ 失败: {str(e)[:50]}") + + +if __name__ == "__main__": + asyncio.run(test_connection()) diff --git a/spug/deploy-docker-compose.sh b/spug/deploy-docker-compose.sh index 3791566..8dcdf78 100644 --- a/spug/deploy-docker-compose.sh +++ b/spug/deploy-docker-compose.sh @@ -21,7 +21,7 @@ export DATABASE_USER="${DATABASE_USER:-postgresql}" export DATABASE_PASSWORD="${DATABASE_PASSWORD:-Jchl1528}" export DATABASE_NAME="${DATABASE_NAME:-hospital_performance}" export SECRET_KEY="${SECRET_KEY:-change-this-secret-key-in-production}" -export DEBUG="${DEBUG:-False}" +export DEBUG="${DEBUG:-True}" # 构建完整的 DATABASE_URL(后端使用此变量) export DATABASE_URL="postgresql+asyncpg://${DATABASE_USER}:${DATABASE_PASSWORD}@${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_NAME}"