Enables AI assistants to safely query MySQL databases with support for executing SELECT queries, listing tables, viewing table structures, and optional write operations with configurable safety controls.
MCP MySQL Server
Model Context Protocol (MCP) server for MySQL - 让 AI 助手(Cursor、Claude Desktop)能够安全地查询 MySQL 数据库。
🇨🇳 中文文档
功能特性
✅ 支持 SQL 查询(SELECT、SHOW、DESCRIBE)
✅ 列出数据库所有表
✅ 查看表结构
✅ 安全检查(禁止 DDL 操作)
✅ 可选的写操作控制
✅ 连接池管理
✅ 环境变量配置
快速开始
方法 1:通过 npm 全局安装(推荐)
方法 2:本地开发安装
配置数据库
创建 .env 文件:
测试运行
看到以下输出表示成功:
配置 Cursor AI
方法 1:使用全局安装的包(推荐)
如果你通过 npm install -g 全局安装了包,配置更简单:
Windows 配置:
编辑配置文件:%APPDATA%\Cursor\User\globalStorage\settings.json
macOS/Linux 配置:
编辑配置文件:~/.config/Cursor/User/globalStorage/settings.json
方法 2:使用本地项目路径
如果你是本地开发安装,需要指定完整路径:
Windows 配置:
macOS/Linux 配置:
重启 Cursor
配置完成后,重启 Cursor 使配置生效。
使用示例
配置完成后,在 Cursor 中可以直接询问 AI:
AI 助手会自动调用 MCP 服务查询数据库并返回结果。
可用工具
MCP 服务器提供以下工具:
1. query - 执行 SQL 查询
2. list_tables - 列出所有表
3. describe_table - 查看表结构
安全说明
默认安全策略
✅ 允许:SELECT、SHOW、DESCRIBE(只读查询)
❌ 禁止:INSERT、UPDATE、DELETE(需手动开启)
❌ 禁止:CREATE、DROP、ALTER、TRUNCATE(DDL 操作)
开启写操作
如需允许 AI 执行写操作(谨慎使用),修改 .env:
推荐:创建只读用户
建议为 MCP 服务创建专用的只读数据库用户:
然后修改 .env:
故障排查
1. 连接失败
检查:
MySQL 是否正在运行:
mysql -u root -p.env中的用户名密码是否正确防火墙是否阻止连接
2. Cursor 无法识别
检查:
配置文件路径是否正确(使用绝对路径)
Windows 路径使用双反斜杠
\\是否重启了 Cursor
3. 权限错误
检查:
数据库用户是否有足够权限
是否需要开启
ALLOW_WRITE
开发模式
使用自动重载(修改代码自动重启):
技术栈
Node.js: 22+
@modelcontextprotocol/sdk: MCP 协议实现
mysql2: MySQL 数据库驱动
dotenv: 环境变量管理
相关链接
许可证
MIT License
🇬🇧 English Documentation
Features
✅ Support SQL queries (SELECT, SHOW, DESCRIBE)
✅ List all database tables
✅ View table structure
✅ Safety checks (prohibit DDL operations)
✅ Optional write operation control
✅ Connection pool management
✅ Environment variable configuration
Quick Start
Method 1: Install via npm (Recommended)
Method 2: Local Development
Database Configuration
Create a .env file:
Configure Cursor AI
Edit configuration file and add:
Windows: %APPDATA%\Cursor\User\globalStorage\settings.json
macOS/Linux: ~/.config/Cursor/User/globalStorage/settings.json
Restart Cursor after configuration.
Usage Examples
After configuration, you can ask AI directly in Cursor:
Powered by Model Context Protocol
Built with ❤️ by shuwu Team
This server cannot be installed
hybrid server
The server is able to function both locally and remotely, depending on the configuration or use case.
Enables AI assistants to safely query MySQL databases with read-only access by default, supporting table listing, structure inspection, and SQL queries with optional write operation control.