Maxim Shoustin
Maxim Shoustin

Reputation: 77904

PHP fails to create connection to mongoDB

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

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?

Upvotes: 2

Views: 13962

Answers (4)

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

Upvotes: 0

dev_monkey_
dev_monkey_

Reputation: 11

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

Upvotes: 0

Perry
Perry

Reputation: 11700

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));

Upvotes: 3

Dustin Klein
Dustin Klein

Reputation: 319

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]]

Upvotes: 0

Related Questions