This tutorial will show you how to configure MongoDB PHP 7 for XAMPP on Windows. XAMPP is an open source, easy to install Apache distribution containing PHP, Perl and MySQL. It’s very easy to install after downloading the Windows exe inataller file.

MongoDB is one of the widely used NoSQL(Not only SQL) database in market today. MongoDB is a document database with the scalability and flexibility that you want with the querying and indexing that you need. Often we come across a situation where we end up using MongoDB with PHP in XAMPP stack.

Since MongoDB does not ship with XAMPP stack so you have to setup manually.

Prerequisites

Knowledge of PHP, any database

Please go through the following steps in order to setup MongoDB with PHP7

Step 1. Download and install XAMPP on Windows

Step 2. Check the PHP version 7.2.1 has been installed. Create a phpinfo.php file under <installation directory>/xampp/htdocs and write below code into it.

<?php

echo phpinfo();

?>

Start XAMPP control panel and start the Apache module. Run the above file, you will find the php version as shown below image.

mongodb xampp php7

Step 3. Download and Install MongoDB. Here we will download community msi version.

mongodb php7 xampp

Step 4. Install MongoDB. Please install MongoDB Compass as well because it is a GUI tool and will help you manage and connect to MongoDB collections easily.

Step 5. Now once the installation is complete, create folder data\db in the same drive where you installed MongoDB. For example, if you installed MongoDB in C:\Program Files\MongoDB then create C:\data\db folders as it is required in order to save the database records.

Step 6. Now connect to MongoDB server using MongoDB Compass tool or using cmd prompt. MongoDB default port is 27017 and you just click on connect button from GUI tool. Using cmd prompt you have to use below commands.

Navigate to the MongoDB bin directory, for example

cd C:\Program Files\MongoDB\server\3.6\bin

Start the server

mongod.exe

Now you will see the following screen at bottom “waiting for connections on port 27017”. It means when you connect to MongoDB server from GUI or cmd prompt then you will find how many connections are there to this server.

Suppose you want to connect from cmd prompt, so use below command

Navigate to the MongoDB bin directory, for example

cd C:\Program Files\MongoDB\server\3.6\bin

Connect to the server

mongo.exe

Now as soon as you connect to the server you will find below changes in the server output

mongodb php7 xampp

Step 7. Download the MongoDB PHP driver in order to establish communication between these two technologies. Choose the file 7.2 Thread Safe (TS) x86.

Step 8. Now extract the archive and copy the php_mongodb.dll file into <xampp installation directory>/xampp/php/ext directory. Now open the <xampp installation directory>/xampp/php/php.ini file and add the below line in order to make it work.

extension=php_mongodb.dll

Step 9. Add php to System Variables. Open computer’s Advance system settings and click on Environment Variables and add <xampp installation directory>/xampp/php/ to Path under User’s variables (ex, ;<xampp installation directory>/xampp/php/). Click Ok.

Step 10. Now check php version from cmd prompt whether you have successfully added php to System Variables or not.

mongodb php7 xampp windows

Step 11. Now restart the Apache server in XAMPP control panel to reflect the changes you have made to php.ini.

Step 12. Now run the phpinfo.php file and check for mongodb. You should see something similar to below image.

mongodb php7 xampp windows

Step 13. Now test the MongoDB connection from PHP 7 using the below code in php-mongo.php file.

<?php

// Configuration
$dbhost = 'localhost';
$dbport = '27017';

$conn = new MongoDB\Driver\Manager("mongodb://$dbhost:$dbport");

print_r($conn);

?>

Step 14. Run the above php file, you will see below output in the browser.

mongodb php7 xampp windows

Congratulations! You have successfully connected to the MongoDB using PHP 7.

You may also like to read MongoDB PHP 7 CRUD Example.

Thanks for reading.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on JEE Tutorials | TwitterFacebook Google PlusLinkedin | Reddit | Email Me

2 thoughts on “Configure MongoDB with PHP 7 for XAMPP on Windows

Leave a Reply

Your email address will not be published. Required fields are marked *