我已经在服务器上安装了MongoDB现在怎么办?

问题描述

| 我对使用Nosql特别是MongoDB感兴趣。为此,我通过SSH进入服务器并运行以下命令,在外部Linux机器上安装了MongoDB:
sudo pecl install mongo
Mongo似乎已正确安装,如果我运行
PHPinfo()
,现在有一个\'Mongo \'部分。 但是现在呢?从这里开始在生产中使用它似乎有些麻烦。问题是,我不认为MongoDB服务正在运行,并且我还没有配置任何MongoDB用户,因为我不确定该怎么做。结果,以下测试脚本失败:
<?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\";
}

?>
当我收到以下错误消息:   致命错误:/ home / [用户名] /public_html/mongodbtest/index.PHP:4中堆栈错误:#0 / home / [用户名] /public_html/mongodbtest/index.PHP(4):Mongo-> __ construct()#1 {main}放在第4行的/home/woohoobi/public_html/mongodbtest/index.PHP中 如何从这里开始使用MongoDB?     

解决方法

        您从未真正安装过MongoDB,而是安装了PHP扩展,该扩展使您可以连接到MongoDB实例。通过下载MongoDB并将其安装在服务器上来安装MongoDB,然后从您的PHP脚本连接到它。