未捕获异常“MongoConnectionException” [英] Uncaught exception 'MongoConnectionException'

查看:382
本文介绍了未捕获异常“MongoConnectionException”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hi Mongo专家...

Hi Mongo experts...

我是一个想试用MongoDB的开发人员。因此,在测试机(Dell E520英特尔双核4GB Ram)中安装了centoOS 6.5 64位,安装了PHP(Apache已经存在)。

I am a developer wanting to try out MongoDB. So installed centoOS 6.5 64bit in a test machine (Dell E520 Intel Dual Core 4GB Ram), installed PHP (Apache was already present).

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

Then installed MongoDB (yum install mongo-10gen mongo-10gen-server), then installed "pecl install mongo" (install ok: channel://pecl.php.net/mongo-1.4.5), added extension=mongo.so to php.ini.

要安装pecl,我安装了几个其他东西,如C ++编译器&梨梨。 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

To install pecl, I installed few other things like C++ compiler & php-pear. php5-dev & php5-cli was not available in yum so installed php-devel & php-cli (installed versions are 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& connected没有问题

I turned off iptables firewall. Ran mongo --host localhost:27017 from shell & connected without problems

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

这些是test.php的内容

These are the contents of 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 = new MongoClient(mongodb:// localhost:27017);

As you can see I have tried both $m = new MongoClient(); & $m = new MongoClient("mongodb://localhost:27017");

但我得到相同的错误。

But I am getting the same error. Where am I going wrong?

推荐答案

此问题记录在http://ca3.php.net/manual/en/mongo.installation.php#mongo.installation.fedora

Red Hat,Fedora和CentOS:

Red Hat,Fedora and CentOS:

这些系统上的默认Apache设置不允许请求进行网络连接,当驱动程序尝试连接到数据库时将获得权限被拒绝错误。如果遇到这种情况,请尝试运行:

The default Apache settings on these systems do not let requests make network connections, meaning that the driver will get "Permission denied" errors when it tries to connect to the database. If you run into this, try running:

$ / usr / sbin / setsebool -P httpd_can_network_connect 1

$ /usr/sbin/setsebool -P httpd_can_network_connect 1

然后重新启动Apache。 (此问题也发生在SELinux。)

Then restart Apache. (This issue has also occurred with SELinux.)

感谢您的支持!

希望此主题有助于某人进入圈子!

Hope this thread helps someone from going in circles!

这篇关于未捕获异常“MongoConnectionException”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