mongoDB Cpanel Installation

M
Created by MK
 22 Nov, 2016

We had to install mongoDB 3.2.10 in our CPanel server recently for one of our projects which uses mongo as the backend DB instead of maria DB. We choose mongo because of speed and scalability factor, the DB will be populated with nearly 500mb-1gb depending on the transactions/activity per day. So we where looking at something 2-5TB of data within 4-6 months of constant activity.

Though mySQL and Maria DB can be used, we wanted to move into the no-sql territory since future data collections are moving into nosql. Big data is going to be a interesting field in future, and no-sql being one of the primary sources for this, we want to experience ourselves on this, not to say that this particular project also made more sense for us to move to mongo DB. 

The major difference between mysql/maria and no-sql is the RDBMS part, where the relation between data is minimal in mongoDB while you can create complex relations in mysql. All relations needs to be handled in the code as such. Below is a simple table between SQL and MongoDB

Terminology and Concepts

The following table presents the various SQL terminology and concepts and the corresponding MongoDB terminology and concepts.

 SQL Terms/Concepts MongoDB Terms/Concepts
 database database


 table collection


 row document or BSON document


 column field


 index index


 table joins embedded documents and linking


 primary key Specify any unique column or column combination as  primary key. primary key In MongoDB, the primary key is automatically   set to the _idfield


 aggregation (e.g. group by) aggregation pipeline



LINK - https://docs.mongodb.com/v3.2/reference/sql-comparison/

CPanel Installation of MongoDB

You need SSH access to your cpanel server to install MongoDB. Login into your VPS or Dedicated server via SSH.

MacBook-Pro:~ mk$ ssh root@xxx.xxx.xxx.xxx -p 22

Once logged in, navigate to repos folder.

MacBook-Pro:~ mk$ cd /etc/yum.repos.d

You need to create the RPM repos file to add the official mongoDB packages path. I use nano which is easy to use, but use any text editor which is available like pico or vi as well.. 

MacBook-Pro:~ mk$ nano -w mongodb.repo

Paste the below code

[mongodb]
name=MongoDB Repo
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/
gpgcheck=0
enabled=1


Save this file by CTRL+X

Install the MongoDB Server

Run the below command to install the server and its dependencies 


MacBook-Pro:~ mk$ yum install mongo-10gen mongo-10gen-server

Add mongoDB to be started whenever you restart or reboot the Cpanel server which might happen occasionally. 

MacBook-Pro:~ chkconfig mongod on

We have successfully installed mongoDB now, great. Now you can start the server with the following command

MacBook-Pro:~ service mongod start

Now you can check if mongoDB is running with the following command

MacBook-Pro:~ mongo

If you get the below error


"Failed global initialization: BadValue Invalid or no user locale set. Please ensure LANG and/or LC_* environment variables are set correctly."

Run

MacBook-Pro:~ export LC_ALL=C

and then 

MacBook-Pro:~ mongo

By default, mongo does not ask for a username and password since its not set, In part 2 of this blog at a later date I will write on how to setup authentication as well as RockMongo PHP client interface similar to phpmyadmin for easy operations on the DB.

Once you confirm mongo DB is running successfully, exit it, since we need to install the PECL driver extension for PHP to connect to mongoDB.

Next to install PHP extension to connect PHP with mongoDB via your code for future use, 

MacBook-Pro:~ pecl install mongo

Then restart Apache via WHM.

That's it you are all set to start using mongoDB with PHP.

Adding Authentication for MongoDB

The above installation we just did does not have authentication enabled, so some scripts might not work since password is needed to connect to mongodb. So follow the below steps to add authentication/admin

user to connect to the mongoDB server.

Goto to the mongo command line, and add a admin user

MacBook-Pro:~ mongo

MongoDB shell version: 3.2.11

connecting to: test

> use admin;

> db.addUser('admin','123456');

Once the admin user is added, stop the server, since we need to restart with --auth 

> db.shutdownServer();

> exit

Now restart the mongoDB with --auth in the command line, $ 

MacBook-Pro:~ sudo ./mongodb/bin/mongod --auth --dbpath /mnt/db/

You can run mongo again in 2 ways:

        i) run mongo first then login:

MacBook-Pro:~ ./mongodb/bin/mongo localhost:27017 > use admin > db.auth('admin','123456');

       ii) run & login to mongo in command line.

MacBook-Pro:~ ./mongodb/bin/mongo localhost:27017/admin -u admin-p 123456