How to Enable ServBay's Built-in MongoDB Module
As a powerful integrated web development tool, ServBay comes with a built-in MongoDB module, and enabling it is quite straightforward. MongoDB is a high-performance, open-source NoSQL database widely used in modern web development. With ServBay, developers can easily enable the MongoDB module to use MongoDB databases in PHP applications.
Introduction to the MongoDB Module
MongoDB is a document-based NoSQL database known for its high performance, flexible data model, and excellent scalability. MongoDB stores data in JSON-like documents and supports a rich query language, allowing developers to perform data storage and retrieval with ease.
Main Features
- High Performance: MongoDB offers high-performance data read/write capabilities through memory-mapped files and efficient indexing mechanisms.
- Flexible Data Model: MongoDB stores data in JSON-like documents, supporting dynamic schemas for flexible storage and manipulation of data.
- High Availability: MongoDB supports replica sets and sharded clusters, providing high availability and data redundancy.
- Rich Query Language: MongoDB supports complex queries, aggregation, and indexing operations, meeting various data processing needs.
- Easy to Scale: MongoDB's sharding mechanism enables easy scaling to handle large-scale data and high concurrency requests.
Versions of ServBay's Built-in MongoDB Module
ServBay supports multiple PHP versions and pre-installs the corresponding MongoDB module for each version. Specific versions include:
- PHP 5.6, 7.0: MongoDB 1.7.5
- PHP 7.1, 7.2, 7.3, 7.4: MongoDB 1.11.1
- PHP 8.0, 8.1, 8.2, 8.3: MongoDB 1.15.0
- PHP 8.4: MongoDB 1.19.1
How to Enable the MongoDB Module
By default, the MongoDB module is disabled. The steps to enable the MongoDB module are simple: navigate to Language
- PHP
, select the PHP version to enable the module, such as PHP 8.4
, click Extensions
on the right, then toggle the switch on the left of the MongoDB
module and save.
Users can also manually open or modify the module configuration, with the following detailed steps:
Step 1: Locate the Configuration File
First, locate the conf.d
directory for the corresponding PHP version. For example, to enable the MongoDB module for PHP 8.3, edit the following file:
/Applications/ServBay/etc/php/8.3/conf.d/mongodb.ini
Step 2: Edit the Configuration File
Open the mongodb.ini
file and uncomment the following content:
[MongoDB]
; Uncomment the following line to enable MongoDB
extension = mongodb.so
2
3
Step 3: Restart PHP Service
In the ServBay service management panel, restart the corresponding PHP service. For example, restart the PHP 8.3 service. Once the restart is complete, the MongoDB module will be successfully loaded.
Verifying the MongoDB Module is Successfully Loaded
You can create a simple PHP file to verify if the MongoDB module is successfully loaded. Create a phpinfo.php
file in the root directory of the web server with the following content:
<?php
phpinfo();
?>
2
3
Access https://servbay.host/phpinfo.php
, and look for MongoDB-related information on the PHP information page. If you see MongoDB-related information, the module has been successfully loaded.
Using MongoDB in PHP Code
Once the MongoDB module is enabled, the MongoDB client can be used in PHP code to perform database operations. Here is a simple example:
Example Code
<?php
require 'vendor/autoload.php'; // If using Composer for dependency management
$client = new MongoDB\Client("mongodb://localhost:27017");
$collection = $client->test->users;
// Insert a document
$insertResult = $collection->insertOne([
'name' => 'Alice',
'email' => '[email protected]',
'age' => 25
]);
echo "Inserted with Object ID '{$insertResult->getInsertedId()}'";
// Find a document
$document = $collection->findOne(['name' => 'Alice']);
echo "Found document: ";
print_r($document);
?>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Conclusion
ServBay provides a convenient way to manage and enable the MongoDB module. With simple configuration and restart operations, developers can quickly enable the MongoDB module in different PHP versions, thereby using MongoDB databases in PHP applications. MongoDB's high performance, flexible data model, and rich query capabilities make it an indispensable database solution in modern web development. With ServBay and MongoDB, developers can build efficient and scalable web applications.