mcp_mysql_服务器
介绍
mcp_mysql_server_pro 不仅涉及 MySQL CRUD 操作,还包括数据库异常分析功能,并方便开发人员使用自定义工具进行扩展。
- 支持 STDIO 和 SSE 模式
- 支持多个SQL执行,以“;”分隔
- 支持根据表注释查询数据库表名和字段
- 支持SQL执行计划分析
- 支持中文字段转拼音
- 支持表锁分析
- 支持readonly、writer、admin三种角色的权限控制
"readonly": ["SELECT", "SHOW", "DESCRIBE", "EXPLAIN"], # Read-only permissions
"writer": ["SELECT", "SHOW", "DESCRIBE", "EXPLAIN", "INSERT", "UPDATE", "DELETE"], # Read-write permissions
"admin": ["SELECT", "SHOW", "DESCRIBE", "EXPLAIN", "INSERT", "UPDATE", "DELETE",
"CREATE", "ALTER", "DROP", "TRUNCATE"] # Administrator permissions
使用说明
SSE模式
将以下内容添加到您的 mcp 客户端工具中,例如 cursor、cline 等。
mcp json 如下:
{
"mcpServers": {
"operateMysql": {
"name": "operateMysql",
"description": "",
"isActive": true,
"baseUrl": "http://localhost:9000/sse"
}
}
}
修改 .env 文件内容以使用数据库详细信息更新数据库连接信息:
# MySQL Database Configuration
MYSQL_HOST=192.168.xxx.xxx
MYSQL_PORT=3306
MYSQL_USER=root
MYSQL_PASSWORD=root
MYSQL_DATABASE=a_llm
MYSQL_ROLE=readonly # Optional, default is 'readonly'. Available values: readonly, writer, admin
启动命令:
# Download dependencies
uv sync
# Start
uv run server.py
STDIO模式
将以下内容添加到您的 mcp 客户端工具中,例如 cursor、cline 等。
mcp json 如下:
{
"mcpServers": {
"operateMysql": {
"isActive": true,
"name": "operateMysql",
"command": "uv",
"args": [
"--directory",
"G:\\python\\mysql_mcp\\src", # Replace this with your project path
"run",
"server.py",
"--stdio"
],
"env": {
"MYSQL_HOST": "192.168.xxx.xxx",
"MYSQL_PORT": "3306",
"MYSQL_USER": "root",
"MYSQL_PASSWORD": "root",
"MYSQL_DATABASE": "a_llm",
"MYSQL_ROLE": "readonly" # Optional, default is 'readonly'. Available values: readonly, writer, admin
}
}
}
}
自定义工具扩展
- 在handles包中添加一个新的工具类,继承自BaseHandler,并实现get_tool_description和run_tool方法
- 在init.py中导入新工具,使其在服务器中可用
示例
- 创建新表并插入数据,提示格式如下:
# Task
Create an organizational structure table with the following structure: department name, department number, parent department, is valid.
# Requirements
- Table name: t_admin_rms_zzjg
- Field requirements: string type uses 'varchar(255)', integer type uses 'int', float type uses 'float', date and time type uses 'datetime', boolean type uses 'boolean', text type uses 'text', large text type uses 'longtext', large integer type uses 'bigint', large float type uses 'double'
- Table header needs to include primary key field, serial number XH varchar(255)
- Table must include these fixed fields at the end: creator-CJR varchar(50), creation time-CJSJ datetime, modifier-XGR varchar(50), modification time-XGSJ datetime
- Field naming should use tool return content
- Common fields need indexes
- Each field needs comments, table needs comment
- Generate 5 real data records after creation
- 根据表comments查询数据,提示如下:
Query Zhang San's data from the user information table
- 分析慢SQL,提示如下:
select * from t_jcsjzx_hjkq_cd_xsz_sk xsz
left join t_jcsjzx_hjkq_jcd jcd on jcd.cddm = xsz.cddm
Based on current index situation, review execution plan and provide optimization suggestions in markdown format, including table index status, execution details, and optimization recommendations
- 分析SQL死锁问题,提示如下:
update t_admin_rms_zzjg set sfyx = '0' where xh = '1' is stuck, please analyze the cause