一个功能齐全的MySQL模型上下文协议(MCP)服务器,提供数据库管理功能。
net start mysql80
git clone https://github.com/blackdesert5410/mysql-mcp-server.git
cd mysql-mcp-server
npm install
启动时通过命令行参数传递MySQL用户名和密码:
node src/index.js <用户名> <密码>
示例
node src/index.js root your_password
cp config.example.env .env
.env文件,填写你的MySQL配置:MYSQL_HOST=localhost
MYSQL_PORT=3306
MYSQL_USER=your_username
MYSQL_PASSWORD=your_password
MYSQL_DATABASE=
node src/index.js
优先级:命令行参数 > 环境变量 > 默认值
# 使用命令行参数
node src/index.js <用户名> <密码>
# 或使用 npm start(需要先配置环境变量)
npm start
打开光标的MCP配置文件(通常在~/.cursor/mcp.json或%APPDATA%\Cursor\mcp.json)
添加以下配置:
Windows路径格式(建议使用正斜杠):
{
"mcpServers": {
"mysql": {
"command": "node",
"args": [
"D:/AI/mcp-server/src/index.js",
"your_username",
"your_password"
]
}
}
}
或者使用双反斜杠:
{
"mcpServers": {
"mysql": {
"command": "node",
"args": [
"D:\\AI\\mcp-server\\src\\index.js",
"your_username",
"your_password"
]
}
}
}
如果Node.js不在PATH中,请使用完整路径:
{
"mcpServers": {
"mysql": {
"command": "C:/Program Files/nodejs/node.exe",
"args": [
"D:/AI/mcp-server/src/index.js",
"your_username",
"your_password"
]
}
}
}
注意:
your_username和your_password替换为你的MySQL用户名和密码/或双反斜杠\\{
"mcpServers": {
"mysql": {
"command": "node",
"args": [
"/path/to/mysql-mcp-server/src/index.js",
"your_username",
"your_password"
]
}
}
}
MCP服务完全支持代理将自然语言转换为SQL查询。通过以下工具,代理可以:
理解数据库结构:
get_database_schema获取整个数据库的完整结构get_table_schema获取特定表的详细列信息get_foreign_keys理解表之间的关联理解数据内容:
get_sample_data查看样本数据,了解数据格式和内容get_table_info了解表的统计信息生成并执行SQL:
execute_sql执行生成的SQLselect、insert、update、delete工具当用户说“查找所有年龄超过25岁的用户”时,代理可以:
get_database_schema或list_tables找到用户表get_table_schema查看用户表的结构,并确认年龄字段的名称(例如age)get_sample_data查看样本数据,了解数据格式SELECT * FROM users WHERE age > 25execute_sql执行查询并返回结果未连接"错误
net start mysql80)连接失败
路由问题
/或双反斜杠\\依赖问题
npm install确保所有依赖项均已安装更多故障排除信息,请参阅TROUBLESHOOTING.md
net start mysql80)欢迎提交Issue和Pull Requests!