“文档” 菜单
文档首页
/ /
Atlas
/

保存连接设置

在此页面上

  • 找到配置文件
  • 创建配置文件
  • 更新配置文件
  • 使用配置文件运行命令

您可以将常用的连接设置另存为配置文件。 配置文件存储项目 ID、组织 ID 以及可选的 API密钥,以便在将来的 Atlas CLI 会话中使用。 为了节省时间,您可以指定配置文件,而不是在每个命令中使用 --projectId--orgId标志。 Atlas CLI 将您的配置文件存储在名为config.toml配置文件中。

注意

存储在环境变量中的任何设置都优先于存储在配置文件中的设置。使用--projectId--orgId标志指定的任何项目或组织都优先于配置文件和环境变量。

Atlas CLI 将配置文件保存到以下位置,具体取决于您的操作系统:

%AppData/atlascli
/Users/{username}/Library/Application Support/atlascli
$XDG_CONFIG_HOME/atlascli

默认情况下,Atlas CLI 会将配置文件保存在$XDG_CONFIG_HOME 环境变量中定义的路径中。您可以将$XDG_CONFIG_HOME 变量中定义的路径修改为您的首选位置。要了解有关修改$XDG_CONFIG_HOME 变量的更多信息,请参阅 XDG 基本目录规范。

如果未设置$XDG_CONFIG_HOME ,Atlas CLI 将使用:

$HOME/.config/atlascli

Atlas CLI 向运行命令的用户授予对该文件的读写访问权限。

首次运行atlas auth loginatlas config init命令时,Atlas CLI 会自动创建config.toml文件和默认配置文件。 如果运行命令时未指定配置文件、环境变量或--projectId--orgId标志,Atlas CLI 将使用该命令的默认配置文件。

根据您的使用案例选择连接方法:

命令
身份验证方法
用例(Use Case)
atlas auth login
Atlas 登录凭证和身份验证令牌
最适合非编程使用
atlas config init
API 密钥
最适合编程使用

要了解更多信息,请参阅选择连接方法。

  • 安装 Atlas CLI。

  • 将您主机的 IP 地址添加到 IP 访问列表中。

  • 如果选择atlas config init作为连接方法,则必须配置 API 密钥。

  • 如果您的 Atlas CLI 安装在防火墙后面,并且您想使用代理URL ,请设置HTTP_PROXYHTTPS_PROXY环境变量。

    重要

    Atlas CLI 支持httphttpssocks5方案。 您必须在代理服务的访问列表中指定cloud.mongodb.com/作为主目标 URL。 如果您的代理配置启用了身份验证,则还必须指定用户名和密码。

    要了解详情,请参阅 代理服务器。

选择一个使用案例,然后按照步骤创建配置文件。

选择默认配置文件或命名配置文件。

创建默认配置文件以:

  • 在不指定配置文件、环境变量或--projectId--orgId标志的情况下运行命令。

  • 使用--profile default标志。

创建命名配置文件以使用--profile <profileName>标志。

按照以下步骤创建默认配置文件。如果默认配置文件已存在,这些命令将更新默认配置文件的值。

1

在终端中运行 atlas auth login 命令。

atlas auth login

该命令会打开一个浏览器窗口,并返回一个一次性激活码。此激活码会在 10 分钟后过期。

2

如果您尚未登录,请在浏览器中登录您的 Atlas 帐户。

3

将激活代码粘贴到浏览器中,然后单击 Confirm Authorization

4

返回终端。如果连接成功,您将看到一条消息:

Successfully logged in as {Your Email Address}.

5

Down ArrowUp Arrow键突出显示所需的组织,然后按Enter

? Choose a default organization: [Use arrows to move, type to filter]
> Org1 (5e39bf1212121e685774c81c)

注意

如果您有权访问超过 500 个组织,则必须先输入组织 ID 或组织名称来过滤结果,然后使用箭头选择相应的组织。

6

Down ArrowUp Arrow键突出显示所需的项目,然后按Enter

? Choose a default project: [Use arrows to move, type to filter]
Project1 (5e5ebffd0c04a97009061234)
Project2 (5cfacee6014b761b07f15678)
> Project3 (5e39bf4979358e6857741212)
Project4 (5c815cc7014b768fb67e3434)
7

