Martyn
Martyn

Reputation: 6383

"Fatal error: Uncaught exception 'MongoConnectionException' with message 'Failed to connect to: /tmp/mongodb-27017.sock:0: Connection refused''"

I have the following code:

$this->connection = new MongoClient($uri, $options);
$this->database = $database;

However, at line $this->connection I get:

Fatal error: Uncaught exception 'MongoConnectionException' with message 'Failed to connect to: /tmp/mongodb-27017.sock:0: Connection refused' in...

If I output the following:

var_dump($uri, $options); exit;

I get:

string(33) "mongodb:///tmp/mongodb-27017.sock" array(1) { ["connect"]=> bool(true) } 

This is in my dev environment, I'm not using any username/password for Mongo as I can login to the Mongo shell with mongo reputation alone. From there I can view collection and documents.

Originally I was getting Permissions denied, but I changed the permissions of my dev /tmp/mongodb-27017.sock to 777. Now I get "connection refused".

Anyway below is the file from my ll /tmp/ dir:

srwxrwxrwx  1 mongodb mongodb     0 Jan 28 07:47 mongodb-27017.sock=

Anyone know what the issue is, or how I can debug further? Thanks

Upvotes: 1

Views: 2573

Answers (2)

JayminLimbachiya
JayminLimbachiya

Reputation: 1006

$connection=  new \MongoClient("mongodb://localhost:27017");
return $this->db = $connection->selectDB($config['dbname']);

try this to connect local mongoDB.

Upvotes: 0

Ray
Ray

Reputation: 41428

I would recommend in PHP connecting not to the socket which your process might not have permissions to access. If you modify the permission of the socket it's only temporary. When you stop the mongo db it will delete the socket, the next time you start it, the permissions will be back to whatever the default is (700 I think).

Instead connect to the to the default port (27017) mongo opens on 127.0.0.1.

Change you connection string in PHP to the following:

$uri = "mongodb://127.0.0.1";
$this->connection = new MongoClient($uri, $options);

Upvotes: 1

Related Questions