MongoDB installation steps and common commands

Article Directory

MongoDB installation steps

Unzip and rename

tar -zxvf mongodb-linux-x86_64-rhel70-4.0.24.tgz
mv mongodb-linux-x86_64-rhel70-4.0.24 mongodb

Create folder

Create the following folders in the mongodb directory to store the corresponding files

mkdir -p ./data/db
mkdir -p ./log

Create a configuration file

Create a mongo.conf file in the mongodb directory and fill in the corresponding configuration content.

vi ./mongo.conf

Fill in the content as follows:

   destination: file
   path: "/root/software/mongodb/log/mongodb.log"
   logAppend: true
   dbPath: "/root/software/mongodb/data/db"
      enabled: true
   fork: true
   bindIp: localhost,
   port: 27017

start up

Start the command under the mongodb root directory:

./bin/mongod -config ./mongo.conf

You can also enter a command to query the startup status and query whether the startup is successful:

ps -ef | grep mongo
Insert picture description here

After the startup is successful, enter the command to open the shell operation interface:


After entering the shell system, you can enter the following command to query:

show dbs
Insert picture description here

Common commands

use databaseNameCreate a custom database. If the databaseName database does not exist, create a new database. If it exists, connect to the database.
show dbsView database list
db.stats()Statistics database information, you can fill in the corresponding field name in db.stats(){inside: parameter}
db.dropDatabase()Delete database
db.getCollectionNames()View the list of collection names in the current database
show rolesView database user role permissions
db.tableName.insert()Create a collection (see instructions for inserting multiple items)
db.collectionName.find()Show collection content
db.collectionName.remove()Delete collection content
db.collectionName.find().count()View quantity