【问题标题】:mongoDB php doesn't find database but is successfully connected to the servermongoDB php 找不到数据库但已成功连接到服务器
【发布时间】:2021-04-19 20:14:19
【问题描述】:

当我使用 PHP 连接到我的 mongoDB 数据库时,该变量有时包含一个空数组,但在 ping 它时,该数组包含数据。当我刚刚重新启动 apache2 网络服务器时,会出现空数组。我想还有其他一些方法可以导致它,但我找不到如何做到这一点的方法。

这不是我唯一的问题。当我选择一个数据库时,变量包含 NULL 所以我不能选择一个集合。也很难找到最新的文档,因为 these two 已经过时了。 现在到我所有的代码:

$mongo = new MongoDB\Driver\Manager("mongodb://root:pwd@localhost:27017");
var_dump($mongo);

有时返回:

object(MongoDB\Driver\Manager)#1 (2) { ["uri"]=> string(47) "mongodb://root:pwd@localhost:27017" ["cluster"]=> array(0) { } }

在打印数据库之前向数据库发送 ping:

$mongo = new MongoDB\Driver\Manager("mongodb://root:pwd@localhost:27017");
$command = new MongoDB\Driver\Command(['ping' => 1]);
$mongo->executeCommand('db', $command);
var_dump($mongo);

返回:

 object(MongoDB\Driver\Manager)#1 (2) { ["uri"]=> string(47) "mongodb://root:pwd@localhost:27017" ["cluster"]=> array(1) { [0]=> array(10) { ["host"]=> string(9) "localhost" ["port"]=> int(27017) ["type"]=> int(1) ["is_primary"]=> bool(false) ["is_secondary"]=> bool(false) ["is_arbiter"]=> bool(false) ["is_hidden"]=> bool(false) ["is_passive"]=> bool(false) ["last_is_master"]=> array(10) { ["ismaster"]=> bool(true) ["maxBsonObjectSize"]=> int(16777216) ["maxMessageSizeBytes"]=> int(48000000) ["maxWriteBatchSize"]=> int(100000) ["localTime"]=> object(MongoDB\BSON\UTCDateTime)#2 (1) { ["milliseconds"]=> string(13) "1618843900377" } ["logicalSessionTimeoutMinutes"]=> int(30) ["minWireVersion"]=> int(0) ["maxWireVersion"]=> int(6) ["readOnly"]=> bool(false) ["ok"]=> float(1) } ["round_trip_time"]=> int(0) } } }

现在我没有找到数据库:

$mongo = new MongoDB\Driver\Manager("mongodb://root:pwd@localhost:27017");
//Ping the database
$command = new MongoDB\Driver\Command(['ping' => 1]);
$mongo->executeCommand('db', $command);
//select the hltv database
$db = $mongo->hltv;
var_dump($db);

返回:

NULL

错误日志显示:

PHP Warning:  Undefined property: MongoDB\\Driver\\Manager::$hltv in /var/www/hltv/index.php on line 6

尝试列出所有数据库时:

$mongo = new MongoDB\Driver\Manager("mongodb://root:pwd@localhost:27017");
//Ping the database
$command = new MongoDB\Driver\Command(['ping' => 1]);
$mongo->executeCommand('db', $command);
//list databases
var_dump($mongo->listDatabases());

返回一个空白屏幕并显示以下错误:

PHP Fatal error:  Uncaught Error: Call to undefined method MongoDB\\Driver\\Manager::listDatabases() in /var/www/hltv/index.php:6\nStack trace:\n#0 {main}\n  thrown in /var/www/hltv/index.php on line 6

此时选择一个集合是没有意义的。 我确保用户 root 具有读写访问权限,并且使用 MongoDb Compass 访问数据库工作正常,并显示所有数据库/集合并让我读/写数据。

我的设置:

操作系统:Ubuntu 18.04

MongoDB 版本:3.6.3

PHP版本:8.0.3

MongoDB PHP 驱动版本:1.9.1

Apache2 用于运行 PHP 代码。

【问题讨论】:

  • 也许尝试列出数据库并查看是否出现?:(docs.mongodb.com/php-library/v1.5/reference/method/…)另外,我认为“ping”差异可能不是问题,因为连接可能只是延迟初始化。
  • list Databases 返回未定义的方法我更新了问题以包含更多详细信息。

标签: php mongodb php-mongodb


【解决方案1】:

现在我发现有一个驱动程序和一个库,所以在安装库之后一切似乎都可以正常工作了。我不知道应用程序被分成了两部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-16
    • 2017-09-17
    • 2012-08-15
    • 2014-09-29
    相关资源
    最近更新 更多