1

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

4
  • What is the permissions and ownership of your /tmp directory? Commented Jan 27, 2016 at 23:23
  • Hi. The tmp dir is drwxrwxrwt 7 root root 20480 Jan 28 08:17 tmp/ Commented Jan 27, 2016 at 23:24
  • Does it work if you leave off the $options and just use $this->connection = new MongoClient($uri); ? Commented Jan 27, 2016 at 23:35
  • Also, when you restart mongo, the permissions you've changed will be reverted back to 700 when it recreates the socket. Commented Jan 27, 2016 at 23:42

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

Comments

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

try this to connect local mongoDB.

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.