Cover image
Try Now
2025-04-05

3 years

Works with Finder

1

Github Watches

0

Github Forks

0

Github Stars

MySQL MCP Server

基于 Model Context Protocol (MCP) 的 MySQL 数据库连接服务器,支持通过 stdio 方式与客户端通信,允许执行 SQL 查询和数据操作。

功能特性

  • 通过 MCP 协议与客户端通信
  • 支持连接多个 MySQL 数据库
  • 支持查询操作(SELECT)
  • 支持数据操作(INSERT/UPDATE/DELETE)
  • 权限控制(可分别配置查询、插入、更新、删除权限)
  • 支持通过环境变量或JSON配置文件进行灵活配置
  • 支持调试模式,将详细日志输出到文件

安装

方法一:直接安装(推荐)

使用 Go 的安装命令直接从 GitHub 安装:

go install github.com/blanplan-ai/ai2mysql-mcp-server/cmd/ai2mysql-mcp-server@latest

安装完成后,可以直接在命令行中运行:

ai2mysql-mcp-server

方法二:手动构建

# 克隆仓库
git clone https://github.com/blanplan-ai/ai2mysql-mcp-server.git
cd ai2mysql-mcp-server

# 构建服务器
go build -o ai2mysql-mcp-server ./cmd/ai2mysql-mcp-server

配置

服务器支持两种配置方式:环境变量和JSON配置文件。当环境变量存在时,会优先使用环境变量的配置。

环境变量配置

可以设置以下环境变量来配置服务器:

环境变量 说明 默认值
MYSQL_HOST MySQL 主机地址 localhost
MYSQL_PORT MySQL 端口号 3306
MYSQL_USER MySQL 用户名 root
MYSQL_PASS MySQL 密码 (空)
DEFAULT_DATABASE 默认数据库名 test
ALLOW_INSERT 是否允许插入操作 false
ALLOW_UPDATE 是否允许更新操作 false
ALLOW_DELETE 是否允许删除操作 false

JSON配置文件

如果未设置环境变量,服务器将使用JSON配置文件。默认配置文件名为 config.json,可以通过 -config 参数指定其他配置文件。

配置文件示例:

{
  "databases": {
    "default": {
      "host": "localhost",
      "port": 3306,
      "user": "root",
      "password": "",
      "dbname": "test"
    },
    "other_db": {
      "host": "localhost",
      "port": 3306,
      "user": "root",
      "password": "",
      "dbname": "other_database"
    }
  },
  "permission": {
    "allow_query": true,
    "allow_insert": false,
    "allow_update": false,
    "allow_delete": false
  }
}

配置说明:

  • databases: 配置多个数据库连接
    • default: 默认数据库(必需)
    • 可以添加其他数据库配置
  • permission: 权限配置
    • allow_query: 是否允许查询操作(默认:true)
    • allow_insert: 是否允许插入操作(默认:false)
    • allow_update: 是否允许更新操作(默认:false)
    • allow_delete: 是否允许删除操作(默认:false)

使用方法

启动服务器

安装后直接运行:

# 使用默认配置文件
ai2mysql-mcp-server

# 指定配置文件
ai2mysql-mcp-server -config=/path/to/config.json

# 使用环境变量配置
MYSQL_HOST=127.0.0.1 MYSQL_USER=root MYSQL_PASS=password ai2mysql-mcp-server

# 启用调试模式
ai2mysql-mcp-server -debug=true

启用调试模式后,详细日志将输出到 /tmp/ai2mysql.log 文件中,包含完整的请求、响应和错误信息。

在 Cursor 中使用

本服务器设计为与支持 MCP 协议的客户端(如 Cursor)配合使用。

在 Cursor 中,可以通过以下配置启用 MySQL MCP 服务器:

{
  "mcpServers": {
    "ai2mysql-mcp-server": {
      "command": "ai2mysql-mcp-server",
      "args": [],
      "env": {
        "MYSQL_HOST": "127.0.0.1",
        "MYSQL_PORT": "3306",
        "MYSQL_USER": "root",
        "MYSQL_PASS": "password",
        "DEFAULT_DATABASE": "test",  // 默认数据库,可选
        "ALLOW_INSERT": "false",     // 是否允许插入,可选
        "ALLOW_UPDATE": "false",     // 是否允许更新,可选
        "ALLOW_DELETE": "false"      // 是否允许删除,可选
      }
    }
  }
}

添加配置后,你可以通过工具调用使用以下功能:

  1. mcp_mysql_query: 执行 SQL 查询操作

    • 参数: sql - SQL 查询语句
  2. mcp_mysql_execute: 执行 SQL 数据操作

    • 参数: sql - SQL 数据操作语句(INSERT/UPDATE/DELETE)

开发

项目结构

ai2mysql-mcp-server/
├── cmd/
│   └── ai2mysql-mcp-server/  # 服务器入口
│       ├── main.go          # 主程序
│       └── mcp_server.go    # MCP 服务器实现
├── pkg/
│   ├── config/              # 配置管理
│   │   └── config.go
│   └── db/                  # 数据库操作
│       └── db.go
├── go.mod                   # Go 模块定义
├── go.sum                   # 依赖校验和
├── config.json.example      # 配置文件示例
└── README.md                # 说明文档

依赖

调试与故障排除

调试模式

使用 -debug=true 参数启动服务器可以启用调试模式:

ai2mysql-mcp-server -debug=true

调试模式下,服务器会将详细的日志输出到 /tmp/ai2mysql.log 文件中,包括:

  • 所有收到的请求和发送的响应
  • SQL 查询和执行的详细信息
  • 错误和异常情况
  • 性能相关信息(如查询执行时间)

常见问题

如果在 Cursor 中遇到问题:

  1. 工具未显示: 检查 Cursor 配置文件中的命令路径是否正确,确保可执行文件存在并有执行权限
  2. 连接失败: 检查数据库连接信息是否正确,使用调试模式查看详细错误信息
  3. 权限问题: 默认只允许查询操作,检查是否需要启用插入/更新/删除权限

许可证

MIT License

相关推荐

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

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

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

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

  • 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.

  • https://zenepic.net
  • Embark on a thrilling diplomatic quest across a galaxy on the brink of war. Navigate complex politics and alien cultures to forge peace and avert catastrophe in this immersive interstellar adventure.

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

  • https://reddgr.com
  • Delivers concise Python code and interprets non-English comments

  • 林乔安妮
  • A fashion stylist GPT offering outfit suggestions for various scenarios.

  • 1Panel-dev
  • 💬 MaxKB is a ready-to-use AI chatbot that integrates Retrieval-Augmented Generation (RAG) pipelines, supports robust workflows, and provides advanced MCP tool-use capabilities.

  • ShrimpingIt
  • Micropython I2C-based manipulation of the MCP series GPIO expander, derived from Adafruit_MCP230xx

  • Dhravya
  • Collection of apple-native tools for the model context protocol.

  • GLips
  • MCP server to provide Figma layout information to AI coding agents like Cursor

  • open-webui
  • User-friendly AI Interface (Supports Ollama, OpenAI API, ...)

  • Mintplex-Labs
  • The all-in-one Desktop & Docker AI application with built-in RAG, AI agents, No-code agent builder, MCP compatibility, and more.

    Reviews

    1 (1)
    Avatar
    user_cZ8EMAgX
    2025-04-17

    I've been using ai2mysql-mcp-server and it's fantastic! This tool by blanplan-ai seamlessly bridges AI and MySQL, making database management incredibly efficient. Highly recommended for anyone looking to automate and optimize their database workflows. Check it out here: https://github.com/blanplan-ai/ai2mysql-mcp-server