【问题标题】:MongoDB PHP define database connectionMongoDB PHP 定义数据库连接
【发布时间】:2016-07-18 12:28:17
【问题描述】:

我正在使用这种方法连接到远程数据库: (SSH隧道在后台运行)

$mongo = new Mongo('mongodb://[root:password]@localhost:27018');

但是我得到这样的错误:

致命错误:未捕获的异常“MongoConnectionException”与 消息'无法连接到:localhost:27018:SASL 身份验证 数据库“管理员”失败:身份验证失败。在 /var/www/html/mongo_seed/index.php:22 堆栈跟踪:#0 /var/www/html/mongo_seed/index.php(22): Mongo->__construct('mongodb://[root...') #1 {main} 抛出 /var/www/html/mongo_seed/index.php 第 22 行

如何在参数字符串中定义要连接的数据库?否则我该如何一个接一个地完成整个事情?

【问题讨论】:

    标签: php mongodb


    【解决方案1】:

    MongoDB 规范是这样说的:

    mongodb://[用户名:密码@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[数据库][?options]]

    https://docs.mongodb.com/manual/reference/connection-string/

    我认为你只需要添加 /databasename最后?

    【讨论】:

    • 能否提供引用文本的链接?
    【解决方案2】:

    $connection = new MongoClient( "mongodb://[root:password]@localhost:27018" );

    使用这个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-25
      • 1970-01-01
      • 2012-02-19
      • 2022-09-24
      • 2014-04-20
      • 2022-07-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多