本项目提供了一个强大的工具集,用于同时管理 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 服务器进行数据库操作。
// 连接数据库
const mysqlServer = new MySQLServer({
host: 'localhost',
user: 'root',
password: 'password',
database: 'test'
});
// 查询数据
await mysqlServer.query('SELECT * FROM users');
// 关闭连接
await mysqlServer.close();
// 连接数据库
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();
本部分将展示一些高级的数据库操作,如预处理语句、事务处理等。
// 预处理语句查询
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();
}
// 插入数据
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);
⚠️ 重要提示
- 始终使用预处理语句来防止 SQL 注入攻击。
- 为数据库连接创建具有最少权限的用户。
- 在生产环境中启用 SSL 证书。
⚠️ 重要提示
- 始终启用身份验证。
- 使用 SSL/TLS 加密通信。
- 限制对敏感集合和字段的访问。
连接超时
权限错误
数据格式问题
💡 使用建议
- 在生产环境中启用详细的日志记录以跟踪数据库操作。
- 定期审查日志文件以检测潜在的安全威胁和性能问题。
这个项目提供了一个强大的工具集,用于同时管理 MySQL 和 MongoDB 数据库。通过预处理语句、事务支持和异步驱动等功能,确保了数据操作的安全性和效率。建议在生产环境中结合使用 SSL 证书和身份验证机制来进一步增强安全性。