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:

systemLog:
   destination: file
   path: "/root/software/mongodb/log/mongodb.log"
   logAppend: true
storage:
   dbPath: "/root/software/mongodb/data/db"
   journal:
      enabled: true
processManagement:
   fork: true
net:
   bindIp: localhost,192.168.146.222
   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:

./bin/mongo

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

show dbs
Insert picture description here

Common commands

commandDescription
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