Sunday, May 9, 2021

Install MongoDB

 Author: MongoDB Documentation Team

This guide describes how to install MongoDB locally. If you would like to use MongoDB in the Cloud using Atlas, our managed database product, see Get Started with Atlas.

Time required: 10 minutes

What You’ll Need

MongoDB supports a variety of 64-bit platforms. Refer to the Supported Platforms table to verify that MongoDB is supported on the platform to which you wish to install it.

Procedure

Install MongoDB

NOTE

These instructions are for installing MongoDB directly from an archive file. If you would rather use your linux distribution’s package manager, refer to the installation instructions for your distribution in the MongoDB Manual.

1

Download the binary files for the desired release of MongoDB.

Download the binaries from the MongoDB Download Center.

2

Extract the files from the downloaded archive.

Extract the archive by double-clicking on the tar file or using the tar command from the command line, as in the following:

tar -xvzf <tgz file>
3

Copy the extracted archive to the target directory.

Copy the extracted folder to the location from which MongoDB will run.

4

Ensure the location of the binaries is in the PATH variable.

The MongoDB binaries are in the bin/ directory of the archive. To ensure that the binaries are in your PATH, you can modify your PATH.

For example, you can add the following line to your shell’s rc file (e.g. ~/.bashrc):

export PATH=<mongodb-install-directory>/bin:$PATH

Replace <mongodb-install-directory> with the path to the extracted MongoDB archive.

Run MongoDB

IMPORTANT

If you are using SELinux on a Red Hat Linux-based system, (Red Hat Enterprise Linux or CentOS Linux), you must configure SELinux to allow MongoDB to start. Refer to Configure SELinux for instructions.

1

Create the data directory

Before you start MongoDB for the first time, create the directory to which the mongod process will write data. By default, the mongod process uses the /data/db directory. If you create a directory other than this one, you must specify that directory in the dbpath option when starting the mongod process later in this procedure.

The following example command creates the default /data/db directory:

mkdir -p /data/db
2

Set permissions for the data directory

Before running mongod for the first time, ensure that the user account running mongod has read and write permissions for the directory.

sudo chown -R $USER:$USER /data/

3

Run MongoDB

To run MongoDB, run the mongod process at the system prompt. If necessary, specify the path of the mongod or the data directory. See the following examples.

Run without specifying paths

If your system PATH variable includes the location of the mongod binary and if you use the default data directory (i.e., /data/db), simply enter mongod at the system prompt:

mongod
Specify the path of the mongod

If your PATH does not include the location of the mongod binary, enter the full path to the mongod binary at the system prompt:

<path to binary>/mongod
Specify the path of the data directory

If you do not use the default data directory (i.e., /data/db), specify the path to the data directory using the --dbpath option:

mongod --dbpath <path to data directory>
4

Verify that MongoDB has started successfully

Verify that MongoDB has started successfully by checking the process output for the following line:

[initandlisten] waiting for connections on port 27017

The output should be visible in the terminal or shell window.

You may see non-critical warnings in the process output. As long as you see the log line shown above, you can safely ignore these warnings during your initial evaluation of MongoDB.

Summary

If you have successfully completed this guide, you have installed MongoDB and are ready to connect to your mongod instance and start inserting data.

1 comment:

  1. cái này phải chạy mongod ở 1 cửa sổ command line. Tắt cửa sổ đó là mongo server chết

    ReplyDelete