Files
aiagent/backend/scripts/create_database.py
2026-01-19 00:09:36 +08:00

48 lines
1.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""
创建数据库脚本
"""
import pymysql
import sys
import os
# 添加项目路径
sys.path.insert(0, '/app')
def create_database():
"""创建数据库"""
# 数据库连接信息(从环境变量或直接配置)
host = 'gz-cynosdbmysql-grp-d26pzce5.sql.tencentcdb.com'
port = 24936
user = 'root'
password = '!Rjb12191'
db_name = 'agent_db'
try:
# 连接到MySQL服务器不指定数据库
conn = pymysql.connect(
host=host,
port=port,
user=user,
password=password
)
cursor = conn.cursor()
# 创建数据库
cursor.execute(f"CREATE DATABASE IF NOT EXISTS {db_name} CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci")
print(f"✅ 数据库 {db_name} 创建成功")
conn.commit()
conn.close()
return True
except Exception as e:
print(f"❌ 创建数据库失败: {e}")
return False
if __name__ == '__main__':
success = create_database()
sys.exit(0 if success else 1)