In order to use MongoDB in our PHP programs, we need to install php mongodb driver. It is a PHP extension that manages the connection to the MongoDB server and enables you to perform all kinds of operations on a NoSQL database through PHP.
In this tutorial we will see how to install and configure the mongodb php driver on Ubuntu, CentOS 7 and Microsoft Windows. As distributions and environments vary, installation instructions will also vary.
MongoDB PHP Driver for Ubuntu provides by the php-mongodb package which we can install with apt-get command.
sudo apt-get update
sudo apt-get install php-mongodb
Then, restart the Apache Web Server:
sudo systemctl restart apache2.service
To verify the installation, we can run phpinfo() function. The phpinfo page should display mongodb details as shown below.
From the command line you can run php -i command to get information on mongodb driver.
php -i | grep mongo
The php mongodb driver on CentOS 7 for PHP 5 available from the epel-repository.
First, enable the epel repository:
sudo yum -y install epel-release
sudo yum repolist
Install the php-mongodb package:
sudo yum -y install php-mongodb
Restart the HTTPD server:
sudo systemctl restart httpd
If you installed PHP 7 using webtatic repository, then the package name should be something like “php71w-pecl-mongodb”. You can search available packages with yum search command.
yum search php | grep -i mongo
Selinux will block mongodb driver on CentOS 7. As a solution you can either disable Selinux or enable the ‘httpd_can_network_connect_db’ boolean.
sudo setsebool -P httpd_can_network_connect_db 1
Following is the sample PHP error caused by Selinux on CentOS 7:
PHP Fatal error: Uncaught MongoDB\\Driver\\Exception\\ConnectionTimeoutException: No suitable servers found (`serverSelectionTryOnce` set): [connection refused calling ismaster on 'localhost:27017']
Important: For Windows, You need to download correct mongodb driver for PHP, based on your PHP version, Architecture and whether Thread Safety is enabled. You can get those information from the phpinfo page.
Do the following steps to install and configure MongoDB driver on Windows XAMPP Server.
extension=php_mongodb.dll
Let’s write a very simple PHP program that creates a connection to the MongoDB server and dump the connection status.
Add following PHP code to your php script and access from the Web browser.
<?php
$connection = new MongoDB\Driver\Manager("mongodb://localhost:27017");
var_dump($connection);
?>
The above PHP example will output something similar to:
object(MongoDB\Driver\Manager)#1 (2) { ["uri"]=> string(25) "mongodb://localhost:27017" ["cluster"]=> array(1) { [0]=> array(11) { ["host"]=> string(9) "localhost" ["port"]=> int(27017) ["type"]=> int(0) ["is_primary"]=> bool(false) ["is_secondary"]=> bool(false) ["is_arbiter"]=> bool(false) ["is_hidden"]=> bool(false) ["is_passive"]=> bool(false) ["tags"]=> array(0) { } ["last_is_master"]=> array(0) { } ["round_trip_time"]=> int(-1) } } }
And that is mean, you have a connection to the MongoDB Server in your PHP script.
Berikut jadwal imsak dan buka puasa Ramadhan 2025 di Kota Serang dari tanggal 1 Ramadhan -…
Jakarta - Pemerintah telah menetapkan awal Ramadan 2025 berdasarkan hasil sidang isbat oleh Kemenag. Begitu pula dengan organisasi masyarakat…
Siapa sangka, salah satu warganet justru mendapat informasi tak terduga yang berasal dari Meta AI.…
Ketika menjalani rutinitas sehari-hari tentu saja kamu pernah merasa suntuk atau jenuh. Supaya kamu bisa…
Sebuah kapal kargo komersial China diduga sengaja menyeret jangkarnya untuk memotong kabel bawah laut yang…
Nabi Sulaiman dikenal sebagai raja yang kaya raya. Para ilmuwan arkeologi kini mengungkap apa sumber kekayaannya,…