Reputation: 742
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:[email protected]: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.
Upvotes: 0
Views: 5621
Reputation: 2104
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:
<?php
MongoLog::setLevel(MongoLog::ALL);
MongoLog::setModule(MongoLog::ALL);
?>
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
Notice: PARSE INFO: Parsing mongodb://theAdmin:Gold1234@localhost:27027 in Command line code on line 1
Notice: PARSE INFO: - Found user 'theAdmin' and a password in Command line code on line 1
Notice: PARSE INFO: - Found node: localhost:27027 in Command line code on line 1
Notice: PARSE INFO: - Connection type: STANDALONE in Command line code on line 1
Notice: PARSE INFO: - No database name found for an authenticated connection. Using 'admin' as default database in Command line code on line 1
Notice: CON INFO: mongo_get_read_write_connection: finding a STANDALONE connection in Command line code on line 1
Notice: CON INFO: connection_create: creating new connection for localhost:27027 in Command line code on line 1
Notice: CON WARN: connection_create: error while creating connection for localhost:27027: Invalid argument in Command line code on line 1
Notice: CON WARN: Couldn't connect to 'localhost:27027': Invalid argument in Command line code on line 1
I suspect a firewall issue at either end.. Can you connect to the server using the mongo shell?
Upvotes: 1