Skip to main content
Glama

MySQL MCP Server

MySQL MCP 服务器

用于与 MySQL 数据库交互的 MCP 服务器。

该服务器支持执行只读查询(query)和最终回滚的写查询(test_execute)。

设置

环境变量

将以下环境变量添加到~/.mcp/.env

MYSQL_HOST=host.docker.internal # Hostname to access host services from Docker container MYSQL_PORT=3306 MYSQL_USER=root MYSQL_PASSWORD=your_password

注意host.docker.internal是一个特殊的 DNS 名称,用于从 Docker 容器访问主机服务。连接到主机上运行的 MySQL 服务器时请使用此设置。如果连接到其他 MySQL 服务器,请更改为相应的主机名。

mcp.json 配置

{ "mcpServers": { "mysql": { "command": "docker", "args": [ "run", "-i", "--rm", "--add-host=host.docker.internal:host-gateway", "--env-file", "/Users/username/.mcp/.env", "ghcr.io/xiangma9712/mcp/mysql" ] } } }

用法

启动服务器

docker run -i --rm --add-host=host.docker.internal:host-gateway --env-file ~/.mcp/.env ghcr.io/xiangma9712/mcp/mysql

注意:如果您使用的是 OrbStack,则会自动支持host.docker.internal ,因此可以省略--add-host选项。虽然 Docker Desktop 通常也自动支持此功能,但为了获得更好的可靠性,建议添加--add-host选项。

可用命令

1.执行只读查询
{ "type": "query", "payload": { "sql": "SELECT * FROM your_table" } }

回复:

{ "success": true, "data": [ { "id": 1, "name": "example" } ] }
2.测试查询执行
{ "type": "test_execute", "payload": { "sql": "UPDATE your_table SET name = 'updated' WHERE id = 1" } }

回复:

{ "success": true, "data": "The UPDATE SQL query can be executed." }
3. 列出表格
{ "type": "list_tables" }

回复:

{ "success": true, "data": ["table1", "table2", "table3"] }
4.描述表
{ "type": "describe_table", "payload": { "table": "your_table" } }

回复:

{ "success": true, "data": [ { "Field": "id", "Type": "int(11)", "Null": "NO", "Key": "PRI", "Default": null, "Extra": "" }, { "Field": "name", "Type": "varchar(255)", "Null": "YES", "Key": "", "Default": null, "Extra": "" } ] }

实现细节

  • 使用 TypeScript 实现
  • 使用 mysql2 包
  • 作为 Docker 容器运行
  • 通过标准输入接受 JSON 命令
  • 通过标准输出返回 JSON 响应
  • 使用host.docker.internal连接到主机 MySQL(兼容 OrbStack 和 Docker Desktop)

安全注意事项

  • 使用环境变量进行敏感信息管理
  • 预防 SQL 注入是实施者的责任
  • 生产使用需要正确的网络配置
  • 连接主机服务时需要适当的防火墙设置
Install Server
A
security – no known vulnerabilities
A
license - permissive license
A
quality - confirmed to work

remote-capable server

The server can be hosted and run remotely because it primarily relies on remote services or has no dependency on the local environment.

可以通过 JSON 命令与 MySQL 数据库交互,支持只读查询、写入查询的测试执行以及通过 Docker 检索表信息。

  1. 设置
    1. 环境变量
    2. mcp.json 配置
  2. 用法
    1. 启动服务器
    2. 可用命令
  3. 实现细节
    1. 安全注意事项

      Related MCP Servers

      • A
        security
        F
        license
        A
        quality
        Enables AI models to perform MySQL database operations through a standardized interface, supporting secure connections, query execution, and comprehensive schema management.
        Last updated -
        7
        35
        26
        JavaScript
      • -
        security
        F
        license
        -
        quality
        Facilitates interaction with a local MySQL database via a RESTful API, supporting database queries and real-time updates with integration for Cursor MCP services.
        Last updated -
        4
        JavaScript
      • A
        security
        A
        license
        A
        quality
        Connect and interact with MySQL databases seamlessly. Execute SQL queries, manage database connections, and retrieve data directly through AI assistants. Enhance your AI capabilities with structured access to your MySQL data.
        Last updated -
        9
        4
        15
        TypeScript
        MIT License
      • -
        security
        F
        license
        -
        quality
        Enables secure interaction with MySQL databases, allowing AI assistants to list tables, read data, and execute SQL queries through a controlled interface.
        Last updated -
        Python
        • Linux
        • Apple

      View all related MCP servers

      MCP directory API

      We provide all the information about MCP servers via our MCP API.

      curl -X GET 'https://glama.ai/api/mcp/v1/servers/xiangma9712/mysql-mcp-server'

      If you have feedback or need assistance with the MCP directory API, please join our Discord server