解决php连接MONGODB出现的”Permission denied”

装完MONGODB后,linux下能用命令操作,用php连接老是出现"Permission denied",费劲周折终于找到是SELINUX的问题。执行以下命令即可解决。

# setsebool httpd_can_network_connect 1 # setsebool httpd_can_network_connect_db 1

参考文章:

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

 

http://ulaptech.blogspot.com/2011/01/fatal-error-uncaught-exception.html

发表评论