MCP MySQL 服务器
一个支持DDL操作、权限控制和操作日志的MCP MySQL服务器。
功能特性
- ✅ 支持SQL查询执行(DDL和DML)
- ✅ 数据库信息获取
- ✅ 操作日志记录
- ✅ 连接池管理
- ✅ 自动重连机制
- ✅ 健康检查
- ✅ 错误处理和恢复





安装
全局安装(推荐)
npm install -g @liangshanli/mcp-server-mysql
本地安装
npm install @liangshanli/mcp-server-mysql
从源码安装
git clone https://github.com/liliangshan/mcp-server-mysql.git
cd mcp-server-mysql
npm install
配置
设置环境变量:
export MYSQL_HOST=localhost
export MYSQL_PORT=3306
export MYSQL_USER=root
export MYSQL_PASSWORD=your_password
export MYSQL_DATABASE=your_database
export ALLOW_DDL=true
export ALLOW_DROP=false
export ALLOW_DELETE=false
使用方法
1. 全局安装后直接运行
mcp-server-mysql
2. 使用 npx 运行(推荐)
npx @liangshanli/mcp-server-mysql
3. 直接启动(源码安装)
npm start
4. 托管启动(推荐用于生产环境)
npm run start-managed
托管启动提供以下功能:
- 自动重启(最多10次)
- 错误恢复
- 进程管理
- 日志记录
5. 开发模式
npm run dev
编辑器集成
Cursor 编辑器配置
- 在项目根目录创建
.cursor/mcp.json文件:
{
"mcpServers": {
"mysql": {
"command": "npx",
"args": ["@liangshanli/mcp-server-mysql"],
"env": {
"MYSQL_HOST": "your_host",
"MYSQL_PORT": "3306",
"MYSQL_USER": "your_user",
"MYSQL_PASSWORD": "your_password",
"MYSQL_DATABASE": "your_database",
"ALLOW_DDL": "false",
"ALLOW_DROP": "false",
"ALLOW_DELETE": "false"
}
}
}
}
VS Code 配置
- 安装 VS Code 的 MCP 扩展
- 创建
.vscode/settings.json文件:
{
"mcp.servers": {
"mysql": {
"command": "npx",
"args": ["@liangshanli/mcp-server-mysql"],
"env": {
"MYSQL_HOST": "your_host",
"MYSQL_PORT": "3306",
"MYSQL_USER": "your_user",
"MYSQL_PASSWORD": "your_password",
"MYSQL_DATABASE": "your_database",
"ALLOW_DDL": "false",
"ALLOW_DROP": "false",
"ALLOW_DELETE": "false"
}
}
}
}
作为MCP服务器
服务器启动后,通过stdin/stdout与MCP客户端通信:
{"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": "2025-06-18"}}
可用工具
-
sql_query: 执行SQL查询
{ "jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": { "name": "sql_query", "arguments": { "sql": "SELECT * FROM users LIMIT 10" } } } -
get_database_info: 获取数据库信息
{ "jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": { "name": "get_database_info", "arguments": {} } } -
get_operation_logs: 获取操作日志
{ "jsonrpc": "2.0", "id": 4, "method": "tools/call", "params": { "name": "get_operation_logs", "arguments": { "limit": 50, "offset": 0 } } }
连接池特性
- 自动创建: 在
notifications/initialized时自动创建连接池 - 健康检查: 每5分钟检查连接池状态
- 自动重连: 连接池失效时自动重新创建
- 连接复用: 使用连接池提高性能
- 优雅关闭: 服务器关闭时正确释放连接
日志
日志文件位置:./logs/mcp-mysql.log
记录内容:
- 所有请求和响应
- SQL操作记录
- 错误信息
- 连接池状态变化
错误处理
- 单个请求错误不会影响整个服务器
- 连接池错误会自动恢复
- 进程异常会自动重启(托管模式)
环境变量
| 变量名 | 默认值 | 说明 |
|---|---|---|
| MYSQL_HOST | localhost | MySQL主机地址 |
| MYSQL_PORT | 3306 | MySQL端口 |
| MYSQL_USER | root | MySQL用户名 |
| MYSQL_PASSWORD | MySQL密码 | |
| MYSQL_DATABASE | 数据库名 | |
| ALLOW_DDL | false | 是否允许DDL操作(CREATE、ALTER、TRUNCATE、RENAME、COMMENT)。设置为’true’启用 |
| ALLOW_DROP | false | 是否允许DROP操作。设置为’true’启用 |
| ALLOW_DELETE | false | 是否允许DELETE操作。设置为’true’启用 |
| MCP_LOG_DIR | ./logs | 日志目录 |
| MCP_LOG_FILE | mcp-mysql.log | 日志文件名 |
开发
项目结构
mcpmysql/
├── src/
│ └── server-final.js # 主服务器文件
├── start-server.js # 托管启动脚本
├── package.json
└── README.md
测试
npm test
快速开始
1. 安装包
npm install -g @liangshanli/mcp-server-mysql
2. 配置环境变量
export MYSQL_HOST=localhost
export MYSQL_PORT=3306
export MYSQL_USER=root
export MYSQL_PASSWORD=your_password
export MYSQL_DATABASE=your_database
export ALLOW_DDL=false
export ALLOW_DROP=false
export ALLOW_DELETE=false
权限控制示例:
# 默认:禁用所有破坏性操作(安全模式)
export ALLOW_DDL=false
export ALLOW_DROP=false
export ALLOW_DELETE=false
# 允许DDL但禁用DROP和DELETE
export ALLOW_DDL=true
export ALLOW_DROP=false
export ALLOW_DELETE=false
# 允许所有操作但禁用DELETE
export ALLOW_DDL=true
export ALLOW_DROP=true
export ALLOW_DELETE=false
# 启用所有操作(谨慎使用)
export ALLOW_DDL=true
export ALLOW_DROP=true
export ALLOW_DELETE=true
3. 运行服务器
mcp-server-mysql
许可证
MIT
MCP MySQL Server
A MCP MySQL server with DDL support, permission control and operation logs.
Features
- ✅ SQL query execution (DDL and DML)
- ✅ Database information retrieval
- ✅ Operation logging
- ✅ Connection pool management
- ✅ Auto-reconnection mechanism
- ✅ Health checks
- ✅ Error handling and recovery
Installation
Global Installation (Recommended)
npm install -g @liangshanli/mcp-server-mysql
Local Installation
npm install @liangshanli/mcp-server-mysql
From Source
git clone https://github.com/liliangshan/mcp-server-mysql.git
cd mcp-server-mysql
npm install
Configuration
Set environment variables:
export MYSQL_HOST=localhost
export MYSQL_PORT=3306
export MYSQL_USER=root
export MYSQL_PASSWORD=your_password
export MYSQL_DATABASE=your_database
export ALLOW_DDL=true
export ALLOW_DROP=false
export ALLOW_DELETE=false
Usage
1. Direct Run (Global Installation)
mcp-server-mysql
2. Using npx (Recommended)
npx @liangshanli/mcp-server-mysql
3. Direct Start (Source Installation)
npm start
4. Managed Start (Recommended for Production)
npm run start-managed
Managed start provides:
- Auto-restart (up to 10 times)
- Error recovery
- Process management
- Logging
5. Development Mode
npm run dev
Editor Integration
Cursor Editor Configuration
- Create
.cursor/mcp.jsonfile in your project root:
{
"mcpServers": {
"mysql": {
"command": "npx",
"args": ["@liangshanli/mcp-server-mysql"],
"env": {
"MYSQL_HOST": "your_host",
"MYSQL_PORT": "3306",
"MYSQL_USER": "your_user",
"MYSQL_PASSWORD": "your_password",
"MYSQL_DATABASE": "your_database",
"ALLOW_DDL": "false",
"ALLOW_DROP": "false",
"ALLOW_DELETE": "false"
}
}
}
}
VS Code Configuration
- Install the MCP extension for VS Code
- Create
.vscode/settings.jsonfile:
{
"mcp.servers": {
"mysql": {
"command": "npx",
"args": ["@liangshanli/mcp-server-mysql"],
"args": ["@liangshanli/mcp-server-mysql"],
"env": {
"MYSQL_HOST": "your_host",
"MYSQL_PORT": "3306",
"MYSQL_USER": "your_user",
"MYSQL_PASSWORD": "your_password",
"MYSQL_DATABASE": "your_database",
"ALLOW_DDL": "false",
"ALLOW_DROP": "false",
"ALLOW_DELETE": "false"
}
}
}
}
As MCP Server
The server communicates with MCP clients via stdin/stdout after startup:
{"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": "2025-06-18"}}
Available Tools
-
sql_query: Execute SQL queries
{ "jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": { "name": "sql_query", "arguments": { "sql": "SELECT * FROM users LIMIT 10" } } } -
get_database_info: Get database information
{ "jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": { "name": "get_database_info", "arguments": {} } } -
get_operation_logs: Get operation logs
{ "jsonrpc": "2.0", "id": 4, "method": "tools/call", "params": { "name": "get_operation_logs", "arguments": { "limit": 50, "offset": 0 } } }
Connection Pool Features
- Auto-creation: Automatically creates connection pool on
notifications/initialized - Health checks: Checks connection pool status every 5 minutes
- Auto-reconnection: Automatically recreates connection pool when it fails
- Connection reuse: Uses connection pool for better performance
- Graceful shutdown: Properly releases connections when server shuts down
Logging
Log file location: ./logs/mcp-mysql.log
Logged content:
- All requests and responses
- SQL operation records
- Error messages
- Connection pool status changes
Error Handling
- Individual request errors don’t affect the entire server
- Connection pool errors are automatically recovered
- Process exceptions are automatically restarted (managed mode)
Environment Variables
| Variable | Default | Description |
|---|---|---|
| MYSQL_HOST | localhost | MySQL host address |
| MYSQL_PORT | 3306 | MySQL port |
| MYSQL_USER | root | MySQL username |
| MYSQL_PASSWORD | MySQL password | |
| MYSQL_DATABASE | Database name | |
| ALLOW_DDL | false | Whether to allow DDL operations (CREATE, ALTER, TRUNCATE, RENAME, COMMENT). Set to ‘true’ to enable |
| ALLOW_DROP | false | Whether to allow DROP operations. Set to ‘true’ to enable |
| ALLOW_DELETE | false | Whether to allow DELETE operations. Set to ‘true’ to enable |
| MCP_LOG_DIR | ./logs | Log directory |
| MCP_LOG_FILE | mcp-mysql.log | Log filename |
Development
Project Structure
mcpmysql/
├── src/
│ └── server-final.js # Main server file
├── start-server.js # Managed startup script
├── package.json
└── README.md
Testing
npm test
Quick Start
1. Install Package
npm install -g @liangshanli/mcp-server-mysql
2. Configure Environment Variables
export MYSQL_HOST=localhost
export MYSQL_PORT=3306
export MYSQL_USER=root
export MYSQL_PASSWORD=your_password
export MYSQL_DATABASE=your_database
export ALLOW_DDL=false
export ALLOW_DROP=false
export ALLOW_DELETE=false
Permission Control Examples:
# Default: Disable all destructive operations (safe mode)
export ALLOW_DDL=false
export ALLOW_DROP=false
export ALLOW_DELETE=false
# Allow DDL but disable DROP and DELETE
export ALLOW_DDL=true
export ALLOW_DROP=false
export ALLOW_DELETE=false
# Allow everything except DELETE
export ALLOW_DDL=true
export ALLOW_DROP=true
export ALLOW_DELETE=false
# Enable all operations (use with caution)
export ALLOW_DDL=true
export ALLOW_DROP=true
export ALLOW_DELETE=true
3. Run Server
mcp-server-mysql
License
MIT


被折叠的 条评论
为什么被折叠?



