Back to MCP directory
publicPublicdnsLocal runtime

MySQL MCP Server

一个用于与MySQL数据库交互的MCP服务器,支持只读查询和测试性写入操作。

article

README

🚀 MySQL MCP 服务器

MySQL MCP 服务器用于与 MySQL 数据库进行交互。它支持执行只读查询(query)和最终回滚的写入查询(test_execute),为数据库操作提供了便利。

🚀 快速开始

环境配置

环境变量

需将以下环境变量添加到 ~/.mcp/.env 文件中:

MYSQL_HOST=host.docker.internal  # 访问主机服务的 hostname
MYSQL_PORT=3306
MYSQL_USER=root
MYSQL_PASSWORD=your_password

⚠️ 重要提示

host.docker.internal 是 Docker 容器访问主机服务的一个特殊 DNS 名。当 MySQL 服务器运行在你的主机机器上时,请使用此设置。如果连接到其他 MySQL 服务器,请更换为适当的 hostname。

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",
      // 原文档此处未结束,推测后续内容也应按原样保留
    }
  ]
}
help

Runtime guide

cloud

Hosted runtime

Hosted servers run from a provider-managed environment. You usually connect the MCP client to the hosted endpoint or follow the provider's authorization flow, without keeping a local process alive

  1. Open provider connection page
  2. Authorize or copy endpoint
  3. Connect from your MCP client
terminal

Local runtime / other methods

Local servers run on your own machine or infrastructure. You normally copy the server_config into your MCP client, install the required package, and provide env variables from env_schema when needed

  1. Copy server_config
  2. Install required package
  3. Fill env variables and restart client