PHP: Class 'MongoClient' not found after installed (Linux) - php

I have mongo server install and the mondo php extension enable:
Why am I still getting this error:
Fatal error: Class 'MongoClient' not found in ...
My code:
// connect to mongodb
$m = new MongoClient();
var_dump($m);
How I install mongo server:
`$ sudo apt-get install mongodb-server`
How I install mongo client:
`$ sudo apt-get install php-pear php5-dev`
`$ sudo pecl install mongodb`
Add extension=mongodb.so at the end of php.ini
Restart Apache sudo /etc/init.d/apache2 restart
What have I missed?

Try this:
Run below command in your project using terminal:
composer require mongodb/mongodb
Add below code in your php file:
require 'vendor/autoload.php';
$client = new MongoDB\Client("mongodb://localhost:27017");
$collection = $client->db->collection;
Go through this documentation for more details.
Hope this may helpfull

Open your Ubuntu Software Centre and search for mongo driver.
Select MongoDB Database Driver php5-mongo and install it.
Restart Apache: sudo /etc/init.d/apache2 restart
I have no idea what mongodb is installed for!!!

The MongoClient class was provided with the now deprecated mongo driver. You installed the mongodb driver, which provides the Manager class:
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
var_dump($manager);
The mongodb driver provides a minimal API for core driver functionality, but no classes such as MongoClient. The MongoDB PHP library provides a high-level abstraction around this lower-level driver, e.g. a Client class, and can be installed with composer.

Related

Installing Mongodb PHP Drivers on Ubuntu 18.04

I have installed mongodb on a new Ubuntu 18.04 server. I can access and work with the database through the command line, but cannot access it via PHP, and was wondering how other people have gotten it to work.
Following a new build of Ubuntu 18.04, and installation of LAMP, I ran:
sudo apt install mongodb-server php-pear php7.2-dev
sudo pecl install mongodb
I then added the mongodb.so extension to the php.ini file, and a phpinfo(); page shows mongodb as installed. So far so good.
If I browse to a test php page ($mongo=new Mongo();), the page fails to load though.
Through web searches, I find the https://github.com/alcaeus/mongo-php-adapter page come up a lot, and I have run the composer installation command there, but to no avail.
Installed versions are: Ubuntu 18.04.2 LTS; mongoDBv3.6.3; php 7.2.17; Apache 2.4.29
I was wondering what steps other people who have gotten PHP to work successfully with Mongodb have followed? I plan to scrap this server and start from a fresh VM install again.
I was able to install php-mongodb after adding the ppa repository ondrej/php:
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt-get install php-mongodb
try:
sudo apt-get install php-mongodb
worked for me
It appears that you're attempting to use the Mongo class from the legacy driver, but you're using the non-legacy driver so that class doesn't exist. Please look at the documentation for the updated version of the driver here. You might also consider using the MongoDB PHP Library to abstract away many of the driver calls.
Please note that there are also compatibility considerations between your versions of the PHP library, MongoDB, and the MongoDB PHP driver. You can find the compatibility information for them here.

MongoDB Exception: Server reports wire version 0, but version of libmongoc requires at least 3

Fatal error: Uncaught MongoDB\Driver\Exception\ConnectionException: Server at localhost:27017 reports wire version 0, but this version of libmongoc requires at least 3 (MongoDB 3.0)
I have PHP 7.0.13, MAMP and MongoDB. The MongoDB extension for PHP has been installed.
I have the following:
<?php
ini_set('display_errors', 'On');
require 'vendor/autoload.php';
var_dump(extension_loaded('mongodb'));
echo phpversion('mongodb')."\n";
$m = new MongoDB\Driver\Manager("mongodb://localhost:27017");
// Query Class
$query = new MongoDB\Driver\Query(array('age' => 30));
// Output of the executeQuery will be object of MongoDB\Driver\Cursor class
$cursor = $m->executeQuery('testDb.testColl', $query);
// Convert cursor to Array and print result
print_r($cursor->toArray());
?>
What does 'wire' refer to in this context, and does anyone have a solution for this problem?
I have got the problem on Linux Mint 19 (think that Ubuntu 18+ can have the same problem):
Server at IP:27017 reports wire version 2, but this version of libmongoc requires at least 3 (MongoDB 3.0)
As the message says - server driver version and mine one are different.
This happened because I installed php mongo driver with the command:
sudo apt-get install php7.2-mongodb
The SOLUTION was to completely uninstall php mongo driver:
sudo apt-get remove --auto-remove php-mongodb
and then install php-mongodb from Pecl mongodb php extension:
sudo pecl install mongodb-1.4.4
(If you bump into error pecl: command not found, just install PEAR package in order to use pecl installer. sudo apt-get update && sudo apt-get install php-pear)
After that add the next line to your php.ini file:
extension=mongodb.so
Don't forget to reload the web server:
sudo systemctl reload apache2
That's it. Everything should work!
If anyone searching for solution for Centos 7, here's what you need to do (based on Evgeny Melnikov answer):
yum erase php-pecl-mongodb && pecl uninstall mongodb
pecl install mongodb-1.9.1
Then add extension=mongodb.so to your php.ini file and restart php-fpm.
I have a Raspberry Pi 3B with mongo version. It runs an older version of MongoDB, so I found an equally old version of pymongo and it worked.
mongo --version
MongoDB shell version: 2.4.14
min_wire_version was added sometime after the release of Mongo 2.4.14, so I just installed pymongo drivers that were equally as old.
pip install pymongo==2.4.2 worked for me.
https://pecl.php.net/package-info.php?package=mongodb&version=1.13.0
pecl uninstall mongodb
pecl install mongodb-1.13.0

