Connecting MongoDB Atlas to a PHP Application
MongoDB Atlas is a fully managed cloud database developed by the same team that builds MongoDB. If you're building a PHP application and want to harness the power of a scalable, flexible NoSQL database, MongoDB Atlas is a top choice. In this article, we'll walk through how to connect a PHP application to MongoDB Atlas, step-by-step.
Why Use MongoDB Atlas with PHP?
Before diving into the connection process, here are a few benefits of using MongoDB Atlas with PHP:
-
Fully managed database: Automatic backups, monitoring, scaling, and security.
-
Global cloud infrastructure: Deploy your database in the region closest to your users.
-
Flexible schema: Store and manage diverse data types without rigid schema constraints.
-
Native PHP support: The official MongoDB PHP driver ensures smooth integration.
Prerequisites
To follow this guide, you’ll need:
-
A MongoDB Atlas account
-
PHP 7.1 or higher installed on your system
-
Composer installed for dependency management
Step 1: Set Up a Cluster on MongoDB Atlas
-
Sign in to your MongoDB Atlas account.
-
Click “Build a Cluster”.
-
Choose your cloud provider and region.
-
Select the cluster tier (the free tier is sufficient for testing).
-
Click Create Cluster.
Once your cluster is ready:
-
Create a database user with a strong password.
-
Whitelist your IP address under Network Access.
-
Navigate to Database > Connect > Connect your application, and copy the connection string.
Example connection string:
mongodb+srv://<username>:<password>@cluster0.mongodb.net/<dbname>?retryWrites=true&w=majority
Step 2: Install the MongoDB PHP Library
Use Composer to install the official MongoDB PHP library:
composer require mongodb/mongodb
Also, make sure the MongoDB PHP extension is installed:
sudo pecl install mongodb
Add the following line to your php.ini
:
extension=mongodb.so
Step 3: Connect to MongoDB Atlas in PHP
Create a file called connect.php
and use the following code:
<?php
require 'vendor/autoload.php';
$uri = "mongodb+srv://<username>:<password>@cluster0.mongodb.net/?retryWrites=true&w=majority";
$client = new MongoDB\Client($uri);
$db = $client->selectDatabase('your_database_name');
$collection = $db->selectCollection('your_collection_name');
$result = $collection->find();
foreach ($result as $entry) {
print_r($entry);
}
?>
Replace <username>
, <password>
, your_database_name
, and your_collection_name
with your actual credentials.
Step 4: Test Your Connection
Run the script using:
php connect.php
If everything is set up correctly, you should see documents from your MongoDB Atlas collection printed on the screen.
Common Troubleshooting Tips
-
Authentication Errors: Double-check your username/password and make sure special characters in the password are URL-encoded.
-
Network Access Denied: Ensure your current IP is whitelisted in the Atlas dashboard.
-
Driver Not Installed: Ensure the
mongodb
extension is correctly installed and enabled inphp.ini
.
Conclusion
Connecting MongoDB Atlas to a PHP application is straightforward with the right tools. By leveraging the official MongoDB PHP driver and Composer, you can quickly set up and interact with your cloud database. This setup empowers you to scale and manage data with ease while building modern PHP web applications.
Happy coding!