CentOS Install MongoDB

3 minute read

# Install MongoDB Community Edition

Modify from MongoDB Help doc Install MongoDB Community Edition on Red Hat Enterprise or CentOS Linux

STEP 1: Configure the package management system(yum).

	vim /etc/yum.repos.d/mongodb-org-3.2.repo 
	Use the following repository file:
	[mongodb-org-3.2]
	name=MongoDB Repository
	baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/
	gpgcheck=1
	enabled=1
	gpgkey=https://www.mongodb.org/static/pgp/server-3.2.asc

STEP 2: Install the MongoDB packages and associated tools.

       sudo yum install -y mongodb-org
   

STEP 3: Run MongoDB Community editions

### Configure SELinux Prerequisites

        semanage port -a -t mongod_port_t -p tcp 27017
  • Set SELinux to permissive mode in /etc/selinux/config by setting the
        vim /etc/selinux/config 
        SELINUX=permissive
    
  • You must reboot the system for the changes to take effect.
        shutdown -r now
    

Data Directories and Permissions

WARNING
>On RHEL 7.0, if you change the data path, the default SELinux policies will prevent mongod from having write access on the new data path if you do not change the security context.

The MongoDB instance stores its data files in /var/lib/mongo and its log files in /var/log/mongodb by default, and runs using the mongod user account. You can specify alternate log and data file directories in /etc/mongod.conf. See systemLog.path and storage.dbPath for additional information.

If you change the user that runs the MongoDB process, you must modify the access control rights to the /var/lib/mongo and /var/log/mongodb directories to give this user access to these directories.

STEP 4: Procedure

-1 Start MongoDB. You can start the mongod process by issuing the following command:

    sudo service mongod start

-2 Verify that MongoDB has started successfully You can verify that the mongod process has started successfully by checking the contents of the log file at /var/log/mongodb/mongod.log for a line reading

[initandlisten] waiting for connections on port where is the port configured in /etc/mongod.conf, 27017 by default.

You can optionally ensure that MongoDB will start following a system reboot by issuing the following command:

    sudo chkconfig mongod on

-3 Stop MongoDB.
As needed, you can stop the mongod process by issuing the following command:

    sudo service mongod stop

-4 Restart MongoDB. You can restart the mongod process by issuing the following command:

    sudo service mongod restart

You can follow the state of the process for errors or important messages by watching the output in the /var/log/mongodb/mongod.log file.

-5 Begin using MongoDB. To help you start using MongoDB, MongoDB provides Getting Started Guides in various driver editions. See Getting Started for the available editions.

Before deploying MongoDB in a production environment, consider the Production Notes document.

Later, to stop MongoDB, press Control+C in the terminal where the mongod instance is running.

Troubleshooting

  1. errno:13 Permission denied Is a mongod instance already running service mongod stop ls -la to see what group & owner mongodb assigned to /var/lib/mongodb and I changed the /data/mongo with chown and chgrp to match. chown -R mongod:mongod /data/db Then I ran service mongodb start and waited about a minute. If you try to connect to 27017 immediately you won’t be able to
  2. Address already in use for socket:0.0.0.0:27017
        ps wuax | grep mongo
    
You should see somethin that looks like this
User           31936   0.5 0.4 2719784 35624   ?? S     7:34pm   0:09.98 mongod
User           31945   0.0 0.0 2423368   184 s000 R+   8:24pm   0:00.00 grep mongo

Now enter the kill command for the mongod instance(31936 in this case):
        kill -9 31936
        killall mongod
    
  1. How to avoid transparent_hugepage/defrag warning from mongodb
    >2015-03-06T21:01:15.526-0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
    2015-03-06T21:01:15.526-0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
    
STEP1. Open /etc/init.d/mongod file
STEP2. Add script to the start fuction
Here are the lines to add to /etc/init.d/mongod:
    if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
        echo never > /sys/kernel/mm/transparent_hugepage/enabled
    fi
    if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
       echo never > /sys/kernel/mm/transparent_hugepage/defrag
    fi
    
STEP3. restart mongod
        # service mongod restart

        Restarting mongod (via systemctl):  Warning: mongod.service changed on disk. Run 'systemctl daemon-reload' to reload units.
                                                                    [  OK  ]
        [root@localhost work]# systemctl daemon-reload

        [root@localhost work]# service mongod restart

        Restarting mongod (via systemctl):                          [  OK  ]
    
  1. WARNING: soft rlimits too low. rlimits set to 4096 processes, 64000 files. Number of processes should be at least 32000 : 0.5 times number of files.

    STEP1. Open vim /etc/security/limits.conf STEP2. Add script to the start fuction Here are the lines to add to /etc/security/limits.conf

    mongod  soft  nofile  64000
    mongod  hard  nofile  64000
    mongod  soft  nproc  32000
    mongod  hard  nproc  32000
    
STEP3. restart mongod 
            # service mongod restart

        
  1. Other client connection mongo Server is CentOS7 STEP1. firewall to and port
        firewall-cmd --zone=public --add-port=2888/tcp --permanent
    

STEP2. Then remember to reload the firewall for changes to take effect.

        firewaall-cmd --reload
    

STEP3. Check file /etc/mongo.conf

        net:
            port:27017
            bindIp:0.0.0.0
    

If don’t like this config ,Change it and restart mongod

Leave a Comment