Thursday, February 04, 2016

Wamp and MongoDB - I

MongoDB is catching up faster. Before we connect it to our WAMP, let me share some important facts about MongoDB.

1.  MongoDB is a Document DATABASE. These documents are equivalent to rows in a RDBMS table. These documents are in JSON format.

2. MongoDB supports scaling feature. The MongoDB database can spread over multiple servers It scales OUT to multiple machine whereas Relational model follow a Scale UP pattern so that data can be served from a single server.

3. MongoDB supports neither joins nor transactions. "Table JOINS" are handled by "embedded documents and linking"

4. MongoDB does not support JOINS - This is mostly true, There is a feature MongoDB 3.2 called $lookup, that does provide some limited join functionality.

5. MongoDB features secondary indexes, an expressive query language. MongoDB provides atomic operations on a single document.

6. Instead of Tables, MongoDB stores documents in Collections.

7. In MongoDB, the primary key is automatically set to the "_id" field.

8. it has Dynamic Schemas

9. MongoDB is NOT for systems that require SQL, joins, and multi-object transactions.

10. Two Storage Engines work, MMAPv1 and WiredTiger

11. While MongoDB writes to journal files promptly, MongoDB writes to the data files lazily. 

12. Written in c++, MongoDB follows AGILE methodologies

In this article, I am going to show how we can connect PHP (Wamp in Windows environment) and MongoDB. I am using Wamp Server version 2.5 with PHP 5.5 and Apache 2.4

A. We need to download MongoDB Community Edition from here :: 
      https://www.mongodb.org/downloads#production ( Windows MSI installation )
      https://www.mongodb.org/dl/win32/x86_64-2008plus-ssl ( ZIP archive )
   
      We need to install/unzip it in location like c:\mongodb. The location can be anything of our choice. 
   
B. Download MongoDB driver for PHP php_mongo.dll from                                                             http://pecl.php.net/package/mongo/1.5.5/windows 
      We need to be very careful while selecting the DLL version here. I would suggest a 32 bit download as 64 bit version did not work for me though my WorkStation was a 64 bit one. 
   
      Now, we need to put this  php_mongo.dll file in our Wamp installation folder as shown below : 
      C:\wamp\bin\php\php5.5.12\ext\
   
     "C:\wamp" is the location where Wamp is installed on my work station. And the above is the location where all the PHP extensions are kept. 

C.  Now, let's do a little home work for setting up our MongoDB.
      Create 3 more folders inside "mongodb" folder namely "conf", "data" and "logs". Also create a folder called "db" inside the "data" folder.
   
   Check the folder structure as shown below : 
   
                                     

   
D. Now create a configuration file called "mongodb.conf" or anything of your choice in the "conf" folder. This configuration will be loaded when the MongoDB starts running. We can have following content within that configuration file.

       # where to log
   logpath = C:\mongodb\logs\mlogger.log
   
   # New Logs will be appended
   logappend = true

   # only run on localhost for development
   bind_ip = 127.0.0.1                                                             
   # Server PORT
   port = 27017
   
   # Enable REST interface
   rest = true
   
   # data lives here
   dbpath = C:\mongodb\data
   
   The above configuration has comments (lines starting with a hash) to understand what it means.
   Log Path, DB path have been defined here. Port no. 27017 has also been provided here. So when we finish MongoDb setup, we would be able to run http://127.0.0.1:27017. The Log file name can be anything and it should exist.
   
E. Our next step will be installing MongoDB as an Windows Service. For this we have to run Command Terminal ... 

        mongod.exe --config Path-to-Config --install

Check the screenshot below



    So, now if we check the Windows Services by running "services.msc", we'll see that a service called "MongoDB" has been successfully installed. If it is not automatically running, select it and click on "Start Service" button as shown in the screenshot below.`




So, now MongoDB is installed and is available from Browser at http://127.0.0.1:27017. Check the screenshot below.





The port number specified in MongoDB configuration file could have been anything else as well. 

Now, with php_mongo.dll copied to the "ext" folder we just need to start Wamp. 

When Wamp icon turns green in the System Tray, click on it. You can see "php_mongo" being listed there. Check the screenshot below. 




As Wamp has started, our Web Server is available at http://127.0.0.1, hence if we run http://127.0.0.1?phpinfo=1 We can see that MongoDB library is successfully loaded. Check the screenshot below.





So far, we have started MongoDB Win32 service, and we made MongoDB available to our Wamp (Apache Server). So, if we run PHP codes for MongoDB (just the way we do mysql_query() for MySQL DB), it would smoothly run.

The process is almost similar in Xampp. We need to download the correct version of php_mongo.dll and keep it C:\xampp\php\ext\ folder. Next, we need to mention this mongoDB driver php_mongo.dll inside the php.ini file. And then restart the Apache server within Xampp. Check the screenshot below .. 




In our next article, we would create a New Database and access it from within PHP itself.

No comments: