How to Install MongoDB on CentOS

1. Create a Virtual Machine in your Laptop with CentOS 6.x
1.1 Create a mongodb directory under /opt to install mongodb

[root@localhost opt]# ls -ltr
total 4
drwxr-xr-x. 2 root root 4096 May 15 22:19 mongodb
[root@localhost opt]#

[root@localhost opt]# cat /etc/redhat-release
CentOS Linux release 6.0 (Final)
[root@localhost opt]#

1.2 Download MongoDB
[root@localhost opt]# curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.4.4.tgz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
75 82.4M 75 62.3M 0 0 2909k 0 0:00:29 0:00:21 0:00:08 3405k

Note: Check my Link how to bridge your laptop internet to the VM created on Your Laptop
2. Extract the files from the downloaded archive.
tar -zxvf mongodb-linux-x86_64-3.4.4.tgz
3. Copy the extracted archive to the target directory.
cp -R -n mongodb-linux-x86_64-3.4.4/ /opt/mongodb
4. Confirm the Binaries are extracted

[root@localhost mongodb-linux-x86_64-3.4.4]# ls -ltr
total 120
-rw-r–r–. 1 root root 55625 May 15 22:22 THIRD-PARTY-NOTICES
-rw-r–r–. 1 root root 1359 May 15 22:22 README
-rw-r–r–. 1 root root 16726 May 15 22:22 MPL-2
-rw-r–r–. 1 root root 34520 May 15 22:22 GNU-AGPL-3.0
drwxr-xr-x. 2 root root 4096 May 15 22:22 bin
[root@localhost mongodb-linux-x86_64-3.4.4]# pwd
/opt/mongodb/mongodb-linux-x86_64-3.4.4
[root@localhost mongodb-linux-x86_64-3.4.4]#

5. Ensure the location of the binaries is in the PATH variable.

The MongoDB binaries are in the bin/ directory of the archive. To ensure that the binaries are in your PATH, you can modify your PATH.
export PATH=<mongodb-install-directory>/bin:$PATH
export PATH=/opt/mongodb/mongodb-linux-x86_64-3.4.4/bin:$PATH
[root@localhost ~]# cat .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
export PATH=/opt/mongodb/mongodb-linux-x86_64-3.4.4/bin:$PATH
[root@localhost ~]#
6. Logout and Login again or reload the profile to get PATH variable updated

[root@localhost ~]# echo $PATH
/opt/mongodb/mongodb-linux-x86_64-3.4.4/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost ~]#
7. Run MongoDB Community Edition

7.1 Create the data directory.
[root@localhost opt]# mkdir db
[root@localhost opt]# cd db
[root@localhost db]# pwd
/opt/db
7.2 Set permissions for the data directory.

7.3 Run MongoDB

[root@localhost ~]# env |grep PATH
PATH=/mongodb/mongodb-linux-x86_64-3.4.4/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost ~]#

mongod –dbpath <path to data directory>
mongod –dbpath /opt/db
[root@localhost db]# mongod –dbpath /opt/db
2017-05-15T22:27:04.770+0200 I CONTROL [initandlisten] MongoDB starting : pid=1267 port=27017 dbpath=/opt/db 64-bit host=localh ost.localdomain
2017-05-15T22:27:04.770+0200 I CONTROL [initandlisten] db version v3.4.4
2017-05-15T22:27:04.770+0200 I CONTROL [initandlisten] git version: 888390515874a9debd1b6c5d36559ca86b44babd
2017-05-15T22:27:04.770+0200 I CONTROL [initandlisten] allocator: tcmalloc
2017-05-15T22:27:04.770+0200 I CONTROL [initandlisten] modules: none
2017-05-15T22:27:04.770+0200 I CONTROL [initandlisten] build environment:
2017-05-15T22:27:04.770+0200 I CONTROL [initandlisten] distarch: x86_64
2017-05-15T22:27:04.770+0200 I CONTROL [initandlisten] target_arch: x86_64
2017-05-15T22:27:04.770+0200 I CONTROL [initandlisten] options: { storage: { dbPath: “/opt/db” } }
2017-05-15T22:27:04.844+0200 I STORAGE [initandlisten]
2017-05-15T22:27:04.844+0200 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the Wi redTiger storage engine

 

Yes your MongoDB is up and Running now!!!!

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.