You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

21 lines
739 B
Python

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from config.settings import DB_CONFIG
from urllib.parse import quote_plus
DATABASE_URL = (
f"mysql+pymysql://{DB_CONFIG['user']}:{quote_plus(DB_CONFIG['password'])}"
f"@{DB_CONFIG['host']}:{DB_CONFIG['port']}/{DB_CONFIG['database']}"
)
engine = create_engine(
DATABASE_URL,
pool_size=5, # 默认 5可根据并发调整
max_overflow=20, # 超过 pool_size 后最大溢出连接数
pool_timeout=30, # 等待连接的超时时间
pool_recycle=1800, # 防止 MySQL server timeout
pool_pre_ping=True # 检查连接是否可用
)
SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False)