未捕获的异常'MongoConnectionException'

Jos*_*shG 2 connection exception mongodb

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
Run Code Online (Sandbox Code Playgroud)

嗨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-pear.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来自shell并连接没有问题

[root@localhost ~]# mongo --host localhost:27017
MongoDB shell version: 2.4.8
connecting to: localhost:27017/test
> 
Run Code Online (Sandbox Code Playgroud)

这些是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";
}
?>
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我已经尝试了两个$ m = new MongoClient(); &$ m = new MongoClient("mongodb:// localhost:27017");

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

Jos*_*shG 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也出现了这个问题.)

感谢您的支持!

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