未捕获的异常'MongoConnectionException'

时间:2014-01-06 16:51:37

标签: mongodb exception connection

Fatal error: Uncaught exception 'MongoConnectionException' with message 
'Failed to connect to: localhost:27017: Permission denied' in 
/var/www/html/test.php:8 Stack trace: #0 /var/www/html/test.php(8): 
MongoClient->__construct() #1 {main} thrown in /var/www/html/test.php 
on line 8

嗨Mongo专家......

我是一名想要试用MongoDB的开发人员。所以在一台测试机(Dell E520 Intel双核4GB Ram)上安装了centoOS 6.5 64bit,安装了PHP(Apache已经存在)。

然后安装MongoDB(yum install mongo-10gen mongo-10gen-server),然后安装" pecl install mongo" (安装ok:channel://pecl.php.net/mongo-1.4.5),将extension = mongo.so添加到php.ini。

要安装pecl,我安装了一些其他东西,比如C ++编译器和放大器。 PHP梨。 php5-dev& php5-cli在yum中不可用所以安装了php-devel& php-cli(已安装的版本为php-devel-5.3.3-27.el6_5.x86_64& php-cli-5.3.3-27.el6_5.x86_64

我关闭了iptables防火墙。 Ran mongo --host localhost:27017 from shell&连接没有问题

[root@localhost ~]# mongo --host localhost:27017
MongoDB shell version: 2.4.8
connecting to: localhost:27017/test
> 

这些是test.php的内容

<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

// connect
$m = new MongoClient();
//$m = new MongoClient("mongodb://localhost:27017");

// select a database
$db = $m->comedy;

// select a collection (analogous to a relational database's table)
$collection = $db->cartoons;

// add a record
$document = array( "title" => "Calvin and Hobbes", "author" => "Bill Watterson" );
$collection->insert($document);

// add another record, with a different "shape"
$document = array( "title" => "XKCD", "online" => true );
$collection->insert($document);

// find everything in the collection
$cursor = $collection->find();

// iterate through the results
foreach ($cursor as $document) {
    echo $document["title"] . "\n";
}
?>

正如你所看到的,我已经尝试了两种方法 $ m = new MongoClient(); &安培; $ m =新的MongoClient(&#34; mongodb:// localhost:27017&#34;);

但是我得到了同样的错误。我哪里错了?

1 个答案:

答案 0 :(得分:10)

http://ca3.php.net/manual/en/mongo.installation.php#mongo.installation.fedora

记录了此问题

Red Hat,Fedora和CentOS:

这些系统上的默认Apache设置不允许请求建立网络连接,这意味着驱动程序在尝试连接到数据库时将收到“权限被拒绝”错误。如果遇到这种情况,请尝试运行:

$ / usr / sbin / setsebool -P httpd_can_network_connect 1

然后重启Apache。 (SELinux也出现了这个问题。)

感谢您的支持!

希望这个帖子可以帮助某人进入圈子!