Wednesday, 11 September 2019

How to Install MongoDB on CentOS 7

MongoDB is a free and open-source document database. It is classified as a NoSQL database which is different than traditional table-based SQL databases like MySQL and PostgreSQL.
In MongoDB, data is stored in flexible, JSON-like documents where fields can vary from document to document. It does not require a predefined schema and data structure can be changed over time.
In this tutorial, we will show you how to install and configure MongoDB Community Edition on a CentOS 7 server from the official MongoDB repositories.
Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges
At the time of writing this article, the latest version of MongoDB available from the official MongoDB repositories is version 4.0. Before continuing with the next step visit the Install on Red Hat section of MongoDB’s documentation and check if there is a new release available.
Follow the steps below to install the latest stable version of MongoDB on your CentOS server :
  1. Enabling MongoDB repository
    To add the MongoDB repository to your system, open your text editor and create a new YUM repository configuration file named mongodb-org.repo inside the /etc/yum.repos.d/ directory:
    /etc/yum.repos.d/mongodb-org.repo
    [mongodb-org-4.0]
    name=MongoDB Repository
    baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
    gpgcheck=1
    enabled=1
    gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
    Copy
    If you want to install an older version of MongoDB, replace each instance of 4.0 with your preferred version.
  2. Installing MongoDB
    Now that the repository is enabled you can install the mongodb-org meta-package using the yum utility:
    sudo yum install mongodb-org
    Copy
    During the installation yum will prompt you to import the MongoDB GPG key. Type y and hit Enter.
    The following packages will be installed on your system as a part of the mongodb-org package:
    • mongodb-org-server - The mongod daemon, and corresponding init scripts and configurations.
    • mongodb-org-mongos - The mongos daemon.
    • mongodb-org-shell - The mongo shell, an interactive JavaScript interface to MongoDB, used to perform administrative tasks thought the command line.
    • mongodb-org-tools - Contains several MongoDB tools for to importing and exporting data, statistics, as well as other utilities.
  3. Starting MongoDB
    Once the installation is completed, start the MongoDB daemon and enable it to start on boot by typing:
    sudo systemctl start mongodsudo systemctl enable mongod
    Copy
  4. Verifying MongoDB Installation
    To verify the installation we will connect to the MongoDB database server using the mongo tool and print the server version:
    mongod
    Copy
    Once you are inside the MongoDB shell type the following command which will display the MongoDB version:
    db.version()
    Copy
    The output will look like the following:
    4.0.1
    Copy
You can configure your MongoDB instance by editing the /etc/mongod.conf configuration file which is written in YAML.
The default configuration settings are sufficient in most cases. However, for production environments we recommend uncommenting the security section and enabling authorization as shown below:
/etc/mongod.conf
security:
  authorization: enabled
Copy
The authorization option enables Role-Based Access Control (RBAC) that regulates users access to database resources and operations. If this option is disabled each user will have access to any database and will be able to execute any action.
After making changes to the MongoDB configuration file, restart the mongod service:
sudo systemctl restart mongod
Copy
To find more information about the configuration options available in MongoDB 4.0 visit the Configuration File Options documentation page
Creating Administrative MongoDB user
If you enabled the MongoDB authentication, create one administrative MongoDB user that you will use to access and manage your MongoDB instance.
First access the mongo shell with:
mongo
Copy
Once you are inside the MongoDB shell type the following command to connect to the admin database:
use admin
Copy
switched to db admin
Copy
Create a new user named mongoAdmin with the userAdminAnyDatabase role:
db.createUser(
  {
    user: "mongoAdmin", 
    pwd: "changeMe", 
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)
Copy
Successfully added user: {
 "user" : "mongoAdmin",
 "roles" : [
  {
   "role" : "userAdminAnyDatabase",
   "db" : "admin"
  }
 ]
}
Copy
You can name the administrative MongoDB user as you want.
Exit the mongo shell with:
Quit()
To test the changes, access the mongo shell using the administrative user you have previously created:
mongo -u mongoAdmin -p --authenticationDatabase admin
Copy
use admin
Copy
switched to db admin
Copy
Now, print the users with:
show users
Copy
{
 "_id" : "admin.mongoAdmin",
 "user" : "mongoAdmin",
 "db" : "admin",
 "roles" : [
  {
   "role" : "userAdminAnyDatabase",
   "db" : "admin"
  }
 ],
 "mechanisms" : [
  "SCRAM-SHA-1",
  "SCRAM-SHA-256"
 ]
}
Copy
You can also try to access the mongo shell without any arguments ( just type mongo) and see if you can list the users using the same commands as above.

1 comment:

The complete list of new features in MySQL 8.0

There are over 250 new features in MySQL 8.0. The MySQL Manual is very good, but verbose. This is a list of new features in short bullet f...