1

I am trying to connect to MongoDB via PHP.

mongod --version
db version v3.2.8

Then,

php -i | grep mongo
 /etc/php/7.0/cli/conf.d/20-mongodb.ini,
 mongodb
 mongodb support => enabled
 mongodb version => 1.1.8
 mongodb stability => stable
 libmongoc version => 1.3.5
 mongodb.debug => no value => no value

I tried:

sudo pecl install mongodb

which returns

pecl/mongodb is already installed and is the same as the released version 1.1.8 
install failed

which (the install failed bit) gets redressed if I try:

sudo pecl uninstall mongodb

and

sudo pecl install mongodb

I have this php file:

<?php

echo "I am here";
$connection = new Mongo('localhost');
$db = $connection->mydb;

$list = $db->listCollections();
foreach ($list as $collection) {
    echo "$collection </br>";       
}

echo "I am never here";
?>

I cannot see the second echo.

I would appreciate any thoughts.

Thank you.

4
  • First echo phpinfo(); and see mongo extension is there or not? If not there then mongo is not installed successfully. Commented Aug 24, 2016 at 12:00
  • follow this stackoverflow.com/questions/11356834/mongodb-connect-using-php Commented Aug 24, 2016 at 12:03
  • @SanjayChaudhari Thank you very much! Indeed there is. There is a mongodb section with the info from php -i. Commented Aug 24, 2016 at 12:08
  • @KarthiVenture Thank you! I saw that post earlier. Tried everything there but to no avail. Commented Aug 24, 2016 at 12:14

2 Answers 2

0

First, check @sanjay comment and then apply this connection:

$db = new MongoClient('mongodb://localhost', [
    'username' => 'root',
    'password' => '',
    'db'       => 'YOUR DB'
]);

Hope it will work for you.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you! I tried but I only see the first echo. When I login to Mongo shell from the command line I don't use any username/password. So, for username and password I just put '' (blank).
I am wondering, could it be a problem that mongod is 3.2.8 whereas mongodb version (from php -i) is 1.1.8?
Which version you used ?
sorry for sounding a little bland, but I cannot be sure of that. Further, when I login to mongo shell I get this message: "Connecting to: test" which I dismiss :) I am quite confused.
Kindly check this answer stackoverflow.com/a/16769216/3081659. It's related to your current problem.
|
0

I managed to have it working. Thanks to this post: After upgrading PHP to version 7, why can't I use the mongodb driver?

and this post: Using the PHP Library for MongoDB (PHPLIB)

Sorry as it seems pretty odd (to me!) but indeed: MongoClient() has become (verbatim! with the slash!) MongoDB\Client().

Hence, the file I had in my original post becomes:

<?php
require 'vendor/autoload.php';
echo "I am here";

$manager = new MongoDB\Client();

$database = $manager->mydb;


foreach ($database->listCollections() as $databaseInfo) {
    var_dump($databaseInfo);
}
echo "I managed to arrive here";
?>

and I can see the contents of mydb as well as the 2 echos. The vendor/autoload.php was generated by

sudo composer require "mongodb/mongodb=^1.0.0"

as I ran it from within the directory of the php file above. Probably not the best idea as I, now, have to resolve how to make the autoload.php (and all it carries with) globally available. But, nonetheless, at least, I managed to arrive somewhere.

I am using: PHP 7.0.8-0ubuntu0.16.04.2 (cli) ( NTS ) under: Ubuntu 16.04.1 LTS

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.