Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

302
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!