使用说明

项目简介

DBHub 是一个通用数据库网关,它作为 MCP 服务器,允许兼容 MCP 协议的客户端连接并探索不同的数据库。通过 DBHub,LLM 应用可以安全、便捷地访问和操作多种数据库,实现数据驱动的智能应用。

主要功能点

  • 多数据库支持: 支持 PostgreSQL, MySQL, SQL Server, SQLite 等多种主流数据库。
  • 资源管理: 提供数据库表结构 (Tables, Schema) 等资源的访问接口,使 LLM 能够理解数据库的组织结构。
  • 工具集成: 内置 "执行查询 (run_query)" 和 "列出连接器 (list_connectors)" 等工具,允许 LLM 执行 SQL 查询和获取可用数据库连接信息。
  • Prompt能力: 支持 "生成 SQL (generate_sql)" 和 "解释数据库元素 (explain_db)" 等 Prompt,辅助 LLM 理解数据库内容并生成 SQL 语句。
  • 灵活的部署方式: 支持 Docker 和 NPM 两种安装方式,方便用户快速部署和使用。
  • 多种传输协议: 支持 Stdio 和 SSE 传输协议,兼容不同的 MCP 客户端,如 Claude Desktop 和 Cursor。
  • Demo模式: 内置示例员工数据库 (SQLite),方便用户快速体验和测试 DBHub 的功能。

安装步骤

  1. Docker 安装 (推荐):

    确保已安装 Docker 环境。根据需要选择数据库类型,以下以 PostgreSQL 和 Demo 模式为例:

    • PostgreSQL 示例:

      docker run --rm --init \
         --name dbhub \
         --publish 8080:8080 \
         bytebase/dbhub \
         --transport sse \
         --port 8080 \
         --dsn "postgres://user:password@localhost:5432/dbname?sslmode=disable"

      请替换连接字符串 '"postgres://user:password@localhost:5432/dbname?sslmode=disable"' 为您实际的 PostgreSQL 数据库连接信息。

    • Demo 模式 (内置 SQLite 示例数据库):

      docker run --rm --init \
         --name dbhub \
         --publish 8080:8080 \
         bytebase/dbhub \
         --transport sse \
         --port 8080 \
         --demo
  2. NPM 安装:

    确保已安装 Node.js 和 NPM 环境。

    • PostgreSQL 示例:

      npx @bytebase/dbhub --transport sse --port 8080 --dsn "postgres://user:password@localhost:5432/dbname"

      同样,请替换连接字符串为您实际的数据库连接信息。

    • Demo 模式 (内置 SQLite 示例数据库):

      npx @bytebase/dbhub --transport sse --port 8080 --demo

服务器配置

MCP 客户端需要配置 DBHub 服务器的启动命令和参数,以便建立连接。以下是针对不同场景的配置示例 (JSON 格式),请注意,以下仅为配置示例,MCP 客户端通常只需要配置 'command' 和 'args' 字段,无需复制以下 JSON 代码到终端执行

1. Claude Desktop 配置 (stdio 传输):

Claude Desktop 仅支持 'stdio' 传输。将以下配置添加到 'claude_desktop_config.json' 文件中。

{
  "mcpServers": {
    "dbhub-postgres-docker": {
      "command": "docker",
      "args": [
        "run",
        "-i",
        "--rm",
        "bytebase/dbhub",
        "--transport",
        "stdio",
        "--dsn",
        "postgres://user:[email protected]:5432/dbname?sslmode=disable"
        //  ↑  请替换为您的 PostgreSQL 数据库连接字符串
        //  如果连接本地 Docker 容器中的数据库,请使用 host.docker.internal 作为主机名
      ]
    },
    "dbhub-postgres-npx": {
      "command": "npx",
      "args": [
        "-y",
        "@bytebase/dbhub",
        "--transport",
        "stdio",
        "--dsn",
        "postgres://user:password@localhost:5432/dbname?sslmode=disable"
        //  ↑  请替换为您的 PostgreSQL 数据库连接字符串
      ]
    },
    "dbhub-demo": {
      "command": "npx",
      "args": ["-y", "@bytebase/dbhub", "--transport", "stdio", "--demo"]
    }
  }
}

2. Cursor 配置 (stdio 或 sse 传输):

Cursor 支持 'stdio' 和 'sse' 两种传输方式。请参考 Cursor MCP 指南 进行配置,并确保使用 Agent 模式。

  • stdio 示例 (与 Claude Desktop 配置类似,只需修改客户端配置):

    使用与 Claude Desktop 相同的 'stdio' 配置即可。

  • sse 示例 (假设 DBHub SSE 服务器运行在 'http://localhost:8080/sse'):

    在 Cursor 的 MCP Server 配置中,指定 SSE 端点为 'http://localhost:8080/sse' 即可。

基本使用方法

  1. 启动 DBHub 服务器: 根据上述安装步骤,选择 Docker 或 NPM 方式启动 DBHub 服务器,并根据需要配置数据库连接 (DSN) 和传输协议 (transport)。

  2. 配置 MCP 客户端: 在您的 MCP 客户端 (如 Claude Desktop, Cursor) 中,根据客户端的要求配置 DBHub 服务器的连接信息 (通常是启动命令和参数)。

  3. 使用 LLM 与数据库交互: 在 MCP 客户端中,您可以使用自然语言与数据库进行交互,例如:

    • 请求列出所有数据库表
    • 请求查询特定表的数据
    • 使用 Prompt 功能生成 SQL 查询或解释数据库结构

    客户端会通过 MCP 协议与 DBHub 服务器通信,DBHub 服务器会处理请求并返回结果给客户端,最终呈现给 LLM。

注意: DBHub 默认只允许执行 'SELECT' 查询,以保障数据库安全。

信息

分类

数据库与文件