MongoDB PHP未捕获MongoDB \ Driver \ Exception \ ConnectionTimeoutException:未找到合适的服务器 [英] MongoDB PHP Uncaught MongoDB\Driver\Exception\ConnectionTimeoutException: No suitable servers found

查看:693
本文介绍了MongoDB PHP未捕获MongoDB \ Driver \ Exception \ ConnectionTimeoutException:未找到合适的服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我陷入了这个奇怪的问题.

I'm stuck with this strange problem.

<?php
require_once __DIR__ . "/vendor/autoload.php";

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// connect to mongodb
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");

$command = new MongoDB\Driver\Command(array("ping" => 1));
$result = $manager->executeCommand("test", $command);

var_dump($result, $result->toArray());
?>

错误:

Fatal error: Uncaught MongoDB\Driver\Exception\ConnectionTimeoutException: No suitable servers found (`serverSelectionTryOnce` set): [connection refused calling ismaster on 'localhost:27017'] in /var/www/html/mongo.php:11 Stack trace: #0 /var/www/html/mongo.php(11): MongoDB\Driver\Manager->executeCommand('test', Object(MongoDB\Driver\Command)) #1 {main} thrown in /var/www/html/mongo.php on line 11

为什么奇怪?好吧,我已经尝试过在互联网上搜索这个问题,人们总是说同样的话:

Why it is strange? Well, I already tried searching around the internet about this problem, people always say the same thing:

  1. MongoDB服务器处于脱机状态
  2. MongoDB服务器数据已损坏

好吧...

它不脱机,我的Java应用程序(和"mongo")可以很好地连接到MongoDB服务器,并且可以与数据库进行交互,而不会引发ConnectionTimeoutException.

It isn't offline, my Java applications (and "mongo") can connect to the MongoDB server just fine and can interact with the database without throwing the ConnectionTimeoutException.

try {
    mongoClient = new MongoClient( "localhost" );
} catch (UnknownHostException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

它没有损坏,因为如果它损坏了,我将无法通过Java连接它(对吗?)

It isn't corrupted, because if it was corrupted, I won't be able to connect it via Java (right?)

我已经尝试连接到127.0.0.1和localhost,同样的问题.

I already tried connecting to 127.0.0.1 and localhost, same issue.

我已经尝试过重启httpd和mongod,同样的问题.

I already tried restarting httpd and mongod, same issue.

我已经尝试将MongoDB服务器绑定到0.0.0.0,而不是127.0.0.1,这是同样的问题.

I already tried binding the MongoDB server to 0.0.0.0 instead of 127.0.0.1, same issue.

是的,已安装MongoDB驱动程序,它们在PHP信息中处于活动状态.

Yes, MongoDB drivers are installed, they are active in PHP Info.

MongoDB PHP版本:1.2.1

MongoDB PHP version: 1.2.1

MongoDB Shell版本:3.2.10(现在更新为3.4.0)

MongoDB shell version: 3.2.10 (now updated to 3.4.0)

PHP版本:7.0.13

PHP version: 7.0.13

推荐答案

好吧,我想我应该再说一遍解决问题的方法(感谢@jmikola!)

Okay, I think I should come back to say how I fixed the issue (thanks @jmikola!)

https://github.com/mongodb/mongo-php-driver /issues/484

您需要使用

setsebool -P httpd_can_network_connect on

解决此问题.

这篇关于MongoDB PHP未捕获MongoDB \ Driver \ Exception \ ConnectionTimeoutException:未找到合适的服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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