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:
- How to Install Composer on Windows 10
- How to install Codeigniter in Xampp
- How to install Slim Framework 3 in windows using XAMPP
- How to install Laravel 5.6 on windows using XAMPP
Steps: Install Mongodb Driver for PHP 7 on XAMPP
- 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.
- 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.
- After downloading zip file. Unzip that zip file and copy
php_mongodb.dll
to yourxampp\php\ext
directory.I have installed xampp inD:
drive so my path will beD:\xampp\php\ext
. - Now open
php.ini
by clickingConfig
and then click onPHP(php.ini)
in XAMPP control panel. - Add
extension=php_mongodb.dll
inphp.ini
and save it. - Restart XAMPP.
- 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.
- Now open cmd and start mongodb server my typing
mongod
command. - Now create a
mongo-test.php
file inhtdocs
to check php and mongodb communication. - 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)
1 2 3 4 5 6 7 8 9 |
<?php $manager = new MongoDB\Driver\Manager("mongodb://localhost:27017/"); var_dump($manager); ?> |
Step by Step Images: