2

I try to figure out why PHP driver can't connect to mongoDB

  • PHP Version 5.3.16 (64bit)
  • MongoDB: mongodb-linux-x86_64-2.4.3
  • OS: CentOS release 5.2 (Final)

  • Added link: mongo -> mongodb-linux-i686-2.4.3

  • Created data folder: mkdir /home/max/mongo/data

  • initiated mongo:

mongo/bin/mongod --dbpath=mongo/data --fork --logpath /var/wefi/logs/feederliteRC/mongodb.log

All works fine and can connect with mongoVUE monitor tool. (from Windows7)

Now, I try to connect to BongoDB from PHP:

I installed driver for PHP:

sudo pecl install mongo

on: php -i | grep mongo I get:

mongo
mongo.allow_empty_keys => 0 => 0
mongo.chunk_size => 262144 => 262144
mongo.cmd => $ => $
mongo.default_host => localhost => localhost
mongo.default_port => 27017 => 27017
mongo.is_master_interval => 15 => 15
mongo.long_as_object => 0 => 0
mongo.native_long => 0 => 0
mongo.ping_interval => 5 => 5
OLDPWD => /usr/share/pear/doc/mongo
_SERVER["OLDPWD"] => /usr/share/pear/doc/mongo

I added to php.ini (nano /etc/php.ini): extension=mongo.so

and restarted httpd: /etc/init.d/httpd restart

from code:

 try {
        // open connection to MongoDB server
        $conn = new Mongo('localhost');
} catch (MongoConnectionException $e) {            
        die('Error connecting to MongoDB server');
        } catch (MongoException $e) {           
        die('Error: ' . $e->getMessage());
    }

PHP sees new Mongo but I get exception: "Error connecting to MongoDB server.

Strange, mongoDB runs, driver for PHP works but it doesn't see mongoDB.

Can someone help me,

[EDIT]

"Error connecting to MongoDB server Failed to connect to: localhost:27017: Connection refused"

Do I need add smoething to php.ini?

2
  • 1
    Try adding $e->getMessage() to your output of the first catch block (the one you are hitting), you might know what the problem is. Commented May 27, 2013 at 8:29
  • Your connection string is wrong, it should be mongodb://localhost try that see if it makes an improvement Commented May 27, 2013 at 8:47

4 Answers 4

3

I think your connection should be something like this:

$conn = new Mongo("mongodb://localhost");

Also you can better use MongoClient because Mongo is DEPRECATED as of version 1.3.0.

$m = new MongoClient("mongodb://localhost", array("username" => $username, "password" => $password));

See the php manual for some more information: http://nl.php.net/manual/en/mongo.connecting.auth.php

EDIT If localhost won't work use IP address instead (thanks to Maxim Shoustin)

For Mongo it will be:

$conn = new Mongo("mongodb://127.0.0.1");

or if you use MongoClient it will be this:

$m = new MongoClient("mongodb://127.0.0.1", array("username" => $username, "password" => $password));
Sign up to request clarification or add additional context in comments.

5 Comments

Mongo() is still valid for use, that is not the problem
@Sammaye as I said it is deprecated. I don't say you can't use it anymore. The same is with mysql you still can use it but you should not do that because it is deprecated. The best is to use the last methods instead of the old ones.
It is deprecated but it is still valid, your answer doesn't solve the problem edit: Ok I actually see you modified the connection string, didn't see that initially
@Perry I have create user by this command db.createUser( { user: "root", pwd: "12345", roles: [{ role: "userAdmin", db: "sangi" }] }); and user is created but when I am using with PHP MongoClient with this codes new MongoClient("mongodb://127.0.0.1",array("username" => 'root', "password" => '12345','db'=>'sangi')); facing same error.
Can you connect to your mongodb instance by cli? Also take a look at php.net/manual/en/mongodb-driver-manager.construct.php since MongoClient is deprecated
0

I found a step by step guide, but in my opinion you are doing it right. Maybe this can help you. In this case he does not use a connection parameter at all, although I am pretty sure it should be an IP or a connection string linke this:

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

Comments

0

For any future people like me that got very confused when starting with MongoDB in 2016. The previously mentioned connection methods are now deprecated.

To connect to a MongoDB with versions such as 3.2, you will need:

$mongo = new Mongo\Driver\Manager('mongodb://localhost'); // if you have MongoDB PHP drivers and no Library
//Alternatively (recommended)
$mongo = new Mongo\Client; // for default local connection - MongoDB library required

Comments

0

If you are running SELinux and command line connections work but PHP connections fail try the command below.

/usr/sbin/setsebool -P httpd_can_network_connect 1

Then restart Apache.

See https://www.php.net/manual/en/mongo.installation.php

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.