Wednesday, December 14, 2011

MongoDB : Remote Access - Part 2

As stated in previous post on MongoDB, once installed, user can verify by opening shell & by typing mongo. It will by defualt connect with the table test (which is not a one you created obviously) like below if everything worked properly during installation.

MongoDB shell version: 1.4.3
Wed Nov 23 14:31:29 ***
connecting to: test

Before moving on to remote access, better to know few very basic commands to check what you really wants to know. Type show dbs which will show what are the existing databases. Then if you want to use existing one or new one type use dbname which will not create a database at the exact moment but on the fly if it does not exist. In mongodb table is considered a collection. Actually here, this is not exact same as the table in RDBMS but for clarity consider it is so. For to view all the collections in used database type show collections & to use one, type use collectionname. To view all the data inside of selected collection, type db.collectionname.find(). Here in mongodb, once you selected a database, when querying with collection you always has to use the reference of the database with query. That is why you have to use db.$what_ever_the_bla_bla next. You can find sql to mongodb mapping relationship page here. 

Now let's move to setting up a remote connection to a mongodb server.  It is better to have two terminals. One to start server & listening on incoming requests. Second to locally execute & view whether remote calls have worked (optional).

To start server

1. Create a directory structure in root
sudo mkdir -p  /data/db
2. Grant user permission to it
sudo chown `id -u` /data/db

3. Run mongo server to listen on incoming connections
You will noticed that sever is starting & saying it is listening on port as indicated in below image.
But If the result is like below

Then do the following.

4. Find mongodb PID & kill it.
ps -eF | grep 'mongo\|PID'

5. You can see in first shell image, I have executed this command & obtained the ID 1143. Next is to kill that process.
sudo kill “PID_VALUE”
Re-run the mongodb server: mongod
This is because if mongodb was installed using sudo apt get install, it will always run each time machine reboots. Therefore before server starts up, already running process has to be killed. Then server will start properly and keep on listening for incoming connections. To connect to a remote server simply type mongo remoteIPaddress after starting the mongodb server on both sides.To get mongodb execution status use sudo status mongodb

From now on there are plenty of enough resources available to continue with mongodb. Go after & enjoy the power of mongo.

Monday, December 12, 2011

MongoDB | Document Oriented, No SQL open source database - Part 1

OBJECTS. It is all about dealing with objects. That is, storing them, retrieving them back, updating them PLUS encoding, efficient indexing, replica managing etc.  Typical object can have its own as well as inherited feature sets. These can be considered as key value pairs like below.

    "username" : "bob",
    "address" : {
        "street" : "123 Main Street",
        "city" : "Springfield",
        "state" : "NY"
Above is an example of simple nested JSON object. When remote method invocation or inter process communication happens, by using this kind of mechanism to send data can be more convenient. When received, by querying, storing data back in SQL databases is not needed if there exits a secure, storage effective  document oriented querying language. That is where MongoDB fits with BSON format. It is a cross language database system yet to come with more features. BSON helps to store JSON objects as binary objects which reduce size & increase indexing & retrieving performance.

In this post, a simple working scenario for successfully installing mongoDB on Linux based operating system (basically on Ubuntu 10.10) will be discussed.


1. Add MongoDB repository into Ubuntu assuming installed ubuntu version is 10.10
Add below line any where of source.list file.
deb 10.10 10gen into /etc/apt/source.list.

2. Create PGP key and Install MongoDB
We need to generate key to gain access into MongoDB repository.
Use sudo apt-key adv --keyserver --recv 7F0CEB10 to get keyserver
Then do sudo apt-get update to update your repository.

3. Do sudo apt-get install mongodb-stable to install mongoDB into your Ubuntu.
If that didnt work, use just sudo apt-get install mongodb instead of -stable
Test by run command mongo and you will get MongoDB shell version

Now we have to configure mongoDB with PHP Driver so that you can interact with mongoDB programetically via server-side scripting language.

4.  Configure MongoDB PHP Driver
Before configure mongoDB PHP driver, first need to have build-essential, php5-dev and php-pear.
To install those:
sudo apt-get install build-essential php5-dev php-pear

**remember to tick on first two updates in software updates/updates in synaptic package manager. Otherwise system will not download all the packages needed & later steps will fail.

5. Then install pecl driver for mongo (For connecting with PHP)
sudo pecl install mongo

6. Add Mongo Extension into php.ini
In the end of line, add into /etc/php5/apache2/php.ini.

7. Restart apache by
sudo service apache2 restart

8. Check with phpinfo() to see if MongoDB already installed.

From next post, How to get remote accessibility by starting mongoDB server & checking it out via remote mongoDB terminal will be discussed. 

Good Luck :)