返回 MCP 目录
public公开dns本地运行

mcp-mongodb-mysql-server

一个支持MySQL和MongoDB的数据库操作服务器,为AI模型提供标准化数据库接口

article

README

🚀 MCP-MySQL-MongoDB 服务器

本项目提供了一个强大的工具集,用于同时管理 MySQL 和 MongoDB 数据库。借助预处理语句、事务支持和异步驱动等功能,保障了数据操作的安全性与效率。

🚀 快速开始

按照以下步骤快速搭建 MCP-MySQL-MongoDB 服务器。

📦 安装指南

安装依赖项

运行以下命令安装所需的依赖项:

npm install mcp-mysql-server mongodb typescript @types/node @types/express express

初始化项目

创建 package.json 文件并添加以下内容:

{
  "name": "mcp-mysql-mongodb-server",
  "version": "1.0.0",
  "main": "server.ts",
  "scripts": {
    "start": "ts-node server.ts"
  },
  "dependencies": {
    "express": "^4.18.2",
    "mcp-mysql-server": "^1.0.0",
    "mongodb": "^5.0.0",
    "@types/node": "^16.0.0",
    "@types/express": "^4.17.1"
  },
  "devDependencies": {
    "ts-node": "^9.0.0",
    "typescript": "^5.0.0"
  }
}

创建服务器文件

创建 server.ts 文件并添加以下内容:

import express from 'express';
import { MySQLServer } from 'mcp-mysql-server';
import { MongoClient } from 'mongodb';

const app = express();
app.listen(3000, () => {
  console.log('服务器已启动,监听在端口 3000');
});

// 初始化数据库连接
const mysqlServer = new MySQLServer({
  host: 'localhost',
  user: 'root',
  password: 'password',
  database: 'test'
});

async function connectMongoDB() {
  const client = await MongoClient.connect('mongodb://localhost:27017', { useNewUrlParser: true, useUnifiedTopology: true });
  const db = client.db('test');
  return { client, db };
}

// 路由示例
app.get('/api/mysql', async (req, res) => {
  try {
    const result = await mysqlServer.query('SELECT * FROM users LIMIT 10');
    res.json(result);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

app.get('/api/mongodb', async (req, res) => {
  try {
    const { db } = await connectMongoDB();
    const collection = db.collection('users');
    const result = await collection.find().toArray();
    res.json(result);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

💻 使用示例

基础用法

本部分将展示如何使用 MCP-MySQL-MongoDB 服务器进行数据库操作。

MySQL 数据库

// 连接数据库
const mysqlServer = new MySQLServer({
  host: 'localhost',
  user: 'root',
  password: 'password',
  database: 'test'
});

// 查询数据
await mysqlServer.query('SELECT * FROM users');

// 关闭连接
await mysqlServer.close();

MongoDB 数据库

// 连接数据库
const client = new MongoClient('mongodb://localhost:27017', { useNewUrlParser: true, useUnifiedTopology: true });
const db = client.db('test');

// 查询数据
const collection = db.collection('users');
await collection.find().toArray();

// 关闭连接
await client.close();

高级用法

本部分将展示一些高级的数据库操作,如预处理语句、事务处理等。

MySQL 数据库

// 预处理语句查询
const preparedStatement = new MySQLPreparedStatement(
  'INSERT INTO users (name, age) VALUES (?, ?)',
  [user.name, user.age]
);
await mysqlServer.execute(preparedStatement);

// 事务处理
await mysqlServer.beginTransaction();
try {
  await mysqlServer.query('INSERT INTO users (name, age) VALUES ("John", 30)');
  await mysqlServer.query('UPDATE users SET age = 31 WHERE name = "John"');
  await mysqlServer.commit();
} catch (error) {
  await mysqlServer.rollback();
}

MongoDB 数据库

// 插入数据
const user = { name: 'John', age: 30 };
await collection.insertOne(user);

// 更新数据
const query = { name: 'John' };
const update = { $set: { age: 31 } };
await collection.updateOne(query, update);

// 删除数据
await collection.deleteOne(query);

✨ 主要特性

MySQL 服务器

  • 预处理语句支持:防止 SQL 注入攻击。
  • 事务支持:确保数据库操作的原子性、一致性、隔离性和持久性。
  • 结果集遍历:高效地处理大数据查询。

MongoDB 数据库

  • 异步驱动:非阻塞式操作,提高性能。
  • 工作区支持:在开发环境中自动加载和保存数据。
  • 连接池管理:优化资源利用。

🚫 安全提示

MySQL 安全性

⚠️ 重要提示

  • 始终使用预处理语句来防止 SQL 注入攻击。
  • 为数据库连接创建具有最少权限的用户。
  • 在生产环境中启用 SSL 证书。

MongoDB 安全性

⚠️ 重要提示

  • 始终启用身份验证。
  • 使用 SSL/TLS 加密通信。
  • 限制对敏感集合和字段的访问。

❗ 错误处理

常见问题

  1. 连接超时

    • 检查数据库服务是否运行。
    • 确保防火墙允许相关端口。
  2. 权限错误

    • 验证用户凭证是否正确。
    • 检查用户是否有足够的权限。
  3. 数据格式问题

    • 确保插入的数据与集合定义的模式一致。
    • 使用调试日志检查数据结构。

日志记录

💡 使用建议

  • 在生产环境中启用详细的日志记录以跟踪数据库操作。
  • 定期审查日志文件以检测潜在的安全威胁和性能问题。

总结

这个项目提供了一个强大的工具集,用于同时管理 MySQL 和 MongoDB 数据库。通过预处理语句、事务支持和异步驱动等功能,确保了数据操作的安全性和效率。建议在生产环境中结合使用 SSL 证书和身份验证机制来进一步增强安全性。

help

运行方式说明

cloud

托管运行

托管运行通常表示这个 MCP Server 由服务方环境承载,用户一般按页面提供的连接方式或授权流程接入,不需要在本地长期启动一个 MCP 进程

  1. 打开服务方连接页
  2. 完成授权或复制端点
  3. 在 MCP 客户端中连接
terminal

本地运行 / 其它方式

本地运行通常需要用户在自己的电脑或服务器上安装依赖,把 server_config 复制到 MCP 客户端,并按 env_schema 补齐环境变量、密钥或其它配置

  1. 复制 server_config
  2. 安装所需依赖
  3. 补齐环境变量后重启客户端