Mongodb – installing mongodb in Linux

Time:2021-6-11

Install mongodb

Configuration environment

  1. Download installation package
    https://www.mongodb.com/downl…
  2. Unzip the installation package
    tar -zxvf mongodb-linux-x86_64-amazon-3.6.4.tgz
  3. Modify folder name
    mv mongodb-linux-x86_64-amazon-3.6.4 mongodb
  4. Add environment variable

    vi /etc/profile  
    export MONGODB_ Home = / usr / local / mongodb # mongodb installation address  
    export PATH=$PATH:$MONGODB_HOME/bin

    After saving, compile the system configuration
    source /etc/profile

configuration file

Create mongodb.conf configuration file in bin directory

cd /usr/local/mongodb/bin  
    vi mongodb.conf  
      
    Dbpath = / usr / local / mongodb / data / DB # data file storage directory  
    Logpath = / usr / local / mongodb / logs / mongodb. Log # log file storage directory  
    Port = 27017? Port  
    Fork = true # enabled as a daemon, that is, running in the background  
    nohttpinterface = true

Data folder

Create a data folder to save the log and DB information. This file is dbpath and logpath in mongodb.conf.
    mkdir -p /data/logs
    mkdir -p /data/db

Start mongodb

Start mongodb

./mongod -f mongodb.conf

Enter mongodb database

./mongo

Introduction to mongodb command

Default startup mode

./mongod
The default startup mode is / data / DB in the data storage directory, and the listening port is 27017

Starting mode with basic parameters

./mongod –port 51017 –dbpath /home/mongo/data –logpath /home/mongo/log/log.log –logappend –fork –journal 1000

Basic parameters:
   -F specifies the configuration file (Reference: http://www.mongodb.org/display/DOCS/File+Based+Configuration )
   --Port specifies the port. The default is 27017
   --Dbpath data directory path
   --Logpath log file path
   --Logappend log append instead of rewrite
   --Fork runs as a child process
   --Journal log submission interval, default 100ms
   --Nojournal turns off the logging function. Version 2.0 or above is turned on by default

Mongodb off

use admin
db.shutdownServer()
DB. Shutdown server ({force: true}) forces mongod to shut down. When the time difference between master and slave in the replica set exceeds 10s, it is not allowed to shut down the master database
Do not use kill to kill Mongo process directly to close data node, which will cause data damage

Mongodb connection error

Mongodb cannot be started: child process failed, exited with error number 48
Reason 1: closing mongodb abnormally
method:

1. Find the mongod.lock file and delete mongod.lock
2. Start mongodb in repair mode
    /root/work/mongodb/mongodb/bin/mongod --repair --dbpath=/root/work/mongodb/mongodb/data
3. Start mongodb
    /usr/bin/mongod -f /etc/mongod.conf
4. Check whether the process is running
   ps aux|grep mongo

Reason 2: the port is occupied
It may be because mongodb has been started. Check whether mongodb has been installed in the system, or change the port.