Li'l Help Please
Li'l Help Please

Reputation: 53

How to get the MongoDB PHP Connection to Work

I have MongoDB installed in my cPanel/WHM CentOS server. I have the PHP Drivers installed. I have Port 27017 opened.

This works $m = new MongoDB\Driver\Manager();

These do not work:

$m = new Mongo(); $m = new MongoClient();

Both result in Fatal error: Class 'Mongo' not found in ... and Fatal error: Class 'MongoClient' not found in ....

Does anyone know why?

Not a duplicate. That SO Q is for a Windows machine. My server is Linux/CentOS

From My Hosting Provider's Sys Admin:

It looks like "mongo" is a seperate php module from "mongodb", and that "mongodb" is the newer one:

root@host [~/support/642192]# pecl search mongo Retrieving data...0%

.Matched packages, channel pecl.php.net:

Package Stable/(Latest) Local mongo 1.6.12 (stable) MongoDB database driver (legacy)

mongodb 1.1.2 (stable) 1.1.2 MongoDB driver for PHP

Did you need the legacy module "mongo" instead of "mongodb"? Here is hte pecl page for the package you have:

https://pecl.php.net/package/mongodb

and here is the one for the legacy module:

https://pecl.php.net/package/mongo

Upvotes: 1

Views: 2024

Answers (3)

V. Högman
V. Högman

Reputation: 86

Short answer

You cannot use the following classes with the new mongodb driver: $m = new Mongo(); $m = new MongoClient(); This corresponds to the legacy mongo driver. Instead you should use MongoDB\Client through the MongoDB PHP Library.

Long answer

All right. I lost some hair on this story as well, because the documentation about Mongo and PHP is extremely confusing. The options are then to get mad, bald, or both. But i found courage and finally got it. So this might help you.

1. MongoDB driver: mongo vs mongodb

First of all, you must clarify which MongoDB driver you use: either mongo (legacy) or mongodb (new). Note the smaller case. First source of confusion, the mongo driver is sometimes referred to as MongoDB (legacy) PHP driver.

https://docs.mongodb.com/ecosystem/drivers/php/

Second source confusion, the version numbers are not logical, as mongo driver had 1.5, 1.6 but mongodb starts again from 1.0. So, going forward, but backwards, it's just insane... Imo they should have started from 2.0!

The choice of the driver depends on your PHP version:

  • PHP5.3: you can only use the mongo legacy driver (1.5, 1.6)
  • PHP7.0: you can only use the new mongodb driver (1.1+)
  • PHP5.4, 5.5, 5.6: here you have the choice between the old mongo (1.5, 1.6) or the new mongodb (1.0, 1.1+)

But it's not over. The driver is just a low-level interface (aka PHP extension). Now we come to the programmer's API and it becomes even worse.

2. API: MongoClient vs MongoDB\Driver

Third source of confusion, the low-level drivers and the API classes have overlapping names.

  • mongo legacy driver -> classes MongoClient, MongoDB (!), ...
  • mongodb driver -> classes MongoDB\Driver, MongoDB\BSON, ...

But it's not over yet. The old mongo driver could be used directly. The new mongodb driver provides classes (such as MongoDB\Driver) but it is actually a low-level API. You are not supposed to use it directly, you could, but it's not convenient. Instead you should use the MongoDB PHP Library which gives an API similar to the old MongoClient classes...!

3. MongoDB PHP Library (with mongodb) -> MongoDB\Client

https://docs.mongodb.com/php-library/master/

So if you installed mongodb, you should install this MongoDB PHP Library in order to use MongoDB\Client. This class is supposed to be similar to the old MongoClient, but there are some differences such as the sort and projection.

To install this library you are advised to use the tool called Composer which allows to you download these classes into your repo. Then use the autoloader provided with Composer.

And here we come to the 4th source of confusion, MongoDB PHP Library is versioned from 1.0 even though you are using the last mongodb driver in version 1.1 ! It's certainly obvious for those who developed this stuff but very hard to follow for lambda users. Crazy confusing stuff.

There are also many other libraries above the drivers, but i don't know them at all so i won't go more into them (https://docs.mongodb.com/ecosystem/drivers/php-libraries/).

TL;DR

Depending on your PHP version, clarify which MongoDB driver to use: mongo (legacy) or mongodb (new) ?

  • with mongo you can use MongoClient class directly.
  • with mongodb you should also install MongoDB PHP Library to use MongoDB\Client class.

Good luck! :)

Upvotes: 2

Tim Hysniu
Tim Hysniu

Reputation: 1540

Looks like you haven't enabled or installed php_mongo extension. After enabling it restart apache and check phpinfo() to see if its properly enabled.

Upvotes: 1

meun5
meun5

Reputation: 322

You have use the correct namespace. As stated in their docs the namespace for the client is MongoDB\Client. Alterantivly you can use a use statement like this use MonogoDB\Client.

Upvotes: 0

Related Questions