Down ArrowUp Arrow键突出显示所需的输出格式,然后按Enter

? Default Output Format: [Use arrows to move, type to filter]
> plaintext
json
选项
说明
plaintext
人类可读的输出,包括
Atlas CLI 返回。
json
JSON 输出,包括 MongoDB CLI 返回的所有字段。
8

Atlas CLI 使用指定的mongosh路径,以便您可以访问部署。默认值为/usr/local/bin/mongosh 。按Enter接受默认值,或在系统上指定mongosh的路径,然后按Enter

? Default MongoDB Shell Path: [? for help] (/usr/local/bin/mongosh)
9

查看配置文件或运行atlas config describe命令以验证您的配置文件。 对于默认配置文件, <profileName>default

atlas config describe <profileName>

该命令会返回以下设置。为了安全起见,Atlas CLI 会编辑访问令牌并刷新令牌值。

SETTING VALUE
"access_token": "redacted",
"mongosh_path": "/usr/local/bin/mongosh",
"org_id": "60c9877baf349d6fc4fd9744",
"output": "json",
"project_id": "60c94857241ae99848af45ad",
"refresh_token": "redacted",
"service": "cloud"

按照以下步骤创建具有自定义名称的配置文件。

1

在终端中使用profile <profileName>标志运行atlas auth login命令。 <profileName>应是新配置文件所需的名称。

atlas auth login --profile myProfile

该命令会打开一个浏览器窗口,并返回一个一次性激活码。此激活码会在 10 分钟后过期。

2

如果您尚未登录,请在浏览器中登录您的 Atlas 帐户。

3

将激活码粘贴到浏览器中,然后单击 Confirm Authorization

4

返回终端。如果连接成功,您将看到一条消息:

Successfully logged in as {Your Email Address}.

5

Down ArrowUp Arrow键突出显示所需的组织,然后按Enter

? Choose a default organization: [Use arrows to move, type to filter]
> Org1 (5e39bf1212121e685774c81c)

注意

如果您有权访问超过 500 个组织,则必须先输入组织 ID 或组织名称来过滤结果,然后使用箭头选择相应的组织。

6

Down ArrowUp Arrow键突出显示所需的项目,然后按Enter

? Choose a default project: [Use arrows to move, type to filter]
Project1 (5e5ebffd0c04a97009061234)
Project2 (5cfacee6014b761b07f15678)
> Project3 (5e39bf4979358e6857741212)
Project4 (5c815cc7014b768fb67e3434)
7

Down ArrowUp Arrow键突出显示所需的输出格式,然后按Enter

? Default Output Format: [Use arrows to move, type to filter]
> plaintext
json
选项
说明
plaintext
人类可读的输出,包括
Atlas CLI 返回。
json
JSON 输出,包括 MongoDB CLI 返回的所有字段。
8

Atlas CLI 使用指定的mongosh路径,以便您可以访问部署。默认值为/usr/local/bin/mongosh 。按Enter接受默认值,或在系统上指定mongosh的路径,然后按Enter

? Default MongoDB Shell Path: [? for help] (/usr/local/bin/mongosh)
9

查看配置文件或运行atlas config describe命令以验证您的配置文件。 对于默认配置文件, <profileName>default

atlas config describe <profileName>

该命令会返回以下设置。为了安全起见,Atlas CLI 会编辑访问令牌并刷新令牌值。

SETTING VALUE
"access_token": "redacted",
"mongosh_path": "/usr/local/bin/mongosh",
"org_id": "60c9877baf349d6fc4fd9744",
"output": "json",
"project_id": "60c94857241ae99848af45ad",
"refresh_token": "redacted",
"service": "cloud"

选择默认配置文件或命名配置文件。

创建默认配置文件以:

  • 在不指定配置文件、环境变量或--projectId--orgId标志的情况下运行命令。

  • 使用--profile default标志。

创建命名配置文件以使用--profile <profileName>标志。

按照以下步骤创建默认配置文件。如果默认配置文件已存在,这些命令将更新默认配置文件的值。

1

在终端中运行 atlas config init 命令。

atlas config init
2

根据提示输入公钥和私钥。

3

Down ArrowUp Arrow键突出显示所需的组织,然后按Enter

