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());
?>
Error:
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:
Well...
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();
}
It isn't corrupted, because if it was corrupted, I won't be able to connect it via Java (right?)
I already tried connecting to 127.0.0.1 and localhost, same issue.
I already tried restarting httpd and mongod, same issue.
I already tried binding the MongoDB server to 0.0.0.0 instead of 127.0.0.1, same issue.
Yes, MongoDB drivers are installed, they are active in PHP Info.
MongoDB PHP version: 1.2.1
MongoDB shell version: 3.2.10 (now updated to 3.4.0)
PHP version: 7.0.13
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
You need to use
setsebool -P httpd_can_network_connect on
to fix the issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With