MCP cover image
See in Github
2025-03-25

MCP服务器访问MySQL数据库

1

Github Watches

2

Github Forks

5

Github Stars

MySQL MCP Server

An MCP server for interacting with MySQL databases.

This server supports executing read-only queries (query) and write queries that are ultimately rolled back (test_execute).

MySQL Server MCP server

Setup

Environment Variables

Add the following environment variables to ~/.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

Note: host.docker.internal is a special DNS name for accessing host machine services from Docker containers. Use this setting when connecting to a MySQL server running on your host machine. If connecting to a different MySQL server, change to the appropriate hostname.

mcp.json Configuration

{
  "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"
      ]
    }
  }
}

Usage

Starting the Server

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

Note: If you're using OrbStack, host.docker.internal is automatically supported, so the --add-host option can be omitted. While Docker Desktop also typically supports this automatically, adding the --add-host option is recommended for better reliability.

Available Commands

1. Execute Read-only Query

{
  "type": "query",
  "payload": {
    "sql": "SELECT * FROM your_table"
  }
}

Response:

{
  "success": true,
  "data": [
    {
      "id": 1,
      "name": "example"
    }
  ]
}

2. Test Query Execution

{
  "type": "test_execute",
  "payload": {
    "sql": "UPDATE your_table SET name = 'updated' WHERE id = 1"
  }
}

Response:

{
  "success": true,
  "data": "The UPDATE SQL query can be executed."
}

3. List Tables

{
  "type": "list_tables"
}

Response:

{
  "success": true,
  "data": ["table1", "table2", "table3"]
}

4. Describe Table

{
  "type": "describe_table",
  "payload": {
    "table": "your_table"
  }
}

Response:

{
  "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": ""
    }
  ]
}

Implementation Details

  • Implemented in TypeScript
  • Uses mysql2 package
  • Runs as a Docker container
  • Accepts JSON commands through standard input
  • Returns JSON responses through standard output
  • Uses host.docker.internal to connect to host MySQL (compatible with both OrbStack and Docker Desktop)

Security Considerations

  • Uses environment variables for sensitive information management
  • SQL injection prevention is the implementer's responsibility
  • Proper network configuration required for production use
  • Appropriate firewall settings needed when connecting to host machine services

相关推荐

  • NiKole Maxwell
  • I craft unique cereal names, stories, and ridiculously cute Cereal Baby images.

  • Bora Yalcin
  • Evaluator for marketplace product descriptions, checks for relevancy and keyword stuffing.

  • https://jgadvisorycpa.com
  • This GPT assists in finding a top-rated business CPA - local or virtual. We account for their qualifications, experience, testimonials and reviews. Business operators provide a short description of your business, services wanted, and city or state.

  • https://suefel.com
  • Latest advice and best practices for custom GPT development.

  • Yusuf Emre Yeşilyurt
  • I find academic articles and books for research and literature reviews.

  • https://maiplestudio.com
  • Find Exhibitors, Speakers and more

  • Carlos Ferrin
  • Encuentra películas y series en plataformas de streaming.

  • Joshua Armstrong
  • Confidential guide on numerology and astrology, based of GG33 Public information

  • Emmet Halm
  • Converts Figma frames into front-end code for various mobile frameworks.

  • Alexandru Strujac
  • Efficient thumbnail creator for YouTube videos

  • Elijah Ng Shi Yi
  • Advanced software engineer GPT that excels through nailing the basics.

  • lumpenspace
  • Take an adjectivised noun, and create images making it progressively more adjective!

  • apappascs
  • 发现市场上最全面,最新的MCP服务器集合。该存储库充当集中式枢纽,提供了广泛的开源和专有MCP服务器目录,并提供功能,文档链接和贡献者。

  • ShrimpingIt
  • MCP系列GPIO Expander的基于Micropython I2C的操作,源自ADAFRUIT_MCP230XX

  • OffchainLabs
  • 进行以太坊的实施

  • modelcontextprotocol
  • 模型上下文协议服务器

  • huahuayu
  • 统一的API网关,用于将多个Etherscan样区块链Explorer API与对AI助手的模型上下文协议(MCP)支持。

  • Mintplex-Labs
  • 带有内置抹布,AI代理,无代理构建器,MCP兼容性等的多合一桌面和Docker AI应用程序。

    Reviews

    1 (1)
    Avatar
    user_yYz3PcuD
    2025-04-16

    As a dedicated user of mysql-mcp-server, I can confidently say that this tool has significantly streamlined our database management processes. The seamless integration and efficient performance have made it an invaluable asset in our tech stack. Kudos to xiangma9712 for developing such a robust server solution! Highly recommended for anyone in need of a reliable MySQL management tool. Check it out at https://github.com/xiangma9712/mysql-mcp-server.