? Choose a default organization: [Use arrows to move, type to filter]
> Org1 (5e39bf1212121e685774c81c)
4

Down ArrowUp Arrow键突出显示所需的项目,然后按Enter

? Choose a default project: [Use arrows to move, type to filter]
Project1 (5e5ebffd0c04a97009061234)
Project2 (5cfacee6014b761b07f15678)
> Project3 (5e39bf4979358e6857741212)
Project4 (5c815cc7014b768fb67e3434)
5

Down ArrowUp Arrow键突出显示所需的输出格式,然后按Enter

? Default Output Format: [Use arrows to move, type to filter]
> plaintext
json
选项
说明
plaintext
人类可读的输出,包括 Atlas CLI 返回的所有字段。
json
JSON 输出,包括 MongoDB CLI 返回的所有字段。
6

Atlas CLI 使用指定的mongosh路径,以便您可以访问部署。默认值为/usr/local/bin/mongosh 。按Enter接受默认值,或在系统上指定mongosh的路径,然后按Enter

? Default MongoDB Shell Path: [? for help] (/usr/local/bin/mongosh)
7

查看配置文件或运行atlas config describe命令以验证您的配置文件。 对于默认配置文件, <profileName>default

atlas config describe <profileName>

该命令返回以下设置。为了安全起见,Atlas CLI 会编辑API密钥值。

SETTING VALUE
"mongosh_path": "/usr/local/bin/mongosh",
"org_id": "60c9877baf349d6fc4fd9744",
"output": "json",
"private_api_key": "redacted",
"project_id": "60c94857241ae99848af45ad",
"public_api_key": "redacted",
"service": "cloud"

按照以下步骤创建具有自定义名称的配置文件。

1

在终端中使用profile <profileName>标志运行atlas config init命令。 <profileName>应是新配置文件所需的名称。

atlas config init --profile myProfile
2

根据提示输入公钥和私钥。

3

Down ArrowUp Arrow键突出显示所需的组织,然后按Enter

? Choose a default organization: [Use arrows to move, type to filter]
> Org1 (5e39bf1212121e685774c81c)
4

Down ArrowUp Arrow键突出显示所需的项目,然后按Enter

? Choose a default project: [Use arrows to move, type to filter]
Project1 (5e5ebffd0c04a97009061234)
Project2 (5cfacee6014b761b07f15678)
> Project3 (5e39bf4979358e6857741212)
Project4 (5c815cc7014b768fb67e3434)
5

Down ArrowUp Arrow键突出显示所需的输出格式,然后按Enter

? Default Output Format: [Use arrows to move, type to filter]
> plaintext
json
选项
说明
plaintext
人类可读的输出,包括 Atlas CLI 返回的所有字段。
json
JSON 输出,包括 MongoDB CLI 返回的所有字段。
6

Atlas CLI 使用指定的mongosh路径,以便您可以访问部署。默认值为/usr/local/bin/mongosh 。按Enter接受默认值,或在系统上指定mongosh的路径,然后按Enter

? Default MongoDB Shell Path: [? for help] (/usr/local/bin/mongosh)
7

查看配置文件或运行atlas config describe命令以验证您的配置文件。 对于默认配置文件, <profileName>default

atlas config describe <profileName>

该命令返回以下设置。为了安全起见,Atlas CLI 会编辑API密钥值。

SETTING VALUE
"mongosh_path": "/usr/local/bin/mongosh",
"org_id": "60c9877baf349d6fc4fd9744",
"output": "json",
"private_api_key": "redacted",
"project_id": "60c94857241ae99848af45ad",
"public_api_key": "redacted",
"service": "cloud"

您可以通过以下方式更新配置文件中存储的设置:

  • 使用文本编辑器编辑config.toml文件。

  • 运行atlas config set命令进行设置。 这将编辑config.toml文件中的单个值。

要使用配置文件运行 Atlas CLI 命令,请执行以下操作:

  • 创建配置文件。

  • --profile <profileName>标志附加到命令或省略--profile <profileName>标志以使用默认配置文件。

例子

此命令使用名为myProfile的配置文件:

atlas <command> --profile myProfile

此命令使用默认配置文件:

atlas <command>

后退

从 Atlas CLI 连接

来年

迁移到 Atlas CLI