MongoDB MCP(MongoDB Command Line Process)服务器是一款功能强大的工具,可通过命令行界面实现对 MongoDB 数据库和 Atlas 云服务的管理,为数据库管理工作带来了极大的便利。以下将详细介绍其功能及使用方法。
MongoDB MCP 服务器支持通过命令行界面管理 MongoDB 数据库和 Atlas 云服务。你可以按照后续的配置方法进行设置,然后使用提供的各种工具和命令开展数据库管理工作。
MCP 提供了一系列核心工具,涵盖了数据库交互、备份恢复、数据导入导出等多个方面:
MCP 还提供了一系列与 Atlas 相关的命令,方便对 Atlas 资源进行管理:
mongoatlas clusters list
:列出所有集群。mongoatlas projects list
:列出所有项目。mongoatlas dbs.list
:获取数据库列表。mongoatlas users.list
:获取用户列表。mongoatlas metrics.getClusterMetrics
:获取集群性能指标。本部分文档未提及安装步骤,若有相关需求,请参考官方文档或联系技术支持。
设置环境变量时,需使用前缀 MDB_MCP_
加上大写的选项名称:
# 设置 Atlas API 凭证
export MDB_MCP_API_CLIENT_ID="your-atlas-client-id"
export MDB_MCP_API_CLIENT_SECRET="your-atlas-client-secret"
# 设置自定义 MongoDB 连接字符串
export MDB_MCP_CONNECTION_STRING="mongodb+srv://username:password@cluster.mongodb.net/myDatabase"
# 设置日志路径
export MDB_MCP_LOG_PATH="/path/to/logs"
{
"mcpServers": {
"MongoDB": {
"command": "npx",
"args": ["-y", "mongodb-mcp-server"],
"env": {
"MDB_MCP_CONNECTION_STRING": "mongodb+srv://username:password@cluster.mongodb.net/myDatabase"
}
}
}
}
{
"mcpServers": {
"MongoDB": {
"command": "npx",
"args": ["-y", "mongodb-mcp-server"],
"env": {
"MDB_MCP_API_CLIENT_ID": "your-atlas-client-id",
"MDB_MCP_API_CLIENT_SECRET": "your-atlas-client-secret"
}
}
}
}
通过命令行传递配置选项:
npx -y mongodb-mcp-server --apiClientId="your-atlas-client-id" --apiClientSecret="your-atlas-client-secret" --connectionString="mongodb+srv://username:password@cluster.mongodb.net/myDatabase" --logPath=/path/to/logs
{
"mcpServers": {
"MongoDB": {
"command": "npx",
"args": [
"-y",
"mongodb-mcp-server",
"--connectionString",
"mongodb+srv://username:password@cluster.mongodb.net/myDatabase"
]
}
}
}
{
"mcpServers": {
"MongoDB": {
"command": "npx",
"args": [
"-y",
"mongodb-mcp-server",
"--apiClientId",
"your-atlas-client-id",
"--apiClientSecret",
"your-atlas-client-secret"
]
}
}
}
使用以下命令创建用户:
mongoatlas users.create --projectId \
--email \
--username \
--password
获取数据库读写权限的命令如下:
mongoatlas dbusers.grantAccess --projectId \
--db \
--user
如果您希望为 MongoDB MCP 项目做出贡献,请参考以下步骤:
git clone https://github.com/mongodb/mongo-mcp.git
npm install
npm test
如遇任何问题,请访问 MongoDB MCP 项目主页 提交 issue。
mongo --db --eval 'db.createDatabase()'
mongo --db --eval 'db.dropDatabase()'
mongodump --db --out /path/to/backup
mongorestore --db /path/to/backup/. BSON
如需更多信息,请参考官方文档或联系技术支持。