Docs HomeMongoDB Shell

Install 安装mongosh

Prerequisites先决条件

To use the MongoDB Shell, you must have a MongoDB deployment to connect to.要使用MongoDB Shell,您必须有一个MongoDB部署来连接。

Supported MongoDB Versions支持的MongoDB版本

You can use the MongoDB Shell to connect to MongoDB version 4.2 or greater.您可以使用MongoDB Shell连接到MongoDB 4.2或更高版本。

Procedure过程

Select the appropriate tab for your operating system:为您的操作系统选择适当的选项卡:

Note

On Windows, mongosh preferences and configuration options are stored in the %APPDATA%/mongodb/mongosh directory.在Windows上,mongosh首选项和配置选项存储在%APPDATA%/mongodb/mongosh目录中。

Install from MSI从MSI安装

1

Open the MongoDB Shell download page.打开MongoDB Shell下载页面。

Open the MongoDB Download Center.打开MongoDB下载中心

2

In the Platform dropdown, select Windows 64-bit (8.1+) (MSI)平台下拉列表中,选择Windows 64位(8.1+)(MSI)

3

Click Download.单击“下载”。

4

Double-click the installer file.双击安装程序文件。

5

Follow the prompts to install mongosh.按照提示安装mongosh

Install from .zip File.zip文件安装

1

Open the MongoDB Shell download page.打开MongoDB Shell下载页面。

Open the MongoDB Download Center.打开MongoDB下载中心

2

Download the mongosh installation archive for your operating system.下载适用于您的操作系统的mongosh安装档案。

Download mongosh from the MongoDB Download Center.MongoDB下载中心下载mongosh

3

Extract the files from the downloaded archive.从下载的存档中提取文件。

Open a cmd terminal and run the following command from the directory that has the mongosh .zip archive:打开cmd终端,并从具有mongosh .zip存档的目录中运行以下命令:

tar -xf mongosh-1.10.1-win32-x64.zip

The extracted archive has a bin folder that contains two files, mongosh.exe and mongosh_crypt_v1.dll.提取的归档文件有一个bin文件夹,其中包含两个文件,mongosh.exemongosh_crypt_v1.dll

4

Add the mongosh binary to your PATH environment variable.mongosh二进制文件添加到PATH环境变量中。

Ensure that the extracted MongoDB Shell binary is in the desired location in your filesystem, then add that location to your PATH environment variable.确保提取的MongoDB Shell二进制文件位于文件系统中所需的位置,然后将该位置添加到PATH环境变量中。

To add the MongoDB Shell binary's location to your PATH environment variable:要将MongoDB Shell二进制文件的位置添加到PATH环境变量中,请执行以下操作:

  1. Open the Control Panel.打开“控制面板”。
  2. In the System and Security category, click System.在“系统和安全”类别中,单击“系统”。
  3. Click Advanced system settings. 单击“高级系统设置”。The System Properties modal displays.将显示“系统属性”模式。
  4. Click Environment Variables.单击“环境变量”。
  5. In the System variables section, select Path and click Edit. 在“系统变量”部分,选择“路径”,然后单击“编辑”。The Edit environment variable modal displays.将显示“编辑环境变量”模态。
  6. Click New and add the filepath to your mongosh binary.单击“新建”并将文件路径添加到您的mongosh二进制文件中。
  7. Click OK to confirm your changes. 单击“确定”确认您的更改。On each other modal, click OK to confirm your changes.在每个其他模态上,单击“确定”以确认您的更改。

To confirm that your PATH environment variable is correctly configured to find mongosh, open a command prompt and enter the mongosh --help command. 要确认PATH环境变量已正确配置为查找mongosh,请打开命令提示符并输入mongosh --help命令。If your PATH is configured correctly, a list of valid commands displays.如果PATH配置正确,将显示一个有效命令列表。

Install with Homebrew使用Homebrew安装

Important

To view the complete list of system requirements for Homebrew, see the Homebrew Website.要查看Homebrew系统要求的完整列表,请参阅Homebrew网站

The Homebrew package manager is the recommended installation method for mongosh on macOS. Homebrew软件包管理器是mongosh在macOS上的推荐安装方法。To learn how to manually install mongosh from an archive instead, see Install from .zip File.要了解如何从存档手动安装mongosh,请参阅.zip文件安装

Considerations注意事项

mongosh installed with Homebrew does not support automatic client-side field level encryption.与Homebrew一起安装的mongosh不支持自动客户端字段级加密

Procedure过程

To install mongosh with Homebrew:要使用Homebrew安装mongosh

1
Install Homebrew.安装Homebrew。

Refer to the Homebrew website for the steps to install Homebrew on macOS.有关在macOS上安装Homebrew的步骤,请参阅Homebrew网站。

2
Install the mongosh package.安装mongosh包装。

Issue the following command from the terminal to install the mongosh package:从终端发出以下命令以安装mongosh软件包:

brew install mongosh

Install from .zip File.zip文件安装

To manually install mongosh using a downloaded .zip file:要使用下载的.zip文件手动安装mongosh,请执行以下操作:

1

Open the MongoDB Shell download page.打开MongoDB Shell下载页面。

Open the MongoDB Download Center.打开MongoDB下载中心

2

Download the mongosh installation archive for your operating system.下载适用于您的操作系统的mongosh安装档案。

Download the appropriate version of mongosh for your operating system. 下载适用于您的操作系统的mongosh的适当版本。MongoDB provides versions of mongosh for Intel and ARM architectures.MongoDB为Intel和ARM架构提供了mongosh版本。

