MongoDB Atlas 云数据库入门
学习在 MongoDB Atlas 上创建、连接和管理云数据库集群。 · 难度:入门 · +15XP
什么是 MongoDB Atlas?
MongoDB Atlas 是 MongoDB 官方的云数据库服务,支持 AWS、Azure 和 GCP。它提供自动备份、监控、弹性伸缩等托管功能,让你无需管理服务器即可运行 MongoDB。
创建免费集群
访问 MongoDB Atlas 并注册。在“Clusters”页面点击“Create”,选择 M0 Sandbox(免费层)。
| 选项 | 说明 |
|---|---|
| Provider | AWS / Azure / GCP |
| Region | 选择离你最近的区域 |
| Cluster Tier | M0 (共享 RAM, 512 MB 存储) |
连接集群
创建用户和 IP 白名单后,点击“Connect”获取连接字符串:
mongosh "mongodb+srv://cluster0.xxxxx.mongodb.net/myDatabase" --username yourUser在 Node.js 中使用 Mongoose 连接:
const mongoose = require('mongoose');
mongoose.connect('mongodb+srv://user:pass@cluster0.xxxxx.mongodb.net/myDB?retryWrites=true&w=majority');练习提示
创建一个免费 Atlas 集群,插入一条文档并查询。尝试在 Atlas 的“Collections”标签页直接操作数据。