MySQL MCP Server
Provides a secure gateway for AI agents to access MySQL databases, supporting read/write operations with risk-level gating, AST-based validation, and full audit trails.
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 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-to-DB Secure Gateway — 不只是一个 MCP Server,而是 AI 与数据库之间的安全执行网关。
基于 NestJS + TypeScript 构建,通过 MCP 协议为 Claude Code 等 AI 助手提供对 MySQL 数据库的受控访问能力。核心目标是在 AI 的灵活性与数据库的安全性之间建立可信边界。
设计理念
传统数据库工具直接暴露执行能力,对 AI 调用者而言风险极高。本项目的核心设计原则:
最小权限:工具按风险分级,高危操作默认关闭
AST 级校验:不依赖关键字匹配,通过 SQL Parser 解析语法树做严格校验
可审计:每次操作完整记录,行为可追溯
可控执行:AI 可以分析(EXPLAIN)但不能随意破坏
Related MCP server: mysql-ops-mcp
快速开始
npm install
cp .env.example .env
# 编辑 .env 填入 MySQL 连接信息
npm run start:dev # 开发模式
npm run build # 构建
npm run start:prod # 生产运行在 Claude Code 中配置
先构建项目:
npm run build使用 claude mcp add 命令注册(推荐,写入用户级全局配置):
claude mcp add mysql node /path/to/mysql-mcp-nest/dist/main.js \
--scope user \
-e DB_HOST=127.0.0.1 \
-e DB_PORT=3306 \
-e DB_USER=root \
-e DB_PASSWORD=your_password \
-e DB_DATABASE=your_database \
-e PERMISSION_MODE=read_write \
-e AUDIT_LOG_ENABLED=true \
-e AUDIT_LOG_DIR=/path/to/mysql-mcp-nest/logs--scope user:写入~/.claude.json,所有项目均可使用--scope project:写入当前项目的.claude.json,仅当前项目可用
注册后重启 claude,执行 /mcp 验证连接状态:
❯ /mcp
mysql connected在 Codex 中配置
先构建项目:
npm run build使用 codex mcp add 命令注册到 Codex:
codex mcp add mysql \
--env DB_HOST=127.0.0.1 \
--env DB_PORT=3306 \
--env DB_USER=root \
--env DB_PASSWORD=your_password \
--env DB_DATABASE=your_database \
--env PERMISSION_MODE=read_write \
--env AUDIT_LOG_ENABLED=true \
--env AUDIT_LOG_DIR=/path/to/mysql-mcp-nest/logs \
-- node /path/to/mysql-mcp-nest/dist/main.js验证配置是否写入成功:
codex mcp list
codex mcp get mysql注册后重启 Codex,或重新开启一个 Codex 会话,让 MCP 配置生效。
连接成功后即可直接使用:
列出 your_database 中的所有表在 Cursor 中配置
项目已包含 .cursor/mcp.json,Cursor 打开本仓库后会注册 mysql MCP Server:
{
"mcpServers": {
"mysql": {
"type": "stdio",
"command": "node",
"args": ["${workspaceFolder}/dist/main.js"],
"envFile": "${workspaceFolder}/.env",
"env": {
"AUDIT_LOG_DIR": "${workspaceFolder}/logs"
}
}
}
}使用前先构建项目,并确认 .env 里已填好 MySQL 连接信息:
npm run build然后重启 Cursor,或在 Cursor Settings → MCP 中刷新/启用 mysql。
Available Tools
6 toolsmysql_describe_tableA
Get column structure, indexes, and CREATE statement for a table.
Inputs: table (required), database (required).
mysql_executeA
[riskLevel: medium] Execute an INSERT, UPDATE, or DELETE statement. UPDATE/DELETE without WHERE are rejected by default.
Inputs: sql (required), params, database.
mysql_explainA
Run EXPLAIN on a SELECT statement to analyze query performance. Returns execution plan and full-scan warnings.
Inputs: sql (required), database.
mysql_list_databasesA
List all accessible databases (system databases are excluded).
Inputs: no named parameters.
mysql_list_tablesB
List all tables in a database.
Inputs: database (required).
mysql_queryB
Execute a SELECT query against MySQL. Returns rows, field names, and row count. Results are automatically limited.
Inputs: sql (required), limit, params, database.
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
TDQS
Scored across 6 tools
Each tool targets a distinct operation (query, explain, list databases, list tables, describe table, execute DML). No overlap in purpose or usage.
All tool names follow a consistent 'mysql_' prefix with verb_noun snake_case (mysql_query, mysql_explain, mysql_list_databases, etc.). Uniform and predictable.
Six tools is well within the ideal 3-15 range. Each tool provides a clear, necessary capability for a MySQL server without redundancy.
The surface covers query execution, performance analysis, schema exploration (databases, tables, describe), and DML operations. Minor gap: no DDL (create/alter/drop) or transaction control, but for a focused MySQL MCP server the coverage is good.
Maintenance
Related MCP Connectors
Safe, read-only Postgres and MySQL access for AI agents. Audit log + column-level controls.
Guard AI agents' PostgreSQL/MySQL access via MCP: SQL audit, auth, masking, write approval
Security gateway for AI agents: policy, approval, and audited execution, no secrets shared.
Query 40 databases from Claude, ChatGPT, or Cursor — on any device. Read-only, encrypted, audited.
Related MCP Servers
- AlicenseNot gradedqualityDmaintenanceEnables natural language database operations on MySQL databases with AI integration, supporting CRUD operations, schema inspection, and audit logging with built-in security features including SQL injection protection and permission controls.MIT
- AlicenseNot gradedqualityDmaintenanceEnables AI tools to interact with MySQL databases through natural language, including schema browsing, queries, CRUD operations, with destructive actions gated behind user approval.64 npm1MIT
- FlicenseNot gradedqualityDmaintenanceEnables AI assistants like Claude to interact with MySQL databases through a standardized protocol, supporting schema inspection, query execution, and data manipulation with safety controls.1-
- AlicenseNot gradedqualityBmaintenanceA security-first MySQL MCP server that uses AST parsing and multi-layer defense to allow AI to safely query MySQL databases with configurable read/write access, table whitelists, and audit logging.2Apache 2.0