MySQL数据库MCP服务器

一个用于MySQL数据库的MCP服务器,通过标准MCP协议为AI助手提供SQL查询、表结构查看、数据库列表及只读访问等能力,适用于大模型驱动的数据查询与操作场景。

  • by ImRieul
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 ImRieul/mysql-mcp-server

使用说明

@imrieul/mysql-mcp-server

한국어 | 日本語 | 中文 | Español | Português | Français | Русский

A simple MySQL MCP server.

Features

  • LLM-friendly — compact column/row format, auto LIMIT, error hints with next-action suggestions
  • Bulk schema — describe_all_tables returns entire DB schema in one call
  • Read-only mode — app-level prefix check + DB-level SET SESSION TRANSACTION READ ONLY
  • Query timeout — Promise.race() based timeout with guaranteed connection cleanup
  • SSL support — one env var to enable encrypted connections
  • Minimal — 3 dependencies, zero config required

Quick Start

Add to your MCP config (.mcp.json or claude_desktop_config.json):

{
  "mcpServers": {
    "mysql": {
      "command": "npx",
      "args": ["-y", "@imrieul/mysql-mcp-server"],
      "env": {
        "MYSQL_HOST": "localhost",
        "MYSQL_PORT": "3306",
        "MYSQL_USER": "root",
        "MYSQL_PASSWORD": "your-password",
        "MYSQL_DATABASE": "your-database"
      }
    }
  }
}

Or with a connection string:

{
  "mcpServers": {
    "mysql": {
      "command": "npx",
      "args": ["-y", "@imrieul/mysql-mcp-server", "mysql://root:password@localhost:3306/mydb"]
    }
  }
}

Codex

Codex uses TOML config, not mcpServers JSON. Add this to ~/.codex/config.toml or a project-scoped .codex/config.toml:

[mcp_servers.mysql]
command = "npx"
args = ["-y", "@imrieul/mysql-mcp-server"]

[mcp_servers.mysql.env]
MYSQL_HOST = "localhost"
MYSQL_PORT = "3306"
MYSQL_USER = "root"
MYSQL_PASSWORD = "your-password"
MYSQL_DATABASE = "your-database"

opencode

Add this to opencode.json or opencode.jsonc:

{
  "$schema": "https://opencode.ai/config.json",
  "mcp": {
    "mysql": {
      "type": "local",
      "command": ["npx", "-y", "@imrieul/mysql-mcp-server"],
      "enabled": true,
      "environment": {
        "MYSQL_HOST": "localhost",
        "MYSQL_PORT": "3306",
        "MYSQL_USER": "root",
        "MYSQL_PASSWORD": "your-password",
        "MYSQL_DATABASE": "your-database"
      }
    }
  }
}

Read-only mode with SSL

{
  "mcpServers": {
    "mysql": {
      "command": "npx",
      "args": ["-y", "@imrieul/mysql-mcp-server", "--readonly"],
      "env": {
        "MYSQL_HOST": "db.example.com",
        "MYSQL_USER": "readonly_user",
        "MYSQL_PASSWORD": "password",
        "MYSQL_DATABASE": "production",
        "MYSQL_SSL": "true",
        "MYSQL_QUERY_TIMEOUT": "10000"
      }
    }
  }
}

Tools

Tool Description Read-only mode
query Execute SELECT, SHOW, DESCRIBE, EXPLAIN Allowed
execute Execute INSERT, UPDATE, DELETE, DDL Blocked
list_databases List all databases Allowed
list_tables List tables in a database Allowed
describe_table Show table schema Allowed
describe_all_tables Show all table schemas at once Allowed

Configuration

Environment variables

Variable Required Default Description
MYSQL_HOST Yes — MySQL host
MYSQL_USER Yes — MySQL user
MYSQL_PASSWORD Yes — MySQL password
MYSQL_PORT No 3306 MySQL port
MYSQL_DATABASE No — Default database
MYSQL_READONLY No false Read-only mode
MYSQL_MAX_ROWS No 100 Auto LIMIT for SELECT queries
MYSQL_QUERY_TIMEOUT No 30000 Query timeout in ms (0 to disable)
MYSQL_SSL No false Enable SSL connection

Connection string

mysql://user:password@host:port/database

Passed as the first CLI argument. Takes priority over environment variables.

License

MIT