PHP mongodb驱动程序检查连接 [英] PHP mongodb driver check connection

查看:199
本文介绍了PHP mongodb驱动程序检查连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用php驱动程序管理器检查与mongodb服务器的连接!我用谷歌搜索了很多次以获得$DriverManager->checkConnection()之类的方法或任何$DBmanager->connected之类的属性!

Trying to check connection with mongodb server using php driver manager! I googled many times to get the method like $DriverManager->checkConnection() or any property like $DBmanager->connected!

php var_dump MongoDB\Driver\Manager的当前输出

Current output from php var_dump MongoDB\Driver\Manager

object(MongoDB\Driver\Manager)#10 (2) {
  ["uri"]=>
  string(24) "mongodb://127.0.0.1:27017"
  ["cluster"]=>
  array(0) {
  }
}

我通过启动数据库服务器然后不运行服务器来检查! var_dump结果之间没有区别!

I checked by starting the database server and then without running the server! There is no difference between var_dump results!

有帮助吗?

推荐答案

MongoDB \ Driver \ Manager是扩展的主要入口点.它负责维护与MongoDB的连接(无论是独立服务器,副本集还是分片群集).

The MongoDB\Driver\Manager is the main entry point to the extension. It is responsible for maintaining connections to MongoDB (be it standalone server, replica set, or sharded cluster).

实例化Manager时未建立与MongoDB的连接.这意味着即使一台或多台MongoDB服务器已关闭,MongoDB \ Driver \ Manager始终可以构建.

No connection to MongoDB is made upon instantiating the Manager. This means the MongoDB\Driver\Manager can always be constructed, even though one or more MongoDB servers are down.

由于延迟创建了连接,因此任何写入或查询都可能引发连接异常.在脚本的生存期内,MongoDB服务器也可能不可用.因此,重要的是将Manager上的所有操作包装在try/catch语句中.

Any write or query can throw connection exceptions as connections are created lazily. A MongoDB server may also become unavailable during the life time of the script. It is therefore important that all actions on the Manager to be wrapped in try/catch statements.

final MongoDB\Driver\Manager {
/* Methods */
final public __construct ([ string $uri = "mongodb://127.0.0.1/" [, array $uriOptions = [] [, array $driverOptions = [] ]]] )
final public MongoDB\Driver\WriteResult executeBulkWrite ( string $namespace , MongoDB\Driver\BulkWrite $bulk [, MongoDB\Driver\WriteConcern $writeConcern ] )
final public MongoDB\Driver\Cursor executeCommand ( string $db , MongoDB\Driver\Command $command [, MongoDB\Driver\ReadPreference $readPreference ] )
final public MongoDB\Driver\Cursor executeQuery ( string $namespace , MongoDB\Driver\Query $query [, MongoDB\Driver\ReadPreference $readPreference ] )
final public MongoDB\Driver\ReadConcern getReadConcern ( void )
final public MongoDB\Driver\ReadPreference getReadPreference ( void )
final public array getServers ( void )
final public MongoDB\Driver\WriteConcern getWriteConcern ( void )
final public MongoDB\Driver\Server selectServer ( MongoDB\Driver\ReadPreference $readPreference )
}

var_dump()设置MongoDB \ Driver \ Manager将打印出有关该管理器的各种详细信息,而这些信息通常不会公开.这对于调试驱动程序如何查看您的MongoDB设置以及使用哪些选项很有用.

var_dump()ing a MongoDB\Driver\Manager will print out various details about the manager that are otherwise not normally exposed. This can be useful to debug how the driver views your MongoDB setup, and which options are used.

<?php $manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
var_dump($manager); ?>

参考资料:链接

这篇关于PHP mongodb驱动程序检查连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