Skip to main content
Glama
MySQL MCP Server

MySQL MCP Server

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 tools
mysql_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

    A3.9/5.0

    Scored across 6 tools

    Disambiguation5/5

    Each tool targets a distinct operation (query, explain, list databases, list tables, describe table, execute DML). No overlap in purpose or usage.

    Naming Consistency5/5

    All tool names follow a consistent 'mysql_' prefix with verb_noun snake_case (mysql_query, mysql_explain, mysql_list_databases, etc.). Uniform and predictable.

    Tool Count5/5

    Six tools is well within the ideal 3-15 range. Each tool provides a clear, necessary capability for a MySQL server without redundancy.

    Completeness4/5

    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

    ActivityInactive
    ResponsivenessNo recent community issues

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      Not graded
      quality
      D
      maintenance
      Enables 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
    • A
      license
      Not graded
      quality
      D
      maintenance
      Enables AI tools to interact with MySQL databases through natural language, including schema browsing, queries, CRUD operations, with destructive actions gated behind user approval.
      64 npm
      1
      MIT
    • F
      license
      Not graded
      quality
      D
      maintenance
      Enables 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
      -
    • A
      license
      Not graded
      quality
      B
      maintenance
      A 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.
      2
      Apache 2.0