Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

303
Vistas
MongoDB PHP Driver Fails to Connect and Authenticate

I have set up for the first time the authentication for MongoDB. I have two users: 'admin', (set as root in the 'admin' database) and 'testUser' which is set up as 'dbAdmin' in the 'testDatabase'.

When I use mongo shell to login using the following command everything works:

mongo -u testUser -p abcd1234 --authenticationDatabase testDatabase

On the PHP's end, I have the following code:

<?php
class DBConnection {
    const HOST = '1.1.1.1';
    const PORT = 27017;
    const DBNAME = 'testDatabase';
    const USERNAME = 'testUser';
    const PASSWORD = 'abcd1234';
    private static $instance;
    public $connection;
    public $database;

    private function __construct() {

        if (!extension_loaded('mongo')) die("MongoDB is not installed!");
        try {
            $this->connection = new MongoClient('mongodb://'.self::HOST.':'.self::PORT.'/'.self::DBNAME, array('username' => self::USERNAME, 'password' => self::PASSWORD));
            $this->database = $this->connection->selectDB(self::DBNAME);
        } catch (MongoConnectionException $e) {
            throw $e;
        }
    }

    static public function instantiate() {
        if (!isset(self::$instance)) {
            $class = __CLASS__;
            self:: $instance = new $class;
        }
        return self::$instance;
    }

    public function getCollection($name) {
        return $this->database->selectCollection($name);
    }

    public function execute($code) {
        return $this->database->execute($code);
    }
}
?>

Naturally, host (as well as the db name, username and password) are obfuscated. I have verified multiple time that there is not a typo in the credentials. I have also verified that I can connect to the database from a remote shell, similar to how this script connects.

Still, I always get this error:

PHP Fatal error: Uncaught exception 'MongoConnectionException' with message 'Failed to connect to: 1.1.1.1:27017: Authentication failed on database 'testDatabase' with username 'testUser': auth failed' in /var/www/html/wip/include/mongoConnect.php:17 Stack trace: 0 /var/www/html/wip/include/mongoConnect.php(17): MongoClient->__construct('mongodb://1.1...', Array) 1 /var/www/html/wip/include/mongoConnect.php(27): DBConnection->__construct() 2 /var/www/html/wip/migration/migrate.php(85): DBConnection::instantiate() 3 {main} thrown in /var/www/html/wip/include/mongoConnect.php on line 17

Credentials for both users are in SCRAM-SHA-1. Any idea what is causing this connection issue?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can try to change the way of logging in

$m = new MongoClient("mongodb://${username}:${password}@localhost");

Take a look at here:
http://php.net/manual/en/mongo.connecting.auth.php

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda