README
🚀 @f4ww4z/mcp-mysql-server
这是一个提供MySQL数据库操作的模型上下文协议(Model Context Protocol)服务器,允许AI模型通过标准化接口与MySQL数据库进行交互。
🚀 快速开始
该服务器允许AI模型通过标准化接口与MySQL数据库交互,为数据库操作提供了便捷的途径。
✨ 主要特性
- 安全连接处理,带自动清理功能
- 预编译语句支持以防止SQL注入
- 全面的错误处理和验证
- TypeScript支持
- 自动化连接管理
📦 安装指南
通过Smithery安装
要自动通过Smithery安装MySQL服务器用于Claude Desktop,可使用以下命令:
npx -y @smithery/cli install @f4ww4z/mcp-mysql-server --client claude
手动安装
npx @f4ww4z/mcp-mysql-server
📚 详细文档
配置
该服务器需要在您的MCP配置文件中的环境变量设置以下内容:
{
"mcpServers": {
"mysql": {
"command": "npx",
"args": ["-y", "@f4ww4z/mcp-mysql-server"],
"env": {
"MYSQL_HOST": "your_host",
"MYSQL_USER": "your_user",
"MYSQL_PASSWORD": "your_password",
"MYSQL_DATABASE": "your_database"
}
}
}
}
可用工具
1. connect_db
使用提供的凭据建立MySQL数据库连接。
use_mcp_tool({
server_name: "mysql",
tool_name: "connect_db",
arguments: {
host: "localhost",
user: "your_user",
password: "your_password",
database: "your_database"
}
});
2. query
执行带有可选预编译语句参数的SELECT查询。
use_mcp_tool({
server_name: "mysql",
tool_name: "query",
arguments: {
sql: "SELECT * FROM users WHERE id = ?",
params: [1]
}
});
3. execute
执行带有可选预编译语句参数的INSERT、UPDATE或DELETE查询。
use_mcp_tool({
server_name: "mysql",
tool_name: "execute",
arguments: {
sql: "INSERT INTO users (name, email) VALUES (?, ?)",
params: ["John Doe", "john@example.com"]
}
});
4. list_tables
列出数据库中的所有表。
use_mcp_tool({
server_name: "mysql",
tool_name: "list_tables",
arguments: {}
});
5. describe_table
获取特定表的结构。
use_mcp_tool({
server_name: "mysql",
tool_name: "describe_table",
arguments: {
table: "users"
}
});
安全性
- 使用预编译语句防止SQL注入
- 通过环境变量支持安全密码处理
- 查询执行前进行验证
- 连接使用后自动关闭
错误处理
该服务器为常见问题提供详细的错误消息:
- 连接失败
- 无效查询
- 缺少参数
- 数据库错误
🤝 贡献
欢迎贡献!请随意提交Pull Request到 https://github.com/f4ww4z/mcp-mysql-server
📄 许可证
MIT
Scan to contact