Limited support This version of the product is in limited support. However, the documentation is available for your convenience. You will not be able to leave comments. Click here to view the documentation for the current version.

Installing and setting up MongoDB on Linux


Use the following procedure to set up the MongoDB on a Linux server. This procedure applies to both stand-alone and high-availability setups. 

Note

 For high-availability, you must perform an additional step of configuring MongoDB for a replica set.

  1. Switch to root access by running the following command:

    sudo su
  2. Download the Mongo database as follows: 

    curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.2.1.tgz

                

              

  3. Extract the MongoDB archive by running the following command: 

    tar -zxvf mongodb-linux-x86_64-3.2.1.tgz
  4. Copy MongoDB to the target directory by running the following commands:

    mkdir -p /dbdata/mongodb
    cp -R -n mongodb-linux-x86_64-3.2.1/ /dbdata/mongodb
  5. Create the database data directory by running the following command:

    mkdir -p /dbdata/mongodatawt
  6. Create a key file that your deployment will use to authenticate servers to each other. Ensure that your key file meets the following requirements:

    • Contains 6 to 1,024 characters and contains only characters in the base64 set
    • Does not have group or world permissions on UNIX system

      To set read and write permissions for the file owner
      chmod 600 <KeyFile>

      • Setting the key file enables authentication, and specifies a key file for the replica set members to use when authenticating to each other
      • The key file content is arbitrary, but is the same on all members of the replica set instances that are connected to the set.

    Use either of the following example Linux commands:

    openssl rand -base64 741 > mongo.key
    chmod 600 mongo.key

    or

    tr -cd '[:alnum:]' < /dev/urandom | fold -w50 | head -n1 > "mongo.key"
  7. (Optional) Set the storage engine to wiredTiger for better performance and reduced storage size by running the following command:

    /dbdata/mongodb/mongodb-linux-x86_64-3.0.6/bin/mongod --dbpath /dbdata/mongodatawt --port 27017 --storageEngine wiredTiger
  8. Connect to the mongo shell, and create the administrator user and the social database administrator as follows:

    use admin
    db.createUser( {
    user: "siteUserAdmin",
    pwd: "<siteUserPassword>",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
    });
    db.createUser( {
    user: "siteRootAdmin",
    pwd: "<siteRootAdminPassword>",
    roles: [ { role: "root", db: "admin" } ]
    });
    use social
    db.createUser( {
    user: "social_admin",
    pwd: "<social_adminPassword>",
    roles:
    [ { role: "dbOwner", db: "social" } ]
    });
  9. Stop the Mongo process.
  10. Add MongoDB as a service with the key file parameter.

    On the Red Hat Enterprise Linux (including CENT OS), create the /etc/mongod.conf file and add the following settings:

    dbpath = /dbdata/mongodatawt 
    port = 27017 
    logpath = /dbdata/mongodb/mongo_social.log
    logappend = true 
    quiet = true 
    journal = true 
    replSet = myitsocial 
    fork = true 
    keyFile = /dbdata/mongodb/mongo.key
    nohttpinterface = true
    storageEngine = wiredTiger

    Important considerations

    In the mongo.cfg file, comment out the following parameters:


    For MongoDB version earlier than 2.6.x:

    #setParameter = textSearchEnabled=true

    For MongoDB version earlier than 3.0.x:

    #storageEngine = wiredTiger

    For a standalone setup:

    #replSet=

    #keyFile= 

  11. Create the /etc/init.d/mongorepl file with the appropriate permissions and the following content:

    #!/bin/sh 

    # /etc/init.d/mongorepl 
    # Subsystem file for "MyIT-MongoRepl" server 

    # chkconfig: 2345 95 05 (1) 
    # description: MyIT-MongoRepl server daemon 
    #MONGOBIN -> point to mongod binary. 
     
    MONGOBIN="/dbdata/mongodb/mongodb-linux-x86_64-3.2.1/bin/mongod" 
     
    do_start() 
    { 
      echo "starting!"
      eval "$MONGOBIN -f /etc/mongod.conf"
    } 
     
    do_stop() 
    { 
      echo "stopping!" 
      eval "$MONGOBIN -f /etc/mongod.conf --shutdown"
    } 
     
    case "$1" in 
       start) 
          do_start 
          ;; 
       stop) 
          do_stop 
          ;; 
    esac 
     
    exit 0
  12. Use the Redhat chkconfig Utility to start the service automatically:

    • To add mongorepl to chkconfig, and enable in runlevel, run: 

      chkconfig --add mongorepl
      chkconfig mongorepl on
    • To start or stop the mongorepl service:

      service mongorepl start
      service mongorepl stop
  13. (For highavailability) After installing and setting up MongoDB on Linux, go to Configuring-MongoDB-for-replica-set.

 

Tip: For faster searching, add an asterisk to the end of your partial query. Example: cert*