I've installed MongoDB on a server. What now?
I'm interesting in playing around with NoSQL and particularly MongoDB. To this end, I've installed MongoDB on an external Linux box by SSHing into the server and running the following command:
sudo pecl install mongo
Mongo seems to have installed correctly and there's now a 'Mongo' section if I run phpinfo()
.
But what now? I seem to be having trouble going from here to using it in production. The problem being, I don't think the MongoDB service is running, and I haven't configured any MongoDB users because I'm not sure how to. As a result, the following test script fails:
<?php
// connect
$m = new Mongo();
// select a database
$db = $m->comedy;
// select a collection (analogous to a relational database's table)
$collection = $db->cartoons;
// add a record
$obj = array( "title" => "Calvin and Hobbes", "author" => "Bill Watterson" );
$collection->insert($obj);
// add another record, with a different "shape"
$obj = array( "title" => "XKCD", "online" => true );
$collection->insert($obj);
// find everything in the collection
$cursor = $collection->find();
// iterate through the results
foreach ($cursor as $obj) {
echo $obj["title"] . "\n";
}
?>
As I get the following error message:
Fatal error: Uncaught exception 'MongoConnectionException' with message 'connecting to failed: Transport endpoint is not connected' in /home/[username]/public_html/mongodbtest/index.php:4 Stack trace: #0 /home/[username]/public_html/mongodbtest/index.php(4): Mongo->__construct() #1 {main} thrown in /home/woohoobi/public_html/mongodbtest/index.php on line 4
Ho开发者_如何学Cw can I get from here to using MongoDB?
You never actually installed MongoDB, but rather the PHP extension that lets you connect to a MongoDB instance. Install MongoDB by downloading it and installing it on your server, and then connect to it from your PHP script.
精彩评论