【发布时间】:2016-06-05 22:32:04
【问题描述】:
我正在尝试连接到 mongodb。使用 openshift 托管。您可以在页脚中看到代码。我尝试输入用户名,在 $env 之前传递,在 $env 之后传递 db_name,但没有任何反应。我从网络服务器收到 500 错误。 有人可以告诉我出了什么问题以及如何解决吗? 我认为rockmongo 工作正常,所以我的代码出错了。我尝试了很多组合
<?php
// connect to MongoDB
$m = new MongoClient("mongodb://$OPENSHIFT_MONGODB_DB_HOST:$OPENSHIFT_MONGODB_DB_PORT");
// select a database
$db = $m->comedy;
// select a collection
$collection = $db->cartoons;
$document = array(
"title" => "Hibernate and MongoDB",
"author" => "Anghel Leonard"
);
//insert a new document
$collection->insert($document);
//returns a cursor for the search results
$cursor = $collection->find();
// iterate through the results
foreach ($cursor as $document) {
echo '"_id": '.$document["_id"]."<br />";
echo '"title": '.$document["title"]."<br />";
echo '"author": '.$document["author"]."<br />";
echo '*********************************';
}
?>
【问题讨论】:
-
那么错误是什么?我能发现的唯一问题是连接字符串中没有
username:password@.. -
另外,请确保连接到您的数据库的用户确实经过身份验证才能访问它(您可以使用 RockMongo 轻松检查:数据库 -> 更多 -> 身份验证)。