PHP Connection to MongoDB

PHP Connection to MongoDB

This guide shows how to connect a PHP application hosted on the platform to MongoDB. The workflow covers MongoDB environment creation, database and user setup, activation of the PHP MongoDB driver, application deployment, and connection verification.

Create the Environment

1

Create a MongoDB environment

Create a new environment and add a MongoDB node of the required version from the NoSQL section. Add any other required nodes as well. The source example adds an Apache PHP server for the test application, although the PHP server can also be located in a separate environment.

Create MongoDB environment
Create an environment with MongoDB and, if required, a PHP application server such as Apache.

Configure the required resources, region, environment name, and other parameters, then click Create.

MongoDB Configurations

1

Check the MongoDB access details

After environment creation, check the email sent by the platform. It contains MongoDB instance details and access credentials.

MongoDB credentials email
The platform email contains MongoDB instance details and access credentials.
2

Open the MongoDB administration panel

Use the Access URL from the email or click Open in Browser for the MongoDB node. Enter the supplied administrator credentials and click Login.

MongoDB admin panel login
Open the MongoDB administration panel and sign in with the supplied credentials.
3

Create a database

Open the Databases tab and create a separate database for the application. The source example uses mongodb-connect.

Create MongoDB database
Create a separate MongoDB database for the PHP application.
4

Create a database user

Open the Execute tab and run the following command:

db.createUser({
  user: "user_name",
  pwd: "password",
  roles: [
    { role: "readWrite", db: "db_name" }
  ]
})
user_nameName of the new MongoDB user.
passwordPassword for the new user.
db_nameDatabase for which the user receives read/write permissions.
Create MongoDB database user
Run db.createUser to create a user with read/write access.

Select the corresponding database from the drop-down list below the command field and click Execute.

Enable the PHP MongoDB Driver

PHP application server builds include two MongoDB driver versions:

  • mongo.so — deprecated legacy driver
  • mongodb.so — current driver used for new projects

Driver recommendation

The source recommends keeping mongo.so only for older applications that depend on its legacy API and using mongodb.so for new projects.

Return to the dashboard and click Config for the PHP application server.

PHP node configuration
Open the PHP application server Config panel.

Open etc/php.ini, locate the required MongoDB extension line, and remove the leading semicolon to enable it.

Enable MongoDB PHP driver
Uncomment the required MongoDB driver in php.ini.

Save the configuration and restart the application-server node.

Restart PHP application server
Restart the PHP application server after enabling the driver.

Application Deployment

Deploy the PHP application using Deployment Manager from a packaged archive or directly from a Git/SVN repository. The source test application uses the newer mongodb.so extension to verify the connection.

The example builds the connection with MongoDB\Driver\Manager, writes three documents, and queries the inserted data:

$manager = new MongoDB\Driver\Manager(
    "mongodb://{$host}/{$userdb}",
    array(
        "username" => $username,
        "password" => $password
    )
);

$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(['x' => 1]);
$bulk->insert(['x' => 2]);
$bulk->insert(['x' => 3]);

$manager->executeBulkWrite($database, $bulk);

$filter = ['x' => ['$gt' => 1]];
$options = [
    'projection' => ['_id' => 0],
    'sort' => ['x' => -1]
];

$query = new MongoDB\Driver\Query($filter, $options);
$cursor = $manager->executeQuery($database, $query);

The source also provides a ready-made test package. A separate test application is available for the deprecated mongo.so driver.

MongoDB PHP application deployed
The test application is deployed to the prepared environment.

Connection Check Up

1

Open the test application

Click Open in Browser for the PHP environment. The test form requests the MongoDB connection details.

MongoDB connection test application
Enter the MongoDB host, user, password, and database to test the connection.
HostMongoDB admin-panel address without the https:// part.
UserName of the user assigned to the database. The source example uses dbuser.
PasswordPassword for the specified database user.
DatabaseName of the database, mongodb-connect in the source example.

Enter the connection values and click Test Me!.

If the details are correct, the application displays Good connection. Otherwise, it displays a connection failure message together with the error details.

Good MongoDB connection
A successful connection displays the Good connection message.
2

Verify the test collection

A successful connection also creates a new phptest collection with several records in the selected database. Open the MongoDB administration panel and confirm that the collection and records are present.

Check phptest collection
A successful test creates the phptest collection and sample records.

Expected Result

The PHP application connects successfully to MongoDB using the enabled mongodb.so driver, authenticates with the configured database user, writes test records, performs a query, displays a successful connection message, and creates the phptest collection in the selected MongoDB database.

Important Notes

  • The MongoDB and PHP application servers can be in the same or separate environments.
  • The source example uses Apache PHP as the application server.
  • The example database is mongodb-connect.
  • The database user receives the readWrite role.
  • mongo.so is deprecated; the source recommends mongodb.so for new projects.
  • Enable the selected driver in php.ini and restart the application server.
  • Use the MongoDB admin-panel host without the https:// prefix in the connection test.
  • A successful test creates the phptest collection.