🧠 使用 Cursor 调用 executeautomation/mcp-database-server(适用于 Windows)
本指南将教你如何在 Windows 上通过 MCP 协议,让 Cursor 编辑器调用数据库查询服务 executeautomation/mcp-database-server
。
🧰 环境准备
你需要准备:
- ✅ 安装 Node.js(推荐 LTS 版本)
- ✅ 安装并配置 Cursor 编辑器
- ✅ 本地或远程数据库(支持 PostgreSQL、MySQL、SQLite、SQL Server)
🛠️ 启动 MCP 数据库服务
以 PostgreSQL 为例,打开 PowerShell 或 CMD 终端,执行以下命令(替换其中内容):
bash
npx @executeautomation/database-server --postgresql ^
--host localhost ^
--port 5432 ^
--database your_database_name ^
--user your_db_user ^
--password your_db_password
🔁 说明:
参数 | 含义 |
---|---|
--postgresql |
指定数据库类型为 PostgreSQL |
--host |
数据库地址 |
--port |
端口(PostgreSQL 默认 5432) |
--database |
数据库名 |
--user |
用户名 |
--password |
密码 |
👉 如果你使用 MySQL,只需将 --postgresql
改为 --mysql
,并设置对应端口(默认 3306)。
⚙️ 配置 Cursor 使用 MCP 工具
步骤 1:打开配置文件
打开配置文件路径(Windows):
C:\Users\你的用户名\.cursor\mcp.json
如果文件不存在,可手动创建。
步骤 2:添加配置内容
json
{
"mcpServers": {
"mydb": {
"command": "npx",
"args": [
"@executeautomation/database-server",
"--postgresql",
"--host", "localhost",
"--port", "5432",
"--database", "your_database_name",
"--user", "your_db_user",
"--password", "your_db_password"
]
}
}
}
📝 替换其中参数为你自己的数据库连接信息。
步骤 3:重启 Cursor
重启 Cursor 编辑器,它会自动加载你配置的 MCP 工具。
💬 在 Cursor 中使用数据库 MCP 工具
你现在可以直接在 Cursor 中和数据库"聊天":
示例 1:列出所有表
text
list_tables()
示例 2:查询订单前 5 条
text
read_query("SELECT * FROM orders LIMIT 5")
示例 3:查看表结构
text
describe_table("customers")
示例 4:更新数据
text
write_query("UPDATE orders SET status = 'shipped' WHERE id = 1001")
🔐 注意:生产环境建议使用只读账号,避免误操作。
✅ 效果示例对话
你:列出所有表名
AI:调用 list_tables() 返回 ["orders", "customers", "products"]
你:查询 orders 表最近 5 条记录
AI:调用 read_query("SELECT * FROM orders ORDER BY order_date DESC LIMIT 5")
📌 常见问题与建议
问题/需求 | 解决方案 |
---|---|
Cursor 工具没启动 | 检查 mcp.json 是否正确、是否重启 Cursor |
查询失败或连接拒绝 | 检查数据库连接信息、用户权限是否正确 |
数据库是远程连接 | 将 host 改为远程 IP,并开放端口 |
想连接其他数据库类型 | 替换 --postgresql 为 --mysql 等 |
🧩 支持的数据库类型(可选参数)
数据库类型 | 启动参数 |
---|---|
PostgreSQL | --postgresql |
MySQL | --mysql |
SQLite | --sqlite |
SQL Server | --mssql |
📦 可选:npm 安装方式(一次安装,多次使用)
bash
npm install -g @executeautomation/database-server
然后你可以直接运行:
bash
database-server --postgresql --host ...(同上参数)
如果你还想添加更多数据库 MCP 工具(如 MongoDB、Oracle、ClickHouse),可以配合其它 MCP 服务组合使用。