Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The Linux platform installs MongoDB


May 17, 2021 MongoDB


Table of contents


The Linux platform installs MongoDB


Download

MongoDB offers 32-bit and 64-bit installation packages on the linux platform, which you can download on your website.

We need to install each Linux platform dependency package before installation.

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/try/download/community


The Linux platform installs MongoDB

The Linux platform installs MongoDB

Installation

Here we select tgz to download, download the installation package, and unziw the tgz (the installation on the 64-bit Linux is shown below).

wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1604-4.2.8.tgz    # 下载
tar -zxvf mongodb-linux-x86_64-ubuntu1604-4.2.8.tgz                                    # 解压
mv mongodb-src-r4.2.8  /usr/local/mongodb4                          # 将解压包拷贝到指定目录

MongoDB's executables are located in the bin directory, so you can add them to the PATH path:

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

the installation path for your MongoDB. As in this article /usr/local/mongodb4.

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

Create a database directory

MongoDB's data is stored in the db directory of the data directory, but this directory is not created automatically during the installation process, so you need to create the data directory manually and the db directory in the data directory.

In the following example, we create the data directory under the root (/).

Note: /data/db is mongoDB's default startup database path (-dbpath).

mkdir -p /data/db

The MongoDB service runs on the command line

You can then execute the mongod command from the bin directory in the mongo installation directory to start the mongdb service.

Note: If your database directory is not /data/db, you can specify it by --dbpath.

$ ./mongod
2015-09-25T16:39:50.549+0800 I JOURNAL  [initandlisten] journal dir=/data/db/journal
2015-09-25T16:39:50.550+0800 I JOURNAL  [initandlisten] recover : no journal files present, no recovery needed
2015-09-25T16:39:50.869+0800 I JOURNAL  [initandlisten] preallocateIsFaster=true 3.16
2015-09-25T16:39:51.206+0800 I JOURNAL  [initandlisten] preallocateIsFaster=true 3.52
2015-09-25T16:39:52.775+0800 I JOURNAL  [initandlisten] preallocateIsFaster=true 7.7 

MongoDB manages the shell in the background

If you need to go into MongoDB background management, you need to open the bin directory under the Mongodb directory and then execute the mongo command file.

MongoDB Shell is an interactive Javascript shell that mongoDB brings with it, an interactive environment for operation and management of MongoDB.

When you go into the mongoDB background, it links to the test document (database) by default:

$ cd /usr/local/mongodb/bin
$ ./mongo
MongoDB shell version: 3.0.6
connecting to: test
Welcome to the MongoDB shell.
……

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

> 2+2
4
> 3+6
9

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

> db.W3Cschool.insert({x:10})
WriteResult({ "nInserted" : 1 })
> db.W3Cschool.find()
{ "_id" : ObjectId("5604ff74a274a611b0c990aa"), "x" : 10 }
>

The first command is to insert data 8 into the z field of the w3r collection (table).



MongoDb web user interface

MongoDB provides a simple HTTP user interface. If you want to enable this feature, you need to specify the parameter --rest at startup.

$ ./mongod --dbpath=/data/db --rest

MongoDB's web interface accesses 1000 more ports than the service.

If your MongoDB run port uses the default 27017, you can access the web user interface with port number 28017, i.e. address: http://localhost:28017.

The Linux platform installs MongoDB