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

06-02 1557阅读

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

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们。

目录[+]

取消
微信二维码
微信二维码
支付宝二维码