MongoClient Not found connection php mongolab

I'm trying to connect to Mongolab db server, but I get this error:
Fatal error: Class 'MongoClient' not found in /home/viviane/public_html/sk/zizi/head.php on line 33
I suspect it is related to the PHP Mongo Driver, but I don't know how. If I try to connect to a remote mongodb server
$uri = "mongodb://dzyasser:dzyasser#ds049624.mongolab.com:49624/serverdb";
$options = array("connectTimeoutMS" => 30000);
$connection = new MongoClient($uri, $options );
So why am I still getting this error when I'm trying to connect to a remote Mongodb server (Mongolab)?
Its look like you don't have MongoDB PHP extension in your system.
Try to install MongoDB PHP extension in order to solve your problems with the MongoClient class. This is how I setup MongoDB PHP extension in OS X.
Get MongoDB extension from PECL
$ sudo pecl install mongodb
Make a copy of php.ini.default
$ sudo cp /etc/php.ini.default /etc/php.ini
Put this line on php.ini than save
extension=mongodb.so
Check the installation using this command
php --re mongodb
Or create simple php document that contain phpinfo() function
If you get problem with writing the extension, please read this post.
I assume, the Mongo driver is installed and loaded.
If so, since the error message says the MongoClient class cannot be found, it might be caused by a namespace used in your script. If so, try
$connection = new \MongoClient($uri, $options );
Regardless of whether you are connecting locally or remotely, it seems PHP requires the mongo DB service to be running on your server. So install it like so:
a) Install mongodb service
$ sudo apt-get install -y mongodb
b) Add the php drivers if they are missing:
$ sudo apt-get install php5-dev php5-cli php-pear -y
$ sudo pecl install mongo
c) Then open your php.ini file and add to it the extension line "extension=mongo.so":
$ sudo vi /etc/php5/apache2/php.ini
> extension=mongo.so
d) Then restart apache
$ sudo service apache2 restart
Disclaimer: This example was tried on Ubuntu 14.04 LTS with Apache web-service and PHP5 installed. If your installation is different, follow the same approach but issue appropriate commands. If you have no access to SSH or sudoer privileges, contact someone who can help you do this.

Using memcached on XAMPP

I am using XAMPP. When trying to use
$memcached = new Memcached();
I get the error: Fatal error: Class 'Memcached' not found in C:\xampp\htdocs\dir\index.php
I have done a lot of searching and cannot find a way to install Memcached.
Thanks
Alex
You dont have installed Memcached extension on your XAMPP PHP Version: If you working on UNIX enviroment you can simply install memcached using command
// Step 1.
$ sudo apt-get install memcached
// Step 2.
$ sudo apt-get install php5-memcached
// Step 3.
$ sudo /etc/init.d/apache2 restart
If you using Windows enviroment you should use Memcache extension instead Memcached. Detail installation on windows bellow:
http://zurmo.org/wiki/installing-memcache-on-windows

Can't get mongo.so extension to load after updating PHP to 5.3

I had PHP 5.2 with MongoDB installed on Debian Lenny x64 and everything was fine.
After updating PHP to 5.3 (dotdeb) I can't get MongoDB to work, I always get an error
Fatal error: Class 'Mongo' not found (...)
Everything else works fine, all default modules are working.
My php.ini:
extension_dir = "/usr/lib/php5/20090626"
extension=mongo.so
Mongo.so is in same location as written above.
But when I run php -m in console to check loaded modules, "mongo" isn't listed there.
I can't use dl('mongo.so') to load module at runtime, because this function was deprecated in PHP 5.3.
May be I should recompile mongo somehow, I just don't know how to do that because I'm not very good in *nix commands.
Thanks for your help!
UPDATE
Also may be it's worth saying that before my mongo.so was in /usr/lib/php5/20060613 and I manually copied it to "/usr/lib/php5/20090626" because it seems that after updating my PHP all modules are located there.
The extension module api has changed between php5.2 and php5.3. When php tries to load an extension module both sides have to "present" an api magic key that identifies the api version. If those numbers don't match the module is not loaded/activated.
Try sudo pecl install mongo again to get an extension module that fits your new php version.
Try re installing mongo & restart service
sudo pecl uninstall mongo
sudo pecl install mongo
sudo service apache2 restart
This will install mongo in to newer version
I spent a little time trying to fix this, I am guessing you had a previous mongo install. The way I fixed it was by:
I installed php5-dev:
sudo apt-get install php5-dev
cd /etc/php5/apache2/conf.d/
sudo rm -rf mongo.ini
I then viewed my php.ini and removed extension=mongo.so
Then:
sudo pecl uninstall mongo
sudo pecl install mongo
sudo service apache2 restart

Categories