MySQL MCP Server
About
A MySQL database server for AI assistants, enabling full CRUD operations, transaction management, and intelligent rollback.
Details
- Author
- guangxiangdebizi
- Categories
- Database, Other
Jump to
Setup
Install MySQL MCP Server in your MCP client (Claude Desktop, Cursor, Windsurf, and others).
Repository: https://github.com/guangxiangdebizi/MySQL_MCP
Follow the installation instructions in the repository README, then restart your MCP client.
一个功能强大且易用的 MySQL 数据库 MCP(Model Context Protocol)服务器,支持 AI 助手安全地操作 MySQL 数据库。
- 🌐StreamableHTTP 协议- 基于最新 MCP 规范实现
- 🔐Header 预配置- 凭证不暴露给 AI,安全可靠
- 🤖AI 动态管理- AI 可以帮你添加/切换数据库连接
- 🔗多数据库支持- 同时管理多个数据库,随时切换
- 📊完整 CRUD- 支持所有 SQL 操作
- 🏗️模块化架构- 清晰的目录结构,易于扩展
- Node.js 18+
- MySQL 5.7+ 或 8.0+
- MCP 客户端(Claude Desktop、Cursor 等)
# 克隆项目 git clone https://github.com/guangxiangdebizi/MySQL_MCP.git cd MySQL_MCP # 安装依赖 npm install # 编译 npm run build # 启动服务器 npm start
Windows:%APPDATA%\Claude\claude_desktop_config.json
macOS:~/Library/Application Support/Claude/claude_desktop_config.json
Linux:~/.config/Claude/claude_desktop_config.json
{ "mcpServers": { "mysql-mcp": { "type": "streamableHttp", "url": "http://localhost:3001/mcp", "timeout": 600, "headers": { "X-MySQL-Host": "localhost", "X-MySQL-Port": "3306", "X-MySQL-User": "root", "X-MySQL-Password": "your_password", "X-MySQL-Database": "your_database" } } } }
{ "mcpServers": { "mysql-mcp": { "type": "streamableHttp", "url": "http://localhost:3001/mcp", "timeout": 600, "headers": { "X-MySQL-Host-1": "prod.mysql.com", "X-MySQL-User-1": "prod_user", "X-MySQL-Password-1": "prod_pass", "X-MySQL-Database-1": "production", "X-MySQL-Host-2": "test.mysql.com", "X-MySQL-User-2": "test_user", "X-MySQL-Password-2": "test_pass", "X-MySQL-Database-2": "testing" } } } }
- ✅ 数据库凭证不暴露给 AI
- ✅ 启动即连接,无需手动操作
- ✅ 支持多数据库同时连接
{ "mcpServers": { "mysql-mcp": { "type": "streamableHttp", "url": "http://localhost:3001/mcp", "timeout": 600 } } }
你: 帮我连接到本地 MySQL,用户名 root,密码 123456,数据库 mydb AI: [调用 add_connection 工具]
你: 显示所有表 AI: [调用 show_tables] 📊 数据库表列表 (共 5 个表) 1. users 2. orders 3. products ... 你: 查看 users 表的结构 AI: [调用 describe_table,参数:users] 📋 表结构: users 字段信息: - id (INT, 主键) - name (VARCHAR) - email (VARCHAR) ...
你: 帮我连接两个数据库: 1. 生产库:prod.mysql.com,用户 admin,密码 xxx,数据库 shop 2. 测试库:test.mysql.com,用户 tester,密码 yyy,数据库 shop_test AI: [调用 add_connection,参数:id=prod, host=prod.mysql.com...] [调用 add_connection,参数:id=test, host=test.mysql.com...] ✅ 两个数据库连接已添加 你: 列出所有连接 AI: [调用 list_connections] 📊 当前数据库连接列表 (共 2 个) 🟢 [1] prod └─ prod.mysql.com:3306/shop └─ ✅ 当前活跃连接 ⚪ [2] test └─ test.mysql.com:3306/shop_test 你: 切换到测试库 AI: [调用 select_database,参数:test] ✅ 已选择数据库: test 你: 查询用户表前 10 条 AI: [调用 execute_query,SQL: SELECT FROM users LIMIT 10] ✅ 查询成功,返回 10 行数据 [显示 JSON 格式数据]
MySQL_MCP/ ├── src/ │ ├── index.ts # 主入口(HTTP Server + 会话管理) │ ├── database.ts # 数据库连接管理器 │ └── tools/ # 工具模块 │ ├── index.ts # 工具统一导出和路由 │ ├── connection.ts # 连接管理工具 │ └── query.ts # 查询工具 ├── dist/ # 编译后的 JS 文件 ├── package.json ├── tsconfig.json └── README.md
- index.ts- Express HTTP 服务器 + MCP Server 初始化
- database.ts- 封装数据库连接池和查询逻辑
- tools/- 每个文件负责一类工具的定义和处理
-- 创建专用用户 CREATE USER 'mcp_user'@'%' IDENTIFIED BY 'strong_password'; -- 授予必要权限 GRANT SELECT, INSERT, UPDATE, DELETE ON your_database. TO 'mcp_user'@'%'; -- 生产环境只读用户 GRANT SELECT ON your_database.* TO 'mcp_readonly'@'%';
- ✅ 使用 Header 预配置,避免凭证暴露给 AI
- ✅ 生产环境使用 HTTPS(Nginx 反向代理)
- ✅ 限制访问 IP(防火墙规则)
- ✅ 定期更新数据库密码
- ✅ 监控日志,发现异常访问
# 开发模式(TypeScript 直接运行) npm run dev # 编译 npm run build # 生产模式(运行编译后的 JS) npm start # 全局安装 npm run install-global
# HTTP 服务器端口 PORT=3001 # Node 环境 NODE_ENV=production
错误:EADDRINUSE: address already in use :::3001
# Windows netstat -ano | findstr :3001 taskkill /F /PID <PID> # Linux/Mac lsof -ti:3001 | xargs kill -9
问题:重启服务器后提示 "Session not found"
错误:Can't add new command when connection is in closed state
- ✅ v4.0.5+ 已使用连接池替代单连接,自动支持:
- 连接保活(Keep-Alive)机制
- 自动重连功能
- 并发查询支持
- 🎯 使用连接池替代单连接
- 🔄 自动连接保活(Keep-Alive)
- 🔌 自动重连机制
- 🚀 支持并发查询
- 🐛 修复 "connection in closed state" 错误
- 🔥 完全重写,全新模块化架构
- ✨ 基于最新 MCP StreamableHTTP 协议
- 🎯 简化工具:连接管理 + 查询操作
- 🏗️ 清晰的目录结构:tools/模块化
- 🚀 更快的响应速度
- 📖 更清晰的代码注释
- 🐛问题报告:GitHub Issues
- 💡功能建议:GitHub Discussions
- 📧联系作者:guangxiangdebizi@gmail.com
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '...@modelcontextprotocol/sdk...'
# 删除旧依赖 rm -rf node_modules package-lock.json # Linux/Mac # 或 rmdir /s /q node_modules && del package-lock.json # Windows # 清理缓存 npm cache clean --force # 重新安装 npm install
npm install -g @xingyuchen/mysql-mcp-server@latest
SSE stream disconnected: TypeError: terminated
{ "mysql-mcp-http": { "type": "streamableHttp", "url": "http://localhost:3002/mcp", "timeout": 0, // 0 表示无超时限制 "headers": { ... } } }
# 自动修复 npm audit fix # 如果还有问题,强制修复 npm audit fix --force # 重新构建 npm run build
Error: listen EADDRINUSE: address already in use :::3002
# Windows netstat -ano | findstr :3002 taskkill /PID <进程ID> /F # Linux/Mac lsof -i :3002 kill -9 <进程ID>
Multi-database agent access (PostgreSQL, SQLite, MySQL, Oracle, SQL Server) with batch queries, pre-configured connections, and SQLGlot-enforced read-only safety
Production safe MCP server that gives AI agents direct access to MySQL databases.
MCP server for MySQL/MariaDB — schema inspection, queries, and data manipulation for AI assistants
Provides AI agents with direct access to query, search, and analyze MySQL databases.
A read-only MySQL database server for LLMs to inspect schemas and execute queries.
Provides direct access to MySQL databases, allowing AI agents to execute SQL queries and manage database content.
Provides access to a MySQL database, allowing agents to execute SQL queries.
Provides tools for AI assistants to interact with a MySQL database.
Provides read-only access to MySQL databases, allowing LLMs to inspect schemas and execute queries.
A read-only MCP server for MySQL, enabling LLMs to query live data using the CData JDBC Driver.
Sign in to leave a review
Use Google, GitHub, or an email account so ratings stay tied to real people.
No reviews posted yet.





