MongoDB is a free and open source document database. It belongs to a family of databases called NoSQL. NoSQL is different from traditional relational databases such as MySQL and PostgreSQL.

In MongoDB, data is flexibly stored as a document in jSON-like form. It does not require a predefined schema, and data structures can be changed all the time.

This guide explains how to install and configure the MongoDB community version on a CentOS 8 server.

Install MongoDB

MongoDB is unavailable on the CentOS 8 core software source. We will enable the official MongoDB software source and install the package.

At the time of this writing, the latest MongoDB version on the official MongoDB software source is 4.4. Before starting the installation, visit the MongoDB documentation Install on Red Hat to check for a newer release.

Perform the following steps to install MongoDB on CentOS 8 as user root or user with sudo permission:

Mysql > create a mongodb source file named mongodb-org.repo in /etc/yum.repos. D /

touch /etc/yum.repos.d/mongodb-org.repo
Copy the code
[mongo - org - 4.4] name = directing a Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/ gpgcheck = 1 enabled = 1 gpgkey=https://www.mongodb.org/static/pgp/server-4.4.ascCopy the code

If you want to install an older version of MongoDB, replace 4.4 with the version you want.

02. Install the mongodb-org meta package:

sudo dnf install mongodb-org
Copy the code

During installation, you will be prompted to insert the MongoDB GPG key. Type y and press Enter.

The following packages will be installed on your system as part of the mongodb-org package:

Mongodb-org-server-mongod daemon, corresponding initialization script and configuration. Mongodb-org-mongos -mongos daemon process. Mongodb-org-shell-mongo shell, an interactive JavaScript interface for mongodb, mainly used to perform some administrative tasks through the command line. Mongodb-org-tools – Contains some mongodb tools, such as data import tool, data export tool, and data statistics tool. 03. Once installed, enable and start the MongoDB service:

sudo systemctl enable mongod --now
Copy the code

To verify the installation, connect to the MongoDB database and print out the server version:

mongo
Copy the code

Run the following command to display the MongoDB version number:

db.version()
Copy the code

The output might look like this:

holdingsCopy the code

2. Configure MongoDB

The MongoDB configuration file is named: mongod.conf and is located in the /etc directory. This file is in YAML format.

The default configuration applies to most user scenarios. However, for a production environment, we recommend opening the security section and enabling user authentication: /etc/mongod.conf

security:
  authorization: enabled
Copy the code

This authorization option enables role-based Access Control (RBAC), which dictates rules for user Access to data and operations. If this option is disabled, then every user can access any data and perform any action.

Restart the Mongod service after making any changes to the MongoDB configuration file:

sudo systemctl restart mongod
Copy the code

For more information about MongoDB configuration options, see the configuration file Options documentation page.

Create a MongoDB administrator

If you have enabled MongoDB user authentication, you need to create an administrator user that can access and manage MongoDB instances.

First, access MongoDB shell:

mongo
Copy the code

Enter the following command to connect to the admin database:

use admin
Copy the code
switched to db admin
Copy the code

MongoAdmin Create a new user named mongoAdmin and assign userAdminAnyDatabase the role:

db.createUser(
  {
    user: "mongoAdmin", 
    pwd: "changeMe", 
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)
Copy the code
Successfully added user: {
	"user" : "mongoAdmin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	]
}
Copy the code

Exit the Mongo shell:

quit()
Copy the code

To test the changes, use the administrator account you created earlier to access Mongo shell:

Mongo -u mongoAdmin -p --authenticationDatabase admin MongoDB shell version v4.2.3 Enter password:Copy the code
use admin
Copy the code
switched to db admin
Copy the code

Now, print user:

show users
Copy the code
{
	"_id" : "admin.mongoAdmin",
	"user" : "mongoAdmin",
	"db" : "admin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	],
	"mechanisms" : [
		"SCRAM-SHA-1",
		"SCRAM-SHA-256"
	]
}
Copy the code

Mongodb setup background running

We will create a data directory and logs directory under mongodb to hold data and logs. Once set up, we usually go through

mongod --dbpath /usr/local/mongodb/data
Copy the code

Let mongodb start. But when we close the shell, mongodb stops running. If you want to run in the background, just add –fork at startup. You can add — logAppend after the log path to prevent the log from being deleted. The code is as follows:

mongod --fork --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs/mongodb2.log --logappend
Copy the code

So you close the shell, open the shell, and allow mongo as usual! Mongodb setup startup after startup. When we shut down the server and restart it, mongodb stopped again. Is there a way to start it immediately after startup? Put the above code in /etc/rc.local and you’re done. Specific operation steps:

vim /etc/rc.local
Copy the code

Add the above startup code:

/usr/local/mongodb/bin/mongod --fork --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs/mongodb2.log --logappend
Copy the code

Next time you reboot, you can run mongodb directly!

Local mongoDB Compass connects to remote databases

  • 1.Hostname is localhost by default. Error creating SSH Tunnel: (SSH) Channel open failure: Connection refused will be displayed when you enter the domain name or IP number.(Make sure the database is started on the server before clicking CONNECT.)
  • 2. The default Port value is 27017.
  • 3. Generally, SSH is used for connection. Enter the same connection information as shell.
  • 4. Go to /etc mongod. Conf and comment out “bindIp: 127.0.0.1” by adding a “#” in front of it.
  • 5. Add “AllowTcpForwarding yes “to the sshd_config file in /etc/ssh on the server to allow TCP connections. Then run sudo systemctl start sshd.service to restart SSH.