0

Timeout while connecting to MongoDB database with mongo php ext under Ubuntu 12.04 and php 5.6

I'm using

  • latest Apache 2.4
  • Ubuntu 12.04
  • php 5.6 with mongo ext Version 1.6.14
  • MongoDB v3.4 running in an lxc container

And the problem is when I connect to the mongo database.

I'm using this connection string:

mongodb://myDbUser:passW0rT@0.0.0.0:27020/myDb

(password, database and addr have been changed).

I have verified that php mongo ext is installed, with this code:

echo extension_loaded("mongo") ? "loaded\n" : "not loaded\n"; //prints loaded.
if (class_exists('\MongoClient')) {
   echo 'is Mongo Client'."\n";//this is printed
}
else {
   echo 'no Mongo Client'."\n";
}


try {
    $client = new MongoClient('mongodb://myDbUser:passW0rT@0.0.0.0:27020/myDb', []);
} catch (Exception $ex) {
    echo $ex->getMessage();//prints Failed to connect to: 0.0.0.0:27020: Connection timed out 
}

What is more strange I can connect to this database using MongoClient and in addition to this I can connect to this database by my local server running on my computer (Win 10).

The problem appears also when I use security.authorization set to enabled and without difference set to disabled – then I use dsn mongodb://0.0.0.0:27020 Unfortunately server's response always gives timeout error when I enter web addr using www (domain which is on the server where mongo database is installed).

It could be one clue that server where mongo is installed is server which runs as container which means that I have to add port number to connect eg. via ssh.

I even changed the server from ngix to apache and problem didn't disappear.

Vince Bowdren
  • 8,326
  • 3
  • 31
  • 56
Marek G.
  • 99
  • 9
  • The way you are using _0.0.0.0_ looks wrong; are you trying to connect to a mongodb process on the local machine? If so, you want to use _127.0.0.1_ I think. – Vince Bowdren Mar 21 '17 at 12:22
  • Yes, to the local machine but this machine works as a container not as machine with normal ip access. I need also give the port to connect. – Marek G. Mar 21 '17 at 12:32
  • But I have checked again if it works with 127.0.0.1:27020 but behaviour is slightly different. Now is without lag and response is immediate. And message is: Failed to connect to: 127.0.0.1:27020: Connection refused Container is of type lxc should I add. A lot of thanks for your response! – Marek G. Mar 21 '17 at 12:36
  • It sounds like you need to check the lxc port configuration perhaps? – Vince Bowdren Mar 21 '17 at 13:12
  • Thanks for help! One clarification I used 0.0.0.0 to hide real IP addr. @Vince you can simply add your previous comment as an answer to this question. The solution is not to use 127.0.0.1:27020 (27020 is port of mongoDB) but simply 127.0.0.1 I thought earlier that I must add port because it is not default like :80 Then I thought, Ok once I live and I removed the port. Refresh and what? It works! I am so glad for your help. So write an answer, I will mark it as a solution for this question. Thank you once again! Solution: mongodb://myDbUser:passW0rT@127.0.0.1/myDb – Marek G. Mar 21 '17 at 14:04
  • Well, it sounds like you fixed it for yourself; so the right way to resolve the question is if _you_ post your solution as an answer, then you will be able to accept it. – Vince Bowdren Mar 21 '17 at 14:06

0 Answers0