See the MongoDB Download Center.请参阅MongoDB下载中心

3

Extract the files from the downloaded archive.从下载的存档中提取文件。

Go to the directory that contains the mongosh .zip archive, then unpack the .zip file.转到包含mongosh .zip归档文件的目录,然后解压缩.zip文件。

If your computer is Intel based, run:如果您的计算机是基于英特尔的,请运行:

unzip mongosh-1.10.1-darwin-x64.zip

If your computer is ARM based (M1 or M2), run:如果您的计算机是基于ARM的(M1或M2),请运行:

unzip mongosh-1.10.1-darwin-arm64.zip

The extracted archive has a bin folder that contains two files, mongosh and mongosh_crypt_v1.dylib.提取的归档文件有一个bin文件夹,其中包含两个文件mongoshmongosh_crypt_v1dylib

If your web browser automatically extracts the archive as part of the download, or if you extract the archive without using the unzip command, you may need to make the binary executable.如果您的web浏览器在下载过程中自动提取存档,或者如果您在不使用unzip命令的情况下提取存档,则可能需要使二进制文件可执行。

To make the binary executable, run the following command in the directory where you extracted the archive:要使二进制文件可执行,请在提取存档的目录中运行以下命令:

chmod +x bin/mongosh
4

Add the downloaded binaries to your PATH environment variable.

You can either:

  • Copy the mongosh binary into a directory listed in your PATH variable, such as /usr/local/bin. Run the following commands from the directory where you extracted the download file:

    sudo cp mongosh /usr/local/bin/
    sudo cp mongosh_crypt_v1.so /usr/local/lib/
  • Create symbolic links to the MongoDB Shell. Switch to the directory where you extracted the files from the .tgz archive. Run the following command to create links to a directory already in your PATH such as /usr/local/bin.

    sudo ln -s $(pwd)/bin/* /usr/local/bin/
5

Allow macOS to run mongosh.

macOS may prevent mongosh from running after installation. If you receive a security error when starting mongosh indicating that the developer could not be identified or verified, perform the following actions:

  1. Open System Preferences.
  2. Select the Security and Privacy pane.
  3. Under the General tab, click the button to the right of the message about mongosh, labelled either Open Anyway or Allow Anyway depending on your version of macOS.

Select the appropriate tab based on your Linux distribution and desired package from the tabs below:

  • To install the .deb package on Ubuntu 22.04 (Jammy), Ubuntu 20.04 (Focal), Ubuntu 18.04 (Bionic), or Debian, click the .deb tab.
  • To install the .rpm package on RHEL or Amazon Linux 2, click the .rpm tab.
  • To install the .tgz tarball, click the .tgz tab.

Supported Platforms支持的平台

mongosh is available as a PPA for the following platforms:

  • Ubuntu 22.04 (Jammy)
  • Ubuntu 20.04 (Focal)
  • Ubuntu 18.04 (Bionic)

Procedure

1

Import the public key used by the package management system.导入包管理系统使用的公钥。

From a terminal, issue the following command to import the MongoDB public GPG key from https://www.mongodb.org/static/pgp/server-6.0.asc:

wget -qO- https://www.mongodb.org/static/pgp/server-6.0.asc | sudo tee /etc/apt/trusted.gpg.d/server-6.0.asc

The previous command writes the GPG key to your system's /etc/apt/trusted.gpg.d folder and prints the key to your terminal. You do not need to copy or save the key that is printed to the terminal.

If you receive an error indicating that gnupg is not installed, perform the following steps:

  1. Install gnupg and its required libraries using the following command:

    sudo apt-get install gnupg
  2. Retry importing the key:

    wget -qO- https://www.mongodb.org/static/pgp/server-6.0.asc | sudo tee /etc/apt/trusted.gpg.d/server-6.0.asc
2

Create a list file for MongoDB.

Create the list file /etc/apt/sources.list.d/mongodb-org-6.0.list for your version of Ubuntu.

Click on the appropriate tab for your version of Ubuntu. If you are unsure of what Ubuntu version the host is running, open a terminal or shell on the host and run lsb_release -dc.

The following instruction is for Ubuntu 22.04 (Jammy). 以下说明适用于Ubuntu 22.04(Jammy)。For other Ubuntu releases, click the appropriate tab.对于其他Ubuntu版本,请单击相应的选项卡。

Create the /etc/apt/sources.list.d/mongodb-org-6.0.list file for Ubuntu 22.04 (Jammy):

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
3

Reload local package database.重新加载本地程序包数据库。

Issue the following command to reload the local package database:发出以下命令以重新加载本地程序包数据库:

sudo apt-get update
4

Install the mongosh package.安装mongosh包装。

mongosh supports OpenSSL. You can also configure mongosh to use your system's OpenSSL installation.

To install the latest stable version of mongosh with the included OpenSSL libraries:

sudo apt-get install -y mongodb-mongosh

To install mongosh with your OpenSSL 1.1 libraries:

sudo apt-get install -y mongodb-mongosh-shared-openssl11

To install mongosh with your OpenSSL 3.0 libraries:

sudo apt-get install -y mongodb-mongosh-shared-openssl3
5

Confirm that mongosh installed successfully.

To confirm that mongosh installed successfully, run the following command:

mongosh --version

Your terminal should respond with the version of mongosh you have installed.

Next Steps接下来的步骤

Once you successfully install mongosh, learn how to connect to your MongoDB deployment.成功安装mongosh后,学习如何连接到MongoDB部署