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.
- 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
xampp\php\extdirectory.I have installed xampp in
D:drive so my path will be
- Now open
Configand then click on
PHP(php.ini)in XAMPP control panel.
php.iniand 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
- Now create a
htdocsto check php and mongodb communication.
- Now run
mongo-test.phpfile 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)
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017/");
Step by Step Images: