Super detailed CentOS installation Mongodb helps you avoid detours (must see)

CentOS7.2 64 bit installation of mongodb4 4.1 (detailed)

Here, I use yum to install automatically. I strongly don't recommend Xiaobai to decompress the compressed package for automatic installation. There are too many pits!

Introduction to environment variable path

  1. mongodb bin directory: / etc
  2. dbpath of mongodb: / var/lib/mongo
  3. logpath of mongodb: / var / log / mongodb / mongod log
  4. config path of mongodb: / etc

Remind you to replace the command with the corresponding directory of your mongodb!

yum install

Connect to the server through the connection tool. I recommend terminal or terminus on the mac side and XShell on the window side. Of course, you can find the version that doesn't charge!

1, Configure the yum source for mongodb

This is equivalent to configuring the way for yum to obtain the mongodb installation package, and setting the version information of the obtained installation package at the same time!

1. New mongodb-org-4.4 Repo file

vim /etc/yum.repos.d/mongodb-org-4.4.repo

2. Edit mongodb-org-4.4 Repo file

Press i to enter inster mode (prompt in the lower left corner) and enter

name=MongoDB Repository

Press the esc key in the upper left corner of the keyboard to exit the editing mode, then press: wq (prompt in the lower left corner) in English, and then press enter to save and exit.

2, Install using yum or up2date

Start to install some tools related to mongodb. If there is no problem, there will be a complete one at last, because the word indicates that the installation is successful!

sudo yum install -y mongodb-org-4.4.1 mongodb-org-server-4.4.1 mongodb-org-shell-4.4.1 mongodb-org-mongos-4.4.1 mongodb-org-tools-4.4.1

3, Encounter problems

If you don't make any mistakes in the above steps, you can skip this part. I just stepped on a lot of holes!

1. Run the yum install command and the error is as follows: File contains no section headersXXX

The best way to solve this problem is to check the yum source file of mongodb configuration. If yes, delete yum repos. D module and download and install again!


rm -f /etc/yum.repos.d/

Download the image again

wget -O /etc/yum.repos.d/CentOS-Base.repo

Clean cache

yum clean all

After doing so, execute sudo yum install -y mongodb-org-4.4.1 mongodb-org-server-4.4.1 mongodb-org-shell-4.4.1 mongodb-org-mongos-4.4.1 mongodb-org-tools-4.4.1 again to install mongodb through yum!

Startup, shutdown and restart of mongodb

1. Start

sudo systemctl start mongod

/usr/bin/mongod --config /etc/mongod.conf / / startup method after user creation

The unsuccessful startup is largely due to mongodb's lack of dbpath and logpath permissions

sudo chown -R mongod:mongod /var/lib/mongo
sudo chown -R mongod:mongod /var/log/mongodb

2. Close

sudo systemctl stop mongod

mongod --shutdown --dbpath /var/lib/mongo / / set the shutdown mode after the user

3. Restart

sudo systemctl restart mongod

4. Startup and self startup

sudo systemctl enable mongod

5. Check the startup status

sudo systemctl status mongod

6. Repair mongodb

Using kill-9 to forcibly shut down, mongodb cannot be started to a large extent. You can use this command to repair mongodb. It's really difficult!

/usr/bin/mongod -f /etc/mongod.conf --repair

Simple operation of mongodb

Operating database in command line mode

mongo //Enter database
show dbs //Show all databases
use admin //Using the action admin Library
show collections //Show all tables in the library
db.createCollection("theme") //Create a theme table in the library
db.theme.insert({"id":"184820911","navbar": "red","num": 9,"fashion":["blue","gold","pink"]}) //Input a record in the table //Delete news table
db.dropDatabase() //Delete database
db.users.update({"id": "xxx"},{$set:{"name": "huasen"}}) //Update data in users table{"id":3}) //Find in the school table

db.createUser({user:"user name",pwd:"password",roles:[{role:"root",db:"admin"}]}) // Create user

use test  // Jump to the database where you want to add users
  user: 'fooadmin',  // user name
  pwd: '123456',  // password
    role: 'readWrite',  // role
    db: 'test'  // Database name

show users  // View users under the current library
db.dropUser('testadmin')  // delete user
db.updateUser('admin', {pwd: '654321'})  // Modify user password
db.auth('admin', '654321')  // Password authentication

sudo chown -R mongod:mongod /var/lib/mongo
sudo chown -R mongod:mongod /var/log/mongodb

/usr/bin/mongod --config /etc/mongod.conf //Startup method after user creation
mongod  --shutdown  --dbpath /var/lib/mongo //Set the user's closing method

Export import data

mongoexport -d admin -c theme -o /data/backup/theme.json //Back up the data in the theme table in the admin library to theme JSON file
mongoimport -d admin -c themecopy --file /data/backup/theme.json //From theme The imported data in the JSON file enters the themecopy table in the admin library

Backup recovery

mongodump -d admin -o /data/backup/mongodb  //Backup data
mongorestore -d admincopy --dir=/data/backup/admin //Recover data

Troubleshooting of browser connection failure

This problem has tormented me for a long time. It involves a wide range of problems and needs patient investigation!

Successful examples

Access through the browser: ip:27017. The default port of mongodb is 27017.

Troubleshooting process

Through the following steps for troubleshooting, I hope I can give you more help!

1. Check whether there are mongo related programs running on port 27017 of linux

View all current Ports - ntlp, view all current ports!

2. Check whether the corresponding security group port of the server is enabled

The specific operation depends on your Alibaba cloud or Tencent cloud operation

Both outbound and inbound rules should open the port 27017. It depends on how your server is set up!

Let's refer to my own settings

3. Check whether the server firewall is open

Check whether the server has restrictions on the ports of the firewall. I'm simple and rude. Close the firewall directly!

systemctl disable firewalld.service
systemctl stop firewalld.service
setenforce 0

Or directly check the opening of the port

firewall-cmd --query-port=27017/tcp // Check the firewall settings of port 27017 under tcp protocol
firewall-cmd --list-ports // View all ports
systemctl stop firewalld // Turn off the firewall
firewall-cmd --add-port=27017/tcp // Turn off firewall restrictions on 27017

The problem of firewall is not very good. Different versions of CentOS use different firewall applications. It depends on your play. Welcome to teach me!

4. Check the bind of mongodb_ IP attribute

bind_ The default IP attribute is By default, it only supports application access in the server, so we can't connect by entering the connection port number in the browser. I directly modified it to make all applications can access the connection! Is to bind_ If the IP attribute is set to, the problem arises. How can mongodb be found through mongodb installed by yum Config file. Generally speaking, the default path of packages installed using yum rpm is as follows:

routeDocument storage description
/etcDirectory of some installation package configuration files
/usr/binExecutable files of some installation packages
/usr/libSome programs use dynamic function libraries
/usr/share/docSome basic software manuals and help documents

Sure enough, I found the file!

Start to modify and save, and you will be able to access it successfully! If not, I can't help you. I suggest reopening!


I'm just a rookie in and out of the society. I made a detailed record when installing mongodb. Because I have little talent and learning, I hope you guys don't spray it. While helping you, I beg you guys to indicate the source if you reprint it!


I have developed a resource website navigation website. Many resources are shared. The vue project will be slower for the first time, but it will be soon after. It covers all aspects of life. If I look carefully, I will definitely gain. There is no advertising and software sharing. I hope you can support me!, if there is no accident, I will open this source to you! Your visit may be an encouragement to me. Thank you!

Keywords: Linux CentOS MongoDB

Added by SemiApocalyptic on Thu, 10 Feb 2022 02:06:12 +0200