Deployed a quick cloud server just to host a MongoDB to tinker with it out of curiosity. It’s installed and works. Made a test DB/Table like this:
db.items.insert({ name: 'eggs', quantity: 10, price: 1.50 })
db.items.insert({ name: 'bacon', quantity: 3, price: 3.50 })
db.items.insert({ name: 'tomatoes', quantity: 30, price: 0.50 })
When I run db.items.find({}) all the items appear and all is well.
Now in PHP when I connect to that database from a different server I do this:
// open connection to MongoDB server
$conn = new Mongo('mongodb://theAdmin:Gold1234@165.225.130.252:27017');
// access database
$db = $conn->test;
// access collection
$collection = $db->items;
// execute query
// retrieve all documents
$cursor = $collection->find();
// iterate through the result set
// print each document
echo $cursor->count() . ' document(s) found. <br/>';
foreach ($cursor as $obj) {
echo 'Name: ' . $obj['name'] . '<br/>';
echo 'Quantity: ' . $obj['quantity'] . '<br/>';
echo 'Price: ' . $obj['price'] . '<br/>';
echo '<br/>';
}
and I get this error:
Fatal error: Uncaught exception ‘MongoConnectionException’ with
message ‘Failed to connect to: 165.225.130.252:27017: Transport
endpoint is not connected’ in /home/moosex/public_html/info.php:4
Stack trace: #0 /home/moosex/public_html/info.php(4):
Mongo->__construct(‘mongodb://[theA…’) #1 {main} thrown in
/home/moosex/public_html/info.php on line 4
I’ve looked up and tried several different ways to connect and still can’t get it. How am I supposed to connect remotely?
BTW, that is the actual username password and address to that server(there’s nothing on there except for eggs bacon and tomatoes), if you can connect to it, god bless you lol.
To be able to debug “random weirdness” like this, it is very useful to turn on the internal driver logging.
The driver does whole lot of things behind the scenes, and can spit out all sort of important debug information.
Add the following at the top of your script:
By default the logger will spew out “php error messages” (E_NOTICE/E_WARNING), if you have error_log enabled, make sure to check that file for the results.
For your (slightly modified) connection string, I get the following results
I suspect a firewall issue at either end.. Can you connect to the server using the mongo shell?