MongoDB Installation on Linux Platform

MongoDB provides a 64-bit installation package for each Linux distribution. You can download the installation package on the official website.

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

Download the installation package and extract tgz (the following demonstrates the installation on 64-bit Linux).

curl - O HTTPS : //fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz Download # 
tar - zxvf MongoDB - Linux - x86_64 - 3.0 . 6.tgz # decompression                                   
Mv mongodb - linux - x86_64 - 3.0 . 6 / / usr / local / mongodb                          # Copy the unpacked package to the specified directory

The MongoDB executable is located in the bin directory, so you can add it to your PATH path:

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

<mongodb-install-directory> is the installation path for your MongoDB. As in this article /usr/local/mongodb .

Create a database directory

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

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

Note: /data/db is the default startup database path (–dbpath) for MongoDB.

mkdir -p /data/db

Run the MongoDB service from the command line

You can start the merged service by executing the mongodb command in the bin directory of the mongo installation directory on the command line.

Note: If your database directory is not /data/db, you can specify it with –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 background management shell

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

The MongoDB Shell is an interactive Javascript shell that comes with MongoDB, an interactive environment for manipulating and managing MongoDB.

When you enter the mongoDB background, it will link 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’s 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.dbase.insert({x:10})
WriteResult({ "nInserted" : 1 })
> db.dbase.find()
{ "_id" : ObjectId("5604ff74a274a611b0c990aa"), "x" : 10 }
>

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

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.

Note : This feature is only available for MongoDB 3.2 and earlier versions.

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

MongoDB’s web interface access port is 1000 more than the service’s port.

If your MongoDB runtime port uses the default 27017, you can access the web user interface at port number 28017, which is located at https://localhost:28017.

MongoDB Installation on Mac OSX Platform

MongoDB provides a 64-bit installation package on the OSX platform. You can download the installation package on the official website.

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

Starting with the MongoDB 3.0 release, only systems with OS X 10.7 (Lion) and newer versions are supported.

Next, we use the curl command to download the installation:

# Enter /usr/local 
cd / usr / local

# Download 
sudo curl - O https : //fastdl.mongodb.org/osx/mongodb-osx-x86_64-3.4.2.tgz

# 解压
sudo tar - zxvf mongodb - osx - x86_64 - 3.4 . 2.tgz

# Renamed to mongodb directory

sudo mv mongodb-osx-x86_64-3.4.2 mongodb

After the installation is complete, we can add MongoDB’s binary command file directory (installation directory /bin) to the PATH path:

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

Install using brew

In addition, you can also use the OSX brew to install MongoDB:

sudo brew install mongodb

If you want to install the support TLS/SSL command as follows:

sudo brew install mongodb --with-openssl

Install the latest development version:

sudo brew install mongodb --devel

Run MongoDB

First, we create a database storage directory /data/db:

sudo mkdir -p /data/db

Start mongodb, the default database directory is /data/db:

sudo mongod

# If you did not create a global path PATH, you need to enter the following directory 
cd / usr / local / mongodb / bin
sudo ./mongod

Then open a terminal and enter the following command:

$ cd /usr/local/mongodb/bin 
$ ./mongo
MongoDB shell version v3.4.2
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.2
Welcome to the MongoDB shell.
……
> 1 + 1
2
>

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