Saturday, January 22, 2011

Fatal error: Uncaught exception 'MongoConnectionException' with message 'connecting to mongodb://localhost:27017 failed: Permission denied'

You've been working with MongoDB in the CLI for so long and then you suddenly require your work taken to the web. But then BAAAAM!, you encounter this nasty little fatal error.
Fatal error: Uncaught exception 'MongoConnectionException' with message 
'connecting to mongodb://localhost:27017 failed: Permission denied'
Could be even worse if you are blanket catching exceptions, even with E_STRICT and all without properly returning the exceptions could take you some work to figure out what's wrong. I encountered this issue while installing PHPMoAdmin and I keep getting the "Cannot Connect To Database" message. So I wrote this article with the intention of showing not just the fix but the root cause as well.
So what's wrong?

SELINUX (Secured Linux) is turned on and the web server application does not have permission to connect to a network or database directly. Is a SELINUX a bad thing? Of course not! It is simply there to provide a standard for application-level security unlike the current ACL of Linux systems. You just have to determine if it is right for you. If not, simply turn it off and it should solve most of your permission problems.

So how do we solve the problem without going through the touble of turning SELINUX off?

Just type the following and press enter. You might need to restart the mongod instance(s) / server.

# setsebool httpd_can_network_connect 1 # setsebool httpd_can_network_connect_db 1

Some information regarding real people encountering this problem can be found here

This is also related to this post
  • Related Links Widget for Blogspot

5 comments:

LG said...

Thanks a lot.
I had error "Permission denied" in CentOS with mongo and php, and the command
# setsebool httpd_can_network_connect 1
with restarting apache and mongo did the trick.

Jitesh Rathod said...

many many thanks a lot.............

Oleg said...

Thank you a lot!

Gorod said...

Thanks!

patrickbarker said...

Ballin, thank you so much