Easy Tutorial
❮ Mongodb Advanced Indexing Mongodb Autoincrement Sequence ❯

MongoDB Installation on Linux

MongoDB provides 64-bit installation packages for various Linux distributions, which you can download from the official website.

Before installation, we need to install the required dependencies for each Linux platform.

Red Hat/CentOS:

sudo yum install libcurl openssl

Ubuntu 18.04 LTS ("Bionic")/Debian 10 "Buster":

sudo apt-get install libcurl4 openssl

Ubuntu 16.04 LTS ("Xenial")/Debian 9 "Stretch":

sudo apt-get install libcurl3 openssl

MongoDB source download address: https://www.mongodb.com/download-center#community

Here, we choose to download the tgz package. After downloading the package and extracting the tgz (the following demonstration is for a 64-bit Linux installation).

wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1604-4.2.8.tgz    # Download
tar -zxvf mongodb-linux-x86_64-ubuntu1604-4.2.8.tgz                                    # Extract

mv mongodb-src-r4.2.8  /usr/local/mongodb4                          # Copy the extracted package to the specified directory

The executable files for MongoDB are located in the bin directory, so you can add them to the PATH environment variable:

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

<mongodb-install-directory> is the installation path of MongoDB. For example, /usr/local/mongodb4 in this article.

export PATH=/usr/local/mongodb4/bin:$PATH

Creating Database Directories

By default, MongoDB initializes the following two directories upon startup:

Before starting, we can create these directories and set the current user with read and write permissions:

sudo mkdir -p /var/lib/mongo
sudo mkdir -p /var/log/mongodb
sudo chown `whoami` /var/lib/mongo     # Set permissions
sudo chown `whoami` /var/log/mongodb   # Set permissions

Next, start the MongoDB service:

mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --fork

Open the /var/log/mongodb/mongod.log file and see the following information, indicating successful startup.

# tail -10f /var/log/mongodb/mongod.log
2020-07-09T12:20:17.391+0800 I  NETWORK  [listener] Listening on /tmp/mongodb-27017.sock
2020-07-09T12:20:17.392+0800 I  NETWORK  [listener] Listening on 127.0.0.1
2020-07-09T12:20:17.392+0800 I  NETWORK  [listener] waiting for connections on port 27017

MongoDB Background Management Shell

If you need to access the MongoDB background management, you need to open the bin directory under the MongoDB installation directory and execute the mongo command file.

The MongoDB Shell is an interactive JavaScript shell provided by MongoDB for interacting with and managing MongoDB.

When you enter the MongoDB backend, it defaults to linking to the test document (database):

$ cd /usr/local/mongodb4/bin
$ ./mongo
MongoDB shell version v4.2.8

Connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("2cfdafc4-dd56-4cfc-933a-187b887119b3") } MongoDB server version: 4.2.8 Welcome to the MongoDB shell. ...

Since it is a JavaScript shell, you can perform some simple arithmetic operations:

> 2+2
4
> 3+6
9

Now, let's insert some simple data and retrieve the inserted data:

> db.tutorialpro.insert({x:10})
WriteResult({ "nInserted" : 1 })
> db.tutorialpro.find()
{ "_id" : ObjectId("5f069bdb4e02f8baf90f1184"), "x" : 10 }
>

The first command inserts the number 10 into the x field of the tutorialpro collection.

mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --shutdown

Alternatively, it can be achieved within the mongo shell:

> use admin
switched to db admin
> db.shutdownServer()

For more installation methods, refer to the official documentation: https://docs.mongodb.com/manual/administration/install-on-linux/

❮ Mongodb Advanced Indexing Mongodb Autoincrement Sequence ❯