MySQL MCP Server
Provides tools for interacting with MariaDB databases, enabling AI agents to query, analyze table structures, get sample data, and execute write operations when enabled.
Provides tools for interacting with MySQL databases, enabling AI agents to query, analyze table structures, get sample data, and execute write operations when enabled.
Click on "Deploy Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@MySQL MCP Server list all tables in the current database"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
MySQL MCP Server
让AI安全地理解和操作你的MySQL/MariaDB数据库
✨ 特性
🔒 安全优先 — 默认只读模式,写操作需显式开启
📊 智能分析 — 表结构、统计信息、执行计划一键获取
🇨🇳 中文优先 — 文档、错误提示、注释全部中文化
🔌 即插即用 — 支持Claude Desktop、Cursor、Cline等MCP客户端
🛡️ 访问控制 — 可配置允许的数据库和禁止的表
🔄 连接池 — 自动管理数据库连接,高效复用
Related MCP server: MCP MySQL Server
🚀 快速开始
安装
pip install mysql-mcp-server配置
在Claude Desktop的配置文件中添加:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
Windows: %APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"mysql": {
"command": "python",
"args": ["-m", "mysql_mcp_server"],
"env": {
"MYSQL_MCP_HOST": "localhost",
"MYSQL_MCP_PORT": "3306",
"MYSQL_MCP_USER": "root",
"MYSQL_MCP_PASSWORD": "your_password",
"MYSQL_MCP_DATABASE": "your_database",
"MYSQL_MCP_READ_ONLY": "true"
}
}
}
}Cursor配置
在Cursor的MCP设置中添加:
{
"mcpServers": {
"mysql": {
"command": "python",
"args": ["-m", "mysql_mcp_server"],
"env": {
"MYSQL_MCP_HOST": "localhost",
"MYSQL_MCP_PORT": "3306",
"MYSQL_MCP_USER": "root",
"MYSQL_MCP_PASSWORD": "your_password",
"MYSQL_MCP_DATABASE": "your_database"
}
}
}
}🛠️ 工具列表
工具 | 说明 | 安全级别 |
| 列出所有可访问的数据库 | 🟢 只读 |
| 列出指定数据库的所有表 | 🟢 只读 |
| 获取表的详细结构(列、索引、建表语句) | 🟢 只读 |
| 执行SQL查询(仅SELECT/SHOW/DESCRIBE) | 🟢 只读 |
| 获取表的样本数据 | 🟢 只读 |
| 获取表的统计信息(行数、大小等) | 🟢 只读 |
| 获取SQL执行计划 | 🟢 只读 |
| 执行写操作(INSERT/UPDATE/DELETE) | 🔴 需关闭只读 |
⚙️ 环境变量
变量 | 默认值 | 说明 |
| localhost | MySQL主机地址 |
| 3306 | MySQL端口 |
| root | MySQL用户名 |
| (空) | MySQL密码 |
| (空) | 默认数据库 |
| true | 只读模式 |
| 100 | 查询结果最大行数 |
| 30 | 查询超时(秒) |
| (空=全部) | 允许访问的数据库列表 |
| (空=无) | 禁止访问的表列表 |
🔒 安全说明
默认只读 — 服务器启动后默认为只读模式,只能执行SELECT等查询
写操作需显式开启 — 设置
MYSQL_MCP_READ_ONLY=false才能使用execute_write访问控制 — 可通过
ALLOWED_DATABASES限制可访问的数据库表级屏蔽 — 可通过
BLOCKED_TABLES屏蔽敏感表(支持通配符)结果限制 — 默认最多返回100行,防止意外返回大量数据
超时保护 — 查询默认30秒超时,防止长时间运行的查询
📝 使用示例
在Claude中对话
你: 帮我看看数据库里有哪些表
Claude: [调用 list_databases → list_tables] 你的数据库中有以下表...
你: users表的结构是什么?
Claude: [调用 describe_table] users表有以下字段...
你: 帮我查一下最近注册的10个用户
Claude: [调用 query] SELECT * FROM users ORDER BY created_at DESC LIMIT 10
你: 这个查询为什么这么慢?
Claude: [调用 explain_query] 让我看看执行计划...🏗️ 开发
# 克隆仓库
git clone https://github.com/your-username/mysql-mcp-server.git
# 安装开发依赖
pip install -e ".[dev]"
# 运行测试
pytest tests/📄 License
MIT License
Available Tools
8 toolsdescribe_tableDescribe TableA
获取表的详细结构信息,包括列定义、索引、建表语句
Inputs: table (required), database (required). Returns: result
execute_writeExecute WriteA
执行写操作(INSERT/UPDATE/DELETE),需要关闭只读模式
⚠️ 此工具仅在只读模式关闭时可用(MYSQL_MCP_READ_ONLY=false)
Inputs: sql (required), database. Returns: result
explain_queryExplain QueryA
获取SQL查询的执行计划,用于分析查询性能
Inputs: sql (required), database. Returns: result
get_table_sampleGet Table SampleA
获取表的样本数据(默认前5行),快速了解数据内容
Inputs: table (required), database (required), limit. Returns: result
get_table_statsGet Table StatsA
获取表的统计信息,包括行数、数据大小、索引大小、列基数
Inputs: table (required), database (required). Returns: result
list_databasesList DatabasesA
列出MySQL服务器上所有可访问的数据库
Inputs: no named parameters. Returns: result
list_tablesList TablesA
列出指定数据库中的所有表
Inputs: database (required). Returns: result
queryQueryA
执行SQL查询(只读,仅支持SELECT/SHOW/DESCRIBE/EXPLAIN)
Inputs: sql (required), database. Returns: result
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
TDQS
Scored across 8 tools
Most tools are clearly distinct in purpose, but `query` overlaps with several specialized tools since it can run SELECT/SHOW/DESCRIBE/EXPLAIN directly. The specialized tools have clear output formats and use cases, so an agent can usually pick correctly, but some boundary overlap exists.
The naming pattern is mostly consistent with snake_case verb_noun names like `list_databases`, `describe_table`, and `explain_query`. The generic `query` tool breaks the pattern slightly, but the overall convention is predictable and readable.
Eight tools is a well-scoped set for a MySQL MCP server. Each tool covers a meaningful area of database interaction—discovery, inspection, sampling, statistics, querying, performance analysis, and writes—without unnecessary redundancy or bloat.
The server provides solid coverage for read operations, schema inspection, query execution, performance analysis, and write operations. Missing DDL operations like creating or altering tables and databases are a minor gap, but for typical database exploration and data manipulation workflows the surface is largely complete.
Maintenance
Related MCP Connectors
Safe, read-only Postgres and MySQL access for AI agents. Audit log + column-level controls.
PostgreSQL, MySQL, and SQL Server in one session. 26 read-only MCP tools for AI agents.
Guard AI agents' PostgreSQL/MySQL access via MCP: SQL audit, auth, masking, write approval
PostgreSQL, MySQL, OpenAPI/Swagger, and shared Agent Memory with scoped access.
Related MCP Servers
- FlicenseNot gradedqualityDmaintenanceEnables secure interaction with MySQL databases, allowing AI assistants to list tables, read data, and execute SQL queries through a controlled interface.-
- AlicenseNot gradedqualityDmaintenanceEnables 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.13 npmMIT
- AlicenseNot gradedqualityNot gradedmaintenanceEnables AI assistants to securely interact with MySQL databases through tools for query execution, schema inspection, and transaction management. It features built-in safety controls like row limits and query validation to ensure safe and standardized database access.716 npm-
- AlicenseBqualityDmaintenanceEnables AI assistants to securely interact with MariaDB and MySQL databases using granular per-connection read/write permissions and transaction support. It allows users to manage multiple database connections, explore schemas, and execute controlled SQL queries through a standardized interface.617 npm5MIT