How to install MongoDB on CentOS 7

Introduction

MongoDB is a document-oriented database that is free and open source software. It is classified as a NoSQL database because it does not rely on the traditional table-based relational database structure. Instead, it uses JSON-like documents and dynamic patterns. Unlike a relational database, MongoDB does not require a predefined schema before adding data to the database. You can change the schema at any time as needed, without setting up a new database with the updated schema.

This tutorial will guide you to install MongoDB Community Edition on CentOS 7 server.

Preparation

A CentOS server with a non-root account that can use the sudo command has been set up, and the firewall has been turned on. Students who don’t have a server can buy it from here, but I personally recommend you to use the free Tencent Cloud Developer Lab for experimentation, and then buy server.

Step 1-Add MongoDB repository

The mongodb-org package does not exist in the default repository of CentOS. However, MongoDB maintains a dedicated repository. We add it to our server.

Use the vi editor to create a .repo file for the yum package of the CentOS management utility:

sudo vi /etc/yum.repos.d/mongodb-org.repo

Then, visit the Install on Red Hat section of the MongoDB document and add the latest stable version of the repository information to the file:

[ mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc

Save and close the file.

Before we continue, we should verify the existence of the MongoDB repository in the yum utility. The repolist command displays a list of enabled repositories:

yum repolist
...
repo id                          repo name
base/7/x86_64                    CentOS-7- Base
extras/7/x86_64                  CentOS-7- Extras
mongodb-org-3.2/7/x86_64         MongoDB Repository
updates/7/x86_64                 CentOS-7- Updates
...

With this MongoDB Repository, let us continue to install.

Step 2-Install MongoDB

We can use the yum utility to install the mongodb-org package from a third-party repository.

sudo yum install mongodb-org

There are two Is this ok [y/N]: prompts. The first allows the installation of the MongoDB package, and the second allows the import of GPG keys. MongoDB publishers sign their software and yum uses the key to confirm the integrity of the downloaded package. At each prompt, type Y and press the ENTER key.

Next, start the MongoDB service using the systemctl utility:

sudo systemctl start mongod

Although we will not use them in this tutorial, you can also use the reload and stop commands to change the status of the MongoDB service.

The reload command requests the mongod process to read the configuration file /etc/mongod.conf and apply any changes without restarting.

sudo systemctl reload mongod

The stop command will suspend all running mongod processes.

sudo systemctl stop mongod

The systemctl utility does not provide results after executing the start command, but we can check whether the service has been started by looking at the end of the file through mongod.log using the following tail command:

sudo tail /var/log/mongodb/mongod.log
...[ initandlisten] waiting for connections on port 27017

Waiting for the output of the connection to confirm that MongoDB has been successfully started, we can use MongoDB Shell to access the database server:

mongo

**Note: **When you start MongoDB Shell, you may see the following warning:

** WARNING: soft rlimits too low. rlimits set to 4096 processes,64000 files. Number of processes should be at least 32000:0.5 times number of files.

MongoDB is a threaded application. It can start other processes to handle its workload. A warning statement, for MongoDB, the most effective is that the number of processes it is authorized to start should be half of the number of files that it can open at any given time. To remove the warning, please edit the mongod``20-nproc.conf file to change the soft rlimit value of processes:

sudo vi /etc/security/limits.d/20-nproc.conf

Add the following line to the end of the file:

...
mongod soft nproc 32000

To make the new restrictions available to MongoDB, restart it using the systemctl utility:

sudo systemctl restart mongod

After that, when you connect to the MongoDB Shell, the warning should no longer exist.

To understand how to interact with MongoDB from the shell, you can view the output of the db.help() method, which provides a list of methods for the db object.

db.help()
DB methods:
 db.adminCommand(nameOrDocument)- switches to 'admin' db, and runs command [ just calls db.runCommand(...)]
 db.auth(username, password)
 db.cloneDatabase(fromhost)
 db.commandHelp(name) returns the help for the command
 db.copyDatabase(fromdb, todb, fromhost)
 db.createCollection(name,{ size :..., capped :..., max :...})
 db.createUser(userDocument)
 db.currentOp() displays currently executing operations in the db
 db.dropDatabase()...

Let the mongod process run in the background, but use the following exit command to exit the shell:

exit
Bye

Step 3-Verify Start

Since database-driven applications cannot run without a database, we will ensure that the MongoDB daemon mongod will start from the system.

Use the systemctl utility to check its startup status:

systemctl is-enabled mongod; echo $?

The output is zero to confirm that the daemon is enabled and we need it. However, a disabled daemon that was confirmed to be unable to start.

...
enabled
0

If the daemon has been disabled, use the systemctl utility to enable it:

sudo systemctl enable mongod

We now have a running MongoDB instance, which will automatically start after the system restarts.

Step 4-Import the sample data set (optional)

Unlike other database servers, MongoDB has no data in its test database. Since we don't want to use production data to test the new software, we will download the sample data set from the "Import Sample Data Set" section of the "Introduction to MongoDB" document. The JSON document contains a series of restaurants, and we will use it to practice the interaction with MongoDB to avoid harm to sensitive data.

First enter the writable directory:

cd /tmp

Use the curl command and the link in MongoDB to download the JSON file:

curl -LO https://raw.githubusercontent.com/mongodb/docs-assets/primer-dataset/primer-dataset.json

The mongoimport command inserts data into the test database. The --db flag defines which database to use, and the --collection flag specifies the location of information stored in the database. The --file flag tells the command on which file to perform the import operation:

mongoimport --db test --collection restaurants --file /tmp/primer-dataset.json

Output confirmation to import data from the primer-dataset.json file:

connected to: localhost
imported 25359 documents

After the sample data set is in place, we will execute a query on it.

Restart MongoDB Shell:

mongo

The shell selects the test database by default, which is where we import data.

Use the find() method to query the restaurants collection to display a list of all restaurants in the dataset. Since the collection contains more than 25,000 entries, use the optional limit() method to reduce the output of the query to the specified number. In addition, the pretty() method uses line breaks and indentation to make the information easier to read.

db.restaurants.find().limit(1).pretty()
{"_ id":ObjectId("57e0443b46af7966d1c8fa68"),"address":{"building":"1007","coord":[-73.856077,40.848447],"street":"Morris Park Ave","zipcode":"10462"},"borough":"Bronx","cuisine":"Bakery","grades":[{"date":ISODate("2014-03-03T00:00:00Z"),"grade":"A","score":2},{"date":ISODate("2013-09-11T00:00:00Z"),"grade":"A","score":6},{"date":ISODate("2013-01-24T00:00:00Z"),"grade":"A","score":10},{"date":ISODate("2011-11-23T00:00:00Z"),"grade":"A","score":9},{"date":ISODate("2011-03-10T00:00:00Z"),"grade":"B","score":14}],"name":"Morris Park Bake Shop","restaurant_id":"30075445"}

You can continue to use the sample data set to familiarize yourself with MongoDB or use the following db.restaurants.drop() method to delete it:

db.restaurants.drop()

Finally, use the following exit command to exit the shell:

exit
Bye

in conclusion

In this tutorial, we introduced how to add a third-party repository in yum, install the MongoDB database server, import sample data sets, and perform simple queries. We barely touch the surface of MongoDB's functionality. You can use multiple collections to create your own database, populate them with many documents and start building powerful applications.

For more CentOS tutorials, please go to [Tencent Cloud + Community] (https://cloud.tencent.com/developer?from=10680) to learn more.

Reference: "How To Install MongoDB on CentOS 7"

Recommended Posts

How to install MongoDB on CentOS 7
How to install MongoDB on CentOS 8
How to install jdk1.8 on centOS7
How to install MySQL on CentOS 8
How to install Memcached on CentOS 8
How to install R on CentOS 8
How to install FFmpeg on CentOS 8
How to install Virtualbox on CentOS 8
How to install TensorFlow on CentOS 8
How to install TeamViewer on CentOS 8
How to install Perl 5 on CentOS
How to install Git on CentOS 8
How to install Gradle on CentOS 8
How to install Elasticsearch on CentOS 8
How to install Jenkins on CentOS 8
How to install Java on CentOS 8
How to install Go on CentOS 8
How to install GCC on CentOS 8
How to install Yarn on CentOS 8
How to install MongoDB on Ubuntu 16.04
How to install Asterisk on CentOS 7
How to install Jenkins on CentOS 8
How to install Python 3.8 on CentOS 8
How to install Tomcat 9 on CentOS 8
How to install Webmin on CentOS 8
How to install Ruby on CentOS 8
How to install Skype on CentOS 8
How to install htop on CentOS 8
How to install Python on CentOS 8
How to install Elasticsearch on CentOS 8
How to install Postgresql on CentOS 8
How to install Wordpress on Centos
How to install htop on CentOS 8
How to install TeamViewer on CentOS 8
How to install MariaDB on CentOS 8
How to install Odoo 13 on CentOS 8
How to install Apache on CentOS 8
How to install OpenCV on CentOS 8
How to install PHP on CentOS 8
How to install Apache Maven on CentOS 8
How to install Apache Kafka on CentOS 7
R&D: How To Install Python 3 on CentOS 7
How to install GCC compiler on CentOS 7
How to install offline JDK1.8 on centos7.0
How to install Visual Studio Code on CentOS 8
How to install and use Docker on CentOS 7
How to install RPM packages on CentOS Linux
How to install and configure VNC on CentOS 8
How to install and use Composer on CentOS 8
How to install and configure Redis on CentOS 8
How to install Node.js and npm on CentOS 8
How to install jdk1.8.0_151 and mysql5.6.38 on centos7.2.1511
How to install and use Curl on CentOS 8
How to install and configure Owncloud on CentOS 8
How to install VirtualBox client extension on CentOS 8
How to install Docker CE on RHEL 8 / CentOS 8
How to install and uninstall tomcat on centos
How to install and configure Redmine on CentOS 8
How to install Ruby on Ubuntu 20.04
How to install Memcached on Ubuntu 20.04
How to install Java on Ubuntu 20.04