In this tutorial, we will try to connect MongoDB with PHP and in the end we will check up our complete setup.
If you are not aware of MongoDB you may refer to our previous blog on MongoDB: https://internityfoundation.wordpress.com/2018/03/26/which-spouse-youd-choose-mongodb-or-mysql/
The System configuration I used for setup is as follows:
Operating System: Windows 10
For PHP and Apache: XAMPP bundle with php v7.1 and 32 bit.
MongoDB v3.6 Community Server.
Composer for windows
So, let’s start with MongoDB installation process. As I found it a bit complex to install the MongoDB first time so I thought of including the steps I took to successfully run the MongoDB server, may be you find better ways later.
You may also refer the tutorial available on the MongoDB website https://docs.mongodb.com/tutorials/install-mongodb-on-windows/ or the following steps:
Step 1: Download the MongoDB installer from this page. https://www.mongodb.com/download-center#community
Step 2: Run the downloaded installer, please select the custom installation process and uncheck the MongoDB Compass installation as this is in Beta phase and when I tried to install it was giving some issues.(at the time of writing the blog, may be you may get a stable release)
My installation Directory: C:\Program Files\MongoDB\Server\3.6\
Step 3: After successful installation of MongoDB, we need to create the database directory and the log directory.
Now, open the command prompt, with Administrator access and run the following commands:
mkdir c:\data\db mkdir c:\data\log
Step 4: Starting the MongoDB server, either you can run the C:\Program Files\MongoDB\Server\3.6\bin\mongod.exe by just opening it or entering the above path in command line.
If everything goes fine till here, you will get a screen, something like this:
Now, as our MongoDB server has been started, MongoDB provides another console to run and execute the commands. In the same direcotry you will find momgo.exe, by opening it you will get another console window to execute you commands.
For testing the setup till here you can run the command
You will get the list of Databases available.
Now, lets begin with connecting our MongoDB with PHP application.
For this we will need driver for php on Windows.
You can find the Drivers list for php on MongoDb website: https://docs.mongodb.com/ecosystem/drivers/php/
The DLL file for driver has many options to download and for a first beginner its again a workaround to download the correct dll file.
You may find something like this when you goto download the drivers, we need to choose the correct version. So, as I said I am using XAMPP 32bit so I needed to download the 32bit but there is one more thing, Thread Safe and Non Thread Safe. You need to check which php version you are using. For this you can check the phpinfo() and there you may find Thread Safety.
If its enabled then you need to download Thread Safe version else Non Thread Safe.
So, now as you have downloaded the zip file, extract it and then move the .dll file in your xampp\installation\directory\php\ext\
For me it was: C:\xampp\php\ext\php_mongodb.dll
As, we have moved the .dll file we need to give the entry for this dll file in our php.ini file.
Before updating php.ini, I will suggest you to take a backup of this file as this contains all the php configurations.
In this file search for Windows extensions and in the end add the following line:
Save the configuration file and restart the apache server for the new settings to take effect.
MongoDB PHP Library
Now, as php-driver is installed we will need to use the driver compatible mongo-db library to start using it.
You can refer to this link for installing the mongo-db library for php: http://php.net/manual/en/mongodb.tutorial.library.php
Do remember to run the composer the composer in the htdocs directory of Xampp server so as to have access to the library files.
In my case I have created a project directory for using MongoDB in C:\xampp\htdocs\mongodb and from inside this directory I ran the command composer require mongodb/mongodb. I assume you have installed the composer in the beginning else this tutorial would have become too long.
If you are all well till now and have all things going great, It’s time to write some php script to test all.
<?php require 'vendor/autoload.php'; $client = new MongoDB\Client("mongodb://localhost:27017"); $db = $client->internity; $result = $db->createCollection('first entry'); var_dump($result);
After executing the above script will create a database internity if it’s not present or else if it’s present it will write a Collection first entry.
You can check in this screenshot internity database is created.
So, you are all set for building your application from here. Start exploring MongoDB your NoSQL. If you get stuck anywhere do let us know in the comment blow. Hope this will help you in your setup process.