39 lines
1.1 KiB
Python
39 lines
1.1 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
MySQL数据库配置
|
|
"""
|
|
|
|
import os
|
|
from typing import Optional
|
|
from dataclasses import dataclass
|
|
|
|
|
|
@dataclass
|
|
class MySQLConfig:
|
|
"""MySQL配置类"""
|
|
host: str = os.getenv("MYSQL_HOST", "cd-cynosdbmysql-grp-7kdd8qe4.sql.tencentcdb.com")
|
|
port: int = int(os.getenv("MYSQL_PORT", "26558"))
|
|
user: str = os.getenv("MYSQL_USER", "root")
|
|
password: str = os.getenv("MYSQL_PASSWORD", "gUjjmQpu6c7V0hMF")
|
|
database: str = os.getenv("MYSQL_DATABASE", "tradingai")
|
|
charset: str = "utf8mb4"
|
|
|
|
@property
|
|
def connection_string(self) -> str:
|
|
"""获取连接字符串"""
|
|
return f"mysql+pymysql://{self.user}:{self.password}@{self.host}:{self.port}/{self.database}?charset={self.charset}"
|
|
|
|
def to_dict(self) -> dict:
|
|
"""转换为字典格式"""
|
|
return {
|
|
'host': self.host,
|
|
'port': self.port,
|
|
'user': self.user,
|
|
'password': self.password,
|
|
'database': self.database,
|
|
'charset': self.charset
|
|
}
|
|
|
|
|
|
# 默认MySQL配置实例
|
|
MYSQL_CONFIG = MySQLConfig() |