9
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?

Weirdali
  • 413
  • 1
  • 7
  • 16
  • 1
    What version of MongoDB server are you using? The error message indicates that your driver requires at least MongoDB 3.0 and your server appears to be an older or unknown version. – Stennie Oct 06 '18 at 23:00

5 Answers5

15

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!

Evgeny Melnikov
  • 952
  • 7
  • 13
  • If you need to install mongodb for several versions of php with pecl, the instalation is convoluted a bit. In short, switch versions of php, install and backup lib, as installing on another version will remove previous one. – PeterM Oct 18 '18 at 08:49
  • Per [mongodb/mongo-php-library#569](https://github.com/mongodb/mongo-php-library/issues/569), `mongodb-1.5.0` was the first version to bump its minimum server version requirement. I'm not sure what version of the driver exists in Linux Mint 19, but Ubuntu 18.04's [php7.2-mongodb](https://packages.ubuntu.com/bionic/php-mongodb) package is 1.3.x and would not encounter this exception. Nor would version 1.4.4, which your answer suggests installing via PECL. That said, I think the best advice would be to upgrade the MongoDB server (versions <3.0 are _quite_ old) rather than downgrade the driver. – jmikola Oct 19 '18 at 20:59
  • Unfortunately, I have no opportunity to upgrade the version. That's why I was ought to downgrade the version in order to have the application work. But of course, it's better to use new version. You are right =) – Evgeny Melnikov Oct 22 '18 at 08:18
  • It turned out I had multiple versions of mongodb installed and I had to fully uninstall both before reinstalling the latest version. Thank you! – Weirdali Oct 24 '18 at 13:56
  • 3
    if previously you had install it with pecl, do following: `sudo pecl uninstall mongodb && sudo pecl install mongodb-1.4.4` – Ravi Misra Jan 15 '19 at 10:30
  • A related problem that I'm having: [https://askubuntu.com/questions/1418808/server-at-localhost27017-reports-wire-version-5-but-this-version-of-libmongoc](https://askubuntu.com/q/1418808/955434) – mickmackusa Jul 15 '22 at 04:55
2

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.

Ilya Shevyryaev
  • 756
  • 6
  • 8
1

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.

JustinDanielson
  • 3,155
  • 1
  • 19
  • 26
0

https://pecl.php.net/package-info.php?package=mongodb&version=1.13.0

pecl uninstall mongodb
pecl install mongodb-1.13.0
DEV Tiago França
  • 1,271
  • 9
  • 9
0

Please look at the driver compatibility with https://www.mongodb.com/docs/drivers/php/#compatibility-table-legend

Install the PHP MongoDB Driver According to the Mongo DB Server.

This will solve all the problems.

enter image description here

Kuppusamy
  • 453
  • 3
  • 11