Files
012-kaopeilian/backend/start_remote.py
111 998211c483 feat: 初始化考培练系统项目
- 从服务器拉取完整代码
- 按框架规范整理项目结构
- 配置 Drone CI 测试环境部署
- 包含后端(FastAPI)、前端(Vue3)、管理端

技术栈: Vue3 + TypeScript + FastAPI + MySQL
2026-01-24 19:33:28 +08:00

101 lines
2.9 KiB
Python

#!/usr/bin/env python3
"""
使用公网数据库的启动脚本
"""
import os
import sys
import asyncio
from pathlib import Path
# 设置环境变量 - 使用公网MySQL数据库
# 数据库信息:
# 主机: 120.79.247.16 或 aiedu.ireborn.com.cn
# 端口: 3306
# 数据库名: kaopeilian
# 用户: root
# 密码: Kaopeilian2025!@# (URL编码后: Kaopeilian2025%21%40%23)
os.environ["DATABASE_URL"] = "mysql+aiomysql://root:Kaopeilian2025%21%40%23@120.79.247.16:3306/kaopeilian?charset=utf8mb4"
os.environ["SECRET_KEY"] = "dev-secret-key-for-testing-only-not-for-production"
os.environ["DEBUG"] = "true"
os.environ["LOG_LEVEL"] = "INFO"
os.environ["LOG_FORMAT"] = "console"
os.environ["REDIS_URL"] = "redis://localhost:6379/0"
# 添加项目根目录到Python路径
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))
async def test_connection():
"""测试数据库连接"""
try:
from app.config.database import engine
from sqlalchemy import text
print("📡 正在连接到公网数据库...")
async with engine.begin() as conn:
result = await conn.execute(text("SELECT VERSION()"))
version = result.scalar()
print(f"✅ 成功连接到MySQL: {version}")
# 检查表数量
result = await conn.execute(text("SHOW TABLES"))
tables = result.fetchall()
print(f"✅ 数据库中有 {len(tables)} 个表")
await engine.dispose()
return True
except Exception as e:
print(f"❌ 数据库连接失败: {e}")
return False
async def main():
"""主函数"""
# 测试数据库连接
if not await test_connection():
print("\n⚠️ 请检查:")
print("1. 网络连接是否正常")
print("2. 数据库服务器是否可访问")
print("3. 用户名密码是否正确")
return
print("\n🚀 启动应用服务器...")
print("访问地址: http://localhost:8000")
print("API文档: http://localhost:8000/docs")
print("\n按 Ctrl+C 停止服务器")
# 导入并运行应用
import uvicorn
uvicorn.run(
"app.main:app",
host="0.0.0.0",
port=8000,
reload=True,
log_config={
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"default": {
"format": "%(asctime)s | %(levelname)s | %(name)s | %(message)s",
},
},
"handlers": {
"default": {
"formatter": "default",
"class": "logging.StreamHandler",
"stream": "ext://sys.stdout",
},
},
"root": {
"level": "INFO",
"handlers": ["default"],
},
}
)
if __name__ == "__main__":
asyncio.run(main())