Skip to content

Commit

Permalink
fix: 修复数据库密码中有特殊符号导致db连接不上的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
NanmiCoder committed Dec 26, 2024
1 parent ea5223c commit 29ab6fe
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 24 deletions.
3 changes: 1 addition & 2 deletions config/db_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
RELATION_DB_PWD = os.getenv("RELATION_DB_PWD", "123456")
RELATION_DB_USER = os.getenv("RELATION_DB_USER", "root")
RELATION_DB_HOST = os.getenv("RELATION_DB_HOST", "localhost")
RELATION_DB_PORT = os.getenv("RELATION_DB_PORT", "3306")
RELATION_DB_PORT = os.getenv("RELATION_DB_PORT", 3306)
RELATION_DB_NAME = os.getenv("RELATION_DB_NAME", "media_crawler")

RELATION_DB_URL = f"mysql://{RELATION_DB_USER}:{RELATION_DB_PWD}@{RELATION_DB_HOST}:{RELATION_DB_PORT}/{RELATION_DB_NAME}"

# redis config
REDIS_DB_HOST = "127.0.0.1" # your redis host
Expand Down
27 changes: 5 additions & 22 deletions db.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,19 @@
from var import db_conn_pool_var, media_crawler_db_var


def parse_mysql_url(mysql_url) -> Dict:
"""
从配置文件中解析db链接url,给到aiomysql用,因为aiomysql不支持直接以URL的方式传递链接信息。
Args:
mysql_url: mysql://root:{RELATION_DB_PWD}@localhost:3306/media_crawler
Returns:
"""
parsed_url = urlparse(mysql_url)
db_params = {
'host': parsed_url.hostname,
'port': parsed_url.port or 3306,
'user': parsed_url.username,
'password': parsed_url.password,
'db': parsed_url.path.lstrip('/')
}
return db_params


async def init_mediacrawler_db():
"""
初始化数据库链接池对象,并将该对象塞给media_crawler_db_var上下文变量
Returns:
"""
db_conn_params = parse_mysql_url(config.RELATION_DB_URL)
pool = await aiomysql.create_pool(
host=config.RELATION_DB_HOST,
port=config.RELATION_DB_PORT,
user=config.RELATION_DB_USER,
password=config.RELATION_DB_PWD,
db=config.RELATION_DB_NAME,
autocommit=True,
**db_conn_params
)
async_db_obj = AsyncMysqlDB(pool)

Expand Down

0 comments on commit 29ab6fe

Please sign in to comment.