Basic administrative commands for the MongoDB tutorial

  • 2020-05-12 06:24:29
  • OfStack

1. Start and stop MongoDB:

Start the MongoDB server by executing the mongod command. mongod has many configurable options, and we can view all of them through mongod --help. Here are just a few of the main options:


    --dbpath :
    The default data directory for the database is /data/db . for Windows Platform if the current executable is located D Disk, then its default data directory is D:\data\db . We can use this option to respecify the data directory for the service. If the current host is running more than one mongod , then you must specify a different data directory for each service because when mongod After successful startup, it is created in the data directory 1 a mongod.lock The file is used to prevent others mongod Process data the data directory. Such as:
    mongod --dbpath=D:/data2/db
   
    --port
    The default port number by default is 27017 . Again, when you have multiple mongod Service procedures in the same 1 When the hosts are running at the same time, you need to specify different listening ports for each. Such as:
    mongod --port=29017
   
    --fork
    Run as a daemon MongoDB .
   
    --logpath
    Specify the log output path, not output to the command line. If you have write permission to the folder, the system will create the file if it doesn't exist. It overwrites the existing files and clears all the original log records. If you want to keep the original log, you need to use it again --logappend Options. Such as:
    /> mongod --logpath=D:\logdata\mylog.log
    all output going to: D:\logdata\mylog.log
    It should be noted that for the example above, logdata Directories must be created manually in advance, otherwise mongod Execution startup will fail.
   
    --config
    Specifies a configuration file to load various options not specified on the command line. Such as:
    mongod --config=D:\mydb.conf
    The sample configuration file looks like this:
    port = 29017
    fork = true   # The hashtag here represents the comment section, for fork This command line option is required true To show that it's open.
    logpath = D:\mylog\db.log

Usually, we want to gracefully close mongodb server. If the service program is running in the foreground, CTRL+C will do. If it is the background, it can notify the service program to exit through SIGINT and SIGTERM signals. After receiving the signals, the server will first properly arrange the data and state saving before the exit, such as normally closing the current connection and flushing the data in the cache to the disk. After all this work is done, the server stops normally. Such as:

    /> pkill mongod
    /> pkill -2 mongod
 

Don't just follow these commands:

    /> pkill -9 mongod
 

This signal will cause mongodb server to be forced to quit immediately.
In addition to the above methods, we can also notify the server of normal exit through mongo client tools, such as:

 > use admin
    switched to db admin
    > db.shutdownServer()
 

2. Server status monitoring:


    C:\Mine\ThirdParty\mongodb\bin>mongostat
    connected to: 127.0.0.1
    insert  query update delete getmore command flushes mapped  vsize    ... ...
     0      0      0      0       0       1       0     0m   100m    ... ...
     0      0      0      0       0       1       0     0m   100m    ... ...
     0      0      0      0       0       1       0     0m   100m    ... ...
     0      0      0      0       0       1       0     0m   100m    ... ...
     0      0      0      0       0       1       0     0m   100m    ... ...
     0      0      0      0       0       1       0     0m   100m    ... ...
     0      0      0      0       0       1       0     0m   100m    ... ...
     0      0      0      0       0       1       0     0m   100m    ... ...


Related articles: