Files
aiagent/数据库初始化说明.md
2026-01-19 00:09:36 +08:00

63 lines
1.5 KiB
Markdown
Raw Permalink 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.
# 数据库初始化说明
## 问题
注册失败的原因是数据库 `agent_db` 不存在。
## 解决方案
### 方法一使用MySQL客户端创建数据库推荐
连接到腾讯云MySQL数据库执行以下SQL
```sql
CREATE DATABASE IF NOT EXISTS agent_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
```
### 方法二:使用命令行工具
如果你有MySQL客户端工具可以执行
```bash
mysql -h gz-cynosdbmysql-grp-d26pzce5.sql.tencentcdb.com -P 24936 -u root -p
# 输入密码: !Rjb12191
# 然后执行
CREATE DATABASE IF NOT EXISTS agent_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
```
### 方法三使用Python脚本创建
```python
import pymysql
# 连接MySQL不指定数据库
conn = pymysql.connect(
host='gz-cynosdbmysql-grp-d26pzce5.sql.tencentcdb.com',
port=24936,
user='root',
password='!Rjb12191'
)
cursor = conn.cursor()
cursor.execute("CREATE DATABASE IF NOT EXISTS agent_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci")
conn.close()
print("数据库创建成功")
```
## 创建表结构
数据库创建后表结构会在首次运行时自动创建通过SQLAlchemy或者你可以手动执行
```bash
# 进入后端容器
docker-compose -f docker-compose.dev.yml exec backend bash
# 运行初始化脚本
python -c "from app.core.database import init_db; init_db(); print('表创建成功')"
```
## 验证
数据库和表创建成功后,重新尝试注册用户即可。