对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

创建并配置 Atlas 集群

本教程演示如何使用Atlas 设置命令:

  1. 在 Atlas 项目中创建一个集群。

  2. 示例数据加载到 Atlas 集群。

  3. 将您的 IP 地址添加到项目的 IP 访问列表中。

  4. 为 Atlas 集群创建 MongoDB 用户。

  5. 使用 MongoDB Shell mongosh连接到新集群。

要在创建新 Atlas 帐户的同时执行所有这些步骤,请参阅Atlas 入门。

要使用配置文件创建Atlas 集群,请使用--file选项而不是atlas setup运行Atlas 集群 create命令。要了解有关 Atlas 集群配置文件的更多信息,请参阅集群配置文件。

您还可以使用atlas setup创建 Atlas 帐户并通过 Atlas 进行身份验证。 要了解更多信息,请参阅Atlas 入门。

在开始之前,请完成以下任务:

确认 Atlas API 在端口 443 (HTTPS) 上的出站访问,以及 MongoDB 连接在端口 27017 上的出站访问。

使用atlas setup命令在 Atlas 中创建M0集群。 M0集群存在一些操作限制。

可以通过以下方式运行该命令:

  • 默认设置模式:该命令使用默认设置创建示例共享层级集群。

  • 交互模式:该命令会提示您输入集群设置并提供默认值。

  • 非交互模式:使用选项运行命令。

单击该标签页可查看适合您首选模式的命令。

该命令将创建一个示例共享层集群,其默认设置如下:

  • 集群名称: Cluster<number>

  • 服务提供商: AWS

  • 提供商地区: US_EAST_1

  • 集群层: M0

  • 磁盘大小: 0.5 GB

  • 数据库用户名: Cluster<number>

  • 数据库用户密码: abcdef12345

  • 允许来自以下 IP 地址的连接: <YourIPAddress>

  • 加载样本数据: Yes

  • 打开shell : No

注意

密码是自动生成的随机值。 abcdef12345是一个示例值。

atlas setup --force
We are deploying Cluster9876543...
Please store your database authentication access details in a secure location
Database User Username: Cluster9876543
Database User Password: abcdef12345
Creating your cluster... [Its safe to 'Ctrl + C']
Cluster created.
Your connection string: mongodb+srv://cluster9876543.example.mongodb.net
Loading sample data into your cluster... [Its safe to 'Ctrl + C']
Now you can connect to your Atlas cluster with: mongosh -u Cluster9876543 -p abcdef12345 mongodb+srv://cluster9876543.example.mongodb.net

该命令会提示您进行集群设置并提供默认选项。 出现提示时,按Y ,然后按Enter接受默认设置。

atlas setup
Press [Enter] to use the default values.
Enter [?] on any option to get help.
[Default Settings]
Cluster Name: Cluster9876543
Cloud Provider and Region: AWS - US_EAST_1
Database User Username: Cluster9876543
Load sample data: Yes
Allow connections from (IP Address): <your-IP>
? Do you want to set up your first free database in Atlas with default settings (it's free forever)? Yes
We are deploying Quickstart-9876543...
Please store your database authentication access details in a secure location:
Database User Username: Cluster9876543
Database User Password: abcdef12345
Creating your cluster... [Its safe to 'Ctrl + C']
Cluster created.
Your connection string: mongodb+srv://cluster9876543.example.mongodb.net
Loading sample data into your cluster... [Its safe to 'Ctrl + C']
Now you can connect to your Atlas cluster with: mongosh -u Cluster9876543 -p abcdef12345 mongodb+srv://cluster9876543.example.mongodb.net

该命令将创建具有以下设置的示例共享层集群:

  • 集群名称: getStarted

  • 服务提供商: AWS

  • 提供商地区: US_EAST-1

  • 集群层: M0

  • 磁盘大小: 2 GB

  • MongoDB版本: 5.0

  • 副本集节点: 3

atlas setup --clusterName getStarted --provider AWS --region US_EAST_1 --username testUser --password changeMe --accessListIp 192.0.2.15 --skipSampleData --force
We are deploying getStarted...
Please store your database authentication access details in a secure location:
Database User Username: testUser
Database User Password: changeMe
Creating your cluster... [Its safe to 'Ctrl + C']
Now you can connect to your |service| cluster with: mongosh -u testUser -p changeMe mongodb+srv://getStarted.example.mongodb.net

在集群预配后,请在连接前确认其已就绪。

检查点
验证步骤

已安装 CLI

运行 atlas --version。该命令返回版本信息。

已验证

运行 atlas auth whoami。该命令返回您的身份验证用户。

集群已创建

CLI 输出显示带有连接字符串的 Cluster created.

IP 访问列表已更新

CLI 输出确认您的 IP 地址已添加。

连接成功

运行 atlas clusters connect <cluster-name> 或使用连接字符串连接到mongosh

创建并验证 Atlas 集群后,您可以:

  • 连接您的应用程序:将连接字符串与 mongoshMongoDB 驱动程序一起使用。

  • 配置安全:将 IP 地址添加到访问列表、创建应用程序特定的数据库用户,或为生产工作负载启用私有端点。

  • 加载数据:在设置过程中使用 --loadSampleData 标志,或使用 mongoimportmongorestore 加载自己的数据。

  • 查看集群状态登录到用户界面或在 Atlas CLI 中运行 atlas clusters 命令。

  • 扩展或修改集群:请参阅 修改集群。