zoukankan      html  css  js  c++  java
  • mongodb连接失败原因排查

    安装了mongodb,加入了管理员root和test数据库的用户rex。而且开启的用户认证。

    依照说明文档连接mongodb数据库:$mongo = new Mongo("mongodb://rex:123456@localhost");

    结果开启firebug之后发现报错:"NetworkError: 500 Internal Server Error - http://192.168.202.132/montest.php"

    在这里尝试了非常多方法都无法解决。也不清楚为什么出了错。仅仅能再看一遍php的官方文档(http://php.net/manual/zh/mongoclient.construct.php)

    偶然看到这么一句:假设给出的主机都无法连接,将会抛出 MongoConnectionException 异常。

    于是我開始尝试打印这个异常:

    try {   
      $mongo = new Mongo("mongodb://rex:123456@localhost"); 
    } catch (Exception $e) {   
      print $e->getMessage();   
      exit();   
    } 

    输出结果是:Failed to connect to: localhost:27017: Authentication failed on database 'admin' with username 'rex': auth failed

    一看便知:rex是test数据库的用户。但是代码确实默认连接数据库admin,问题就出在这里!

    于是改成:$mongo = new Mongo("mongodb://rex:123456@localhost/test");

    问题解决!



  • 相关阅读:
    ansible命令应用示例
    ansible运维自动化工具
    grep 命令使用指南
    Nginx httpS server配置
    find 使用指南
    fastcgi_param 详解
    nginx虚拟主机的配置
    shell中字体变色
    nginx.conf 配置文件详解
    php-fpm.conf 配置文件详解
  • 原文地址:https://www.cnblogs.com/lxjshuju/p/7160756.html
Copyright © 2011-2022 走看看