mysql-mcp-server安装及配置指南

MySQL MCP Server MySQL MCP 服务器

A Model Context Protocol (MCP) implementation that enables secure interaction with MySQL databases. This server component facilitates communication between AI applications (hosts/clients) and MySQL databases, making database exploration and analysis safer and more structured through a controlled interface.
模型上下文协议(MCP)实现,可安全地与 MySQL 数据库进行交互。该服务器组件促进了 AI 应用(主机/客户端)与 MySQL 数据库之间的通信,通过受控接口使数据库探索和分析更加安全且结构化。

Note: MySQL MCP Server is not designed to be used as a standalone server, but rather as a communication protocol implementation between AI applications and MySQL databases.
注意:MySQL MCP 服务器不是设计为独立服务器使用,而是作为 AI 应用与 MySQL 数据库之间的通信协议实现。

Features 特点

  • List available MySQL tables as resources
    列出可用的 MySQL 表资源
  • Read table contents 读取表内容
  • Execute SQL queries with proper error handling
    执行 SQL 查询并正确处理错误
  • Secure database access through environment variables
    通过环境变量安全访问数据库
  • Comprehensive logging 完善的日志记录

Installation 安装

Manual Installation 手动安装

pip install mysql-mcp-server

Installing via Smithery 通过 Smithery 安装

To install MySQL MCP Server for Claude Desktop automatically via Smithery:
通过 Smithery 自动安装 MySQL MCP Server for Claude Desktop:

npx -y @smithery/cli install mysql-mcp-server --client claude

Configuration 配置

Set the following environment variables:
设置以下环境变量:

MYSQL_HOST=localhost     # Database host
MYSQL_PORT=3306         # Optional: Database port (defaults to 3306 if not specified)
MYSQL_USER=your_username
MYSQL_PASSWORD=your_password
MYSQL_DATABASE=your_database

Usage 用法

With Claude Desktop 使用 Claude 桌面版

Add this to your claude_desktop_config.json:
将此添加到您的 claude_desktop_config.json :

{
  "mcpServers": {
    "mysql": {
      "command": "uv",
      "args": [
        "--directory", 
        "path/to/mysql_mcp_server",
        "run",
        "mysql_mcp_server"
      ],
      "env": {
        "MYSQL_HOST": "localhost",
        "MYSQL_PORT": "3306",
        "MYSQL_USER": "your_username",
        "MYSQL_PASSWORD": "your_password",
        "MYSQL_DATABASE": "your_database"
      }
    }
  }
}

With Visual Studio Code 使用 Visual Studio Code

Add this to your mcp.json:
将此添加到您的 mcp.json :

{
  "servers": {
      "mysql": {
            "type": "stdio",
            "command": "uvx",
            "args": [
                "--from",
                "mysql-mcp-server",
                "mysql_mcp_server"
            ],
      "env": {
        "MYSQL_HOST": "localhost",
        "MYSQL_PORT": "3306",
        "MYSQL_USER": "your_username",
        "MYSQL_PASSWORD": "your_password",
        "MYSQL_DATABASE": "your_database"
      }
  }
}

Note: Will need to install uv for this to work
注意:要使此功能正常工作,需要安装 uv

Debugging with MCP Inspector 使用 MCP Inspector 进行调试

While MySQL MCP Server isn’t intended to be run standalone or directly from the command line with Python, you can use the MCP Inspector to debug it.
虽然 MySQL MCP Server 不打算独立运行或直接通过 Python 命令行运行,但您可以使用 MCP Inspector 来调试它。

The MCP Inspector provides a convenient way to test and debug your MCP implementation:
MCP Inspector 提供了一个方便的方式来测试和调试您的 MCP 实现:

# Install dependencies
pip install -r requirements.txt
# Use the MCP Inspector for debugging (do not run directly with Python)

The MySQL MCP Server is designed to be integrated with AI applications like Claude Desktop and should not be run directly as a standalone Python program.
MySQL MCP 服务器旨在与 Claude 桌面等 AI 应用程序集成,不应直接作为独立的 Python 程序运行。

Development 开发

# Clone the repository
git clone https://kkgithub.com/yourusername/mysql_mcp_server.git
cd mysql_mcp_server
# Create virtual environment
python -m venv venv
source venv/bin/activate  # or `venv\Scripts\activate` on Windows
# Install development dependencies
pip install -r requirements-dev.txt
# Run tests
pytest

Security Considerations 安全考虑

  • Never commit environment variables or credentials
    永远不要提交环境变量或凭证
  • Use a database user with minimal required permissions
    使用具有最小必要权限的数据库用户
  • Consider implementing query whitelisting for production use
    考虑在生产环境中实施查询白名单
  • Monitor and log all database operations
    监控并记录所有数据库操作

Security Best Practices 安全最佳实践

This MCP implementation requires database access to function. For security:
此 MCP 实现需要数据库访问才能运行。为了安全:

  1. Create a dedicated MySQL user with minimal permissions
    创建具有最小权限的专用 MySQL 用户
  2. Never use root credentials or administrative accounts
    永远不要使用 root 凭证或管理员账户
  3. Restrict database access to only necessary operations
    仅允许必要的操作访问数据库
  4. Enable logging for audit purposes
    启用日志记录以供审计
  5. Regular security reviews of database access
    定期进行数据库访问的安全审查

See MySQL Security Configuration Guide for detailed instructions on:
查看 MySQL 安全配置指南以获取有关以下内容的详细说明:

  • Creating a restricted MySQL user
    创建受限的 MySQL 用户
  • Setting appropriate permissions
    设置适当的权限
  • Monitoring database access
    监控数据库访问
  • Security best practices 安全最佳实践

⚠️ IMPORTANT: Always follow the principle of least privilege when configuring database access.
⚠️ 重要:配置数据库访问时始终遵循最小权限原则。


links:

https://kkgithub.com/designcomputer/mysql_mcp_server

### MCP MySQL Server Configuration and Management #### Overview of MCP MySQL Server Setup For configuring an MCP (Multi-Cloud Platform) environment with a MySQL server, the setup involves ensuring that all necessary configurations are correctly applied to integrate MySQL as part of the data storage solution. In scenarios where applications like Nacos need to connect to a MySQL database, it is crucial to ensure proper initialization and configuration. #### Initialization of MySQL Database for Application Integration When initializing a MySQL database specifically for application integration such as switching from default settings to using MySQL, one can modify `application.properties` or configure during Docker startup[^1]. This ensures that the application connects to the correct MySQL instance configured within the MCP environment. #### Schema Creation for Application Data Persistence To avoid issues related to no datasource exceptions when deploying services on platforms like Nacos via Docker while aiming at persisting data into MySQL, creating databases and tables beforehand is essential. The SQL script located at `D:\software\nacos\nacos-server-2.3.1\conf\mysql-schema.sql` serves this purpose by providing commands needed for setting up schemas required by Nacos[^2]. #### Troubleshooting Common Issues During Deployment In cases similar to those described in experiences involving deployment challenges—such as encountering errors due to missing database creation steps—it becomes evident how critical these preparatory actions are before attempting service deployments. Ensuring that both the database exists along with its schema helps prevent common pitfalls associated with connectivity problems between containers running inside Docker environments and external databases[^3]. ```sql -- Example command to create a new database named 'mcp_db' CREATE DATABASE mcp_db; USE mcp_db; SOURCE D:/software/nacos/nacos-server-2.3.1/conf/mysql-schema.sql; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学亮编程手记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值