MySQL只读访问服务器

一个基于流式HTTP传输的MySQL数据库只读访问服务器,提供安全的数据查询功能。

  • by jatzz10
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 jatzz10/mysql-mcp-server

使用说明

MySQL MCP服务器

一种模型上下文协议(MCP)服务器,使用流式http传输提供对MySQL数据库的只读访问。

特性

  • 只读访问:出于安全考虑,只允许使用SELECT查询
  • 多种工具:查询数据、描述表、列出表并获取表信息
  • 可流式HTTP传输:使用流式HTTP进行实时通信
  • 基于环境的配置:使用环境变量轻松设置
  • 错误处理:全面的错误处理和记录
  • 基于HTTP:作为具有可流式传输端点的HTTP服务器运行

安装

  1. 克隆或下载此存储库

  2. 安装依赖项:

    pip install -r requirements.txt
    
  3. 设置环境变量:

    cp env.example .env
    # Edit .env with your MySQL connection details
    

配置

创建一个 .env 包含MySQL连接详细信息的文件:

MYSQL_HOST=localhost
MYSQL_PORT=3306
MYSQL_USER=your_username
MYSQL_PASSWORD=your_password
MYSQL_DATABASE=your_database

用法

运行服务器

# Basic usage (default: localhost:3000)
python mysql_mcp_server.py

# Custom host and port
python mysql_mcp_server.py --host 0.0.0.0 --port 8080

# Using the run script
python run_server.py --host localhost --port 3000

可用工具

  1. query_mysql:执行SELECT查询

    • 参数:
      • query (必填):SQL SELECT查询
      • limit (可选):要返回的最大行数(默认值:1000)
  2. describe_table:获取表结构

    • 参数:
      • table_name (必填):表格名称
  3. list_tables:列出数据库中的所有表

    • 参数:无
  4. get_table_info:获取详细的表格信息

    • 参数:
      • table_name (必填):表格名称

查询示例

# Query all users
query_mysql({"query": "SELECT * FROM users"})

# Query with limit
query_mysql({"query": "SELECT name, email FROM users WHERE active = 1", "limit": 100})

# Describe a table
describe_table({"table_name": "users"})

# List all tables
list_tables({})

# Get detailed table info
get_table_info({"table_name": "users"})

快速开始

选项1:使用测试数据库(建议用于测试)

  1. 安装依赖项:

    pip install -r requirements.txt
    
  2. 设置测试数据库:

    ./setup_test_db.sh
    

    这将创建一个测试用户、数据库,并用示例数据填充它。

  3. 测试安装:

    python test_installation.py
    
  4. 运行服务器:

    python run_server.py
    # Or with custom host/port:
    python run_server.py --host 0.0.0.0 --port 8080
    
  5. 使用示例客户端进行测试:

    python example_client.py
    # Or connect to custom server:
    python example_client.py --host localhost --port 3000
    

选项2:使用自己的数据库

  1. 安装依赖项:

    pip install -r requirements.txt
    
  2. 配置数据库:

    cp env.example .env
    # Edit .env with your MySQL connection details
    
  3. 测试安装:

    python test_installation.py
    
  4. 运行服务器:

    python run_server.py
    
  5. 使用示例客户端进行测试:

    python example_client.py
    

测试数据库

测试数据库包括以下样本数据:

  • 用户:10个具有个人资料和偏好的示例用户
  • 产品:13种不同类别的产品(电子产品、服装、书籍等)
  • 分类:具有11个类别的分层类别结构
  • 订单:10个不同状态的样品订单
  • 项目:详细的订单行项目

示例查询

使用MCP服务器尝试以下示例查询:

-- List active users
SELECT username, email, first_name, last_name FROM users WHERE is_active = 1;

-- Get products with categories
SELECT p.name, p.price, c.name as category FROM products p 
LEFT JOIN categories c ON p.category_id = c.id;

-- Recent orders with user details
SELECT o.id, u.username, o.total_amount, o.status, o.order_date 
FROM orders o JOIN users u ON o.user_id = u.id 
ORDER BY o.order_date DESC LIMIT 5;

-- Top customers by spending
SELECT u.username, SUM(o.total_amount) as total_spent 
FROM users u JOIN orders o ON u.id = o.user_id 
WHERE o.status != 'cancelled' 
GROUP BY u.id, u.username 
ORDER BY total_spent DESC LIMIT 5;

看 test_queries.sql 更全面的例子。

安全

  • 只允许SELECT查询
  • 无INSERT、UPDATE、DELETE或DDL操作
  • 连接参数已验证
  • 强制执行查询限制以防止大型结果集

错误处理

服务器为以下对象提供详细的错误消息:

  • 连接失败
  • 无效查询
  • 缺少参数
  • 数据库错误

日志记录

服务器记录重要事件,包括:

  • 连接建立
  • 查询执行
  • 错误和警告

需求

  • Python 3.8+
  • MySQL数据库
  • 所需软件包(见requirements.txt)

许可证

这个项目是开源的,可以在MIT许可证下使用。