PHP Connection to MongoDB
MongoDB is a widely used NoSQL database, implemented based on the document-oriented model and intended for storing semi-structured data. The guide below provides an instruction on how you can easily use it in a bundle with your PHP app, hosted within the platform.
Create an Environment
Log into your platform account and create a new environment with the MongoDB node of the preferred version (can be found within the NoSQL wizard section).
Add all the rest of the necessary instances (if there are any - we’ll also include Apache to subsequently deploy a test app to it; however, it could be located in a separate environment as well) and configure other required parameters, like amount of resources, region, environment name, etc.
Click the Create button when ready and wait for a couple of minutes for your environment to be set up.
MongoDB Configurations
1. Enter your email box and find a letter from Robot@iNET.elastic, which contains your MongoDB instance details and access data:
2. Here, click on the Access URL link or switch to the dashboard and Open in browser your MongoDB node.
In the opened browser tab, you’ll see the Sign in form for the MongoDB admin panel. Enter the admin credentials you’ve received within the abovementioned email and click Login to access it.
3. Now, let’s create a separate database to establish the connection to. For that, switch to the Databases tab and specify a Name for it within the Create Database section (for example, mongodb-connect).
Click Save to proceed.
4. The next step is to create a separate DB user for working with our newly added database. Thus, switch to the Execute tab and paste the following command inside the shown input field:
db.createUser({ user: “user_name”, pwd: “password”, roles:[{ role: “readWrite”, db: “db_name"}]})
where
- user_name- name for your new DB user
- password - password for this user
- db_name - database (the above created one is suggested) this user will have the read/write permissions for
Then, select the corresponding database using the drop-down list below and Execute the specified command with the same-named button. You’ll get the success response in just a few seconds.
5. Now you need to activate the dedicated connection driver for enabling the interaction between your app server and MongoDB. At the platform, it is included to all PHP app servers' builds by default.
Note: Starting with the 4.3 PaaS version, there are two driver versions available at all of the newly created PHP app servers:
- mongo.so (currently, it’s considered deprecated)
- mongodb.so
Both these extensions implement different API, so we recommend to use the legacy module for keeping old applications running, whilst adapt your new projects due to the latest one.
Tip: The current version of used PaaS installation usually can be seen within the logo in the top left dashboard corner. Additionally, you can check it within the 'World Cloud' - under construction Catalog.
So, in order to enable the required driver, return to your dashboard, hover over the compute node in your environment and click on the appeared Config button.
6. Within the opened configuration manager tab, expand the etc folder and choose the php.ini file inside.
Scroll down to the approximately 483rd line and uncomment the string with the required driver (either mongo.so or mongodb.so extension) by removing the semicolons mark at its beginning.
7. Save the performed changes and Restart your app server node to apply them.
Application Deployment
Now you can proceed to application deployment into the environment you’ve just prepared using the platform Deployment Manager (for projects, packed into a single archive or fetched directly from a GIT/SVN repo).
As an example, we’ll use the following simple app, designed to verify the establishment of connection between the corresponding compute node and the specified MongoDB server by means of the latter mongodb.so extension.
index.php:
|
|
Thus, if you’d like to test the connection itself, just download the already wrapped package with the project, shown above, and deploy it.
Tip: For the legacy driver version (i.e. mongo.so), please use this test application. The workflow will be similar to the described below.
Consequently, you’ll receive an environment, similar to the one shown above.
Connection Check Up
1. Open your environment in Browser with the same-named button - you’ll see a simple form for entering your MongoDB database details.
The following values should be specified:
- Host - link to your database admin panel without the https:// part (can be found within the corresponding email or just copied from the browser address bar after clicking Open in browser for your MongoDB node)
- User - name of the user you’ve assigned to the database (dbuser in our case)
- Password - password for the user, specified within the previous field
- Database - name of the required database (mongodb-connect in our case)
After the data is entered to the corresponding fields, click on the Test Me! button.
2. If the specified information is correct, the Good connection message will be shown.
Otherwise, you’ll get a notification that your connection Doesn’t work and the detailed output of the error this was caused by.
3. In addition, upon a successful connection establishment, the new phptest collection with a few new records inside will be added to the specified database.
Thus, you can navigate to your DB’s admin panel for making sure everything works correctly and proceed with performing any other required operations.
Useful to Know
The platform Developer’s Center also contains a number of more specific MongoDB guides, that may come in handy for a variety of different tasks:
- configure a Replica Set with an uneven number of MongoDB nodes to simultaneously implement the master-slave replication and automated failover
- increase your information safety through setting Backups Scheduling, which may prevent data loss or minimize its consequences (through recovering) if an unexpected server failure occurs
- use the Remote Access option for operating with your database directly via the preferred client application, getting rid of the necessity to log in to our dashboard for that
- examine how to Import & Export Dump files using the inbuilt or 3d-party DB management clients for easily backing up your data