第一次提交
This commit is contained in:
47
backend/scripts/create_database.py
Normal file
47
backend/scripts/create_database.py
Normal file
@@ -0,0 +1,47 @@
|
||||
#!/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)
|
||||
Reference in New Issue
Block a user