How to install Mongodb driver for PHP 7 on XAMPP – Step by Step

As we have seen in my earlier post that how to install and run mongodb on windows 10. But we didn’t see how mongodb will connect to php. I usually use xampp for my local development which have php 7.2 version. So we will be configuring mongodb on xampp to make it works.

If you don’t have xampp on your machine, please have a look on how to install xampp on windows 10.

By default, PHP in XAMPP has no mongodb support. So we have to download mongodb extension from PECL (PHP extension repository) and configure it in xampp.

So lets started.

Also Read:

Steps: Install Mongodb Driver for PHP 7 on XAMPP

  1. Check your current PHP version and also check that it is Thread Safe or not. My PHP version is 7.2 with thread safety enabled.
  2. Goto https://pecl.php.net/package/mongodb/1.5.3/windows and download driver according to your windows and php version. I am going to download 7.2 Thread Safe (TS) x86 because I have windows 10 64bit.
  3. After downloading zip file. Unzip that zip file and copy php_mongodb.dll to your xampp\php\ext directory.I have installed xampp in D: drive so my path will be D:\xampp\php\ext.
  4. Now open php.ini by clicking Config and then click on PHP(php.ini) in XAMPP control panel.
  5. Add extension=php_mongodb.dll in php.ini and save it.
  6. Restart XAMPP.
  7. Now again check PHP info. Now there will be a separate section for mongodb. If you didn’t find mongodb section in phpinfo then make sure you downloaded current mongodb driver zip.
  8. Now open cmd and start mongodb server my typing mongod command.
  9. Now create a mongo-test.php file in htdocs to check php and mongodb communication.
  10. Now run mongo-test.php file from browser. If all goes well, you should get object.

Note: Default address of mongodb server is 127.0.0.1 and default port is 27017.

Test code: (mongo-test.php)


Step by Step Images:

check php version thread save

download-dll according to php version

extract mongodb dll files

copy mongo dll to xampp php ext folder

open php ini file

add extension php mongodb dll

checkmongodb extension enabled

start mongodb server

run mongodb script

 

Author: Ahsan Zameer

I am a web developer and love to search new stuff on web. Trying different approaches and ideas to make web developing more interesting and enjoyable.