While uninstalling Watchdog module:

MongoConnectionException: Failed to connect to: localhost:27017: Authentication failed on database 'admin' with username '[mongouser]': auth fails in Mongo->__construct() (line 34 of [path]/mongodb/mongodb.module).


Alexander Matveev’s picture

In mongodb.module changed this:

$mongo = new Mongo($host, $options);

to this:

$mongo = new Mongo($host . '/' . $db, $options);

And it works.

tloudon’s picture

Here's the full function:

function mongodb($alias = 'default') {
  static $mongo_objects;
  $connections = variable_get('mongodb_connections', array());
  if (!isset($connections[$alias])) {
    $alias = 'default';
  $connection = isset($connections[$alias]) ? $connections[$alias] : array();
  $connection += array('host' => 'localhost', 'db' => 'drupal', 'connection_options' => array());
  $host = $connection['host'];
  $options = $connection['connection_options'] +  array('connect' => TRUE);
  $db = $connection['db'];
  if (!isset($mongo_objects[$host][$db])) {
    try {
      $mongo = new Mongo($host, $options);
      if (!empty($connection['slave_ok'])) {
      $mongo_objects[$host][$db] = $mongo->selectDB($db);
      $mongo_objects[$host][$db]->connection = $mongo;

That line $mongo_objects[$host][$db] = $mongo->selectDB($db); should set the database and it should default to 'drupal' if no 'db' element is defined in settings.php.

I don't have any issues connecting to my mongo instance and I have auth enabled in the mongo.conf and set up in settings.php. Also used same code w/o a user. Outside of Drupal, if I don't specify a user or db prior to starting the mongo shell client, I can use "db.auth('user', 'pass)" and "use mydatabase". So I don't think the Mongo server restricts access prior to connection or requires user/pass/db/etc either.

What kind of config did you set up for authorization? Is that all working? My guess is that it's a config issue, but if you give more info and replication steps I can check things out.

protools’s picture

from #1 work for me too

ikhlasstudio’s picture

You can try with drupal database 's user instead admin database 's user

bigjim’s picture

The issue is before $mongo_objects[$host][$db] = $mongo->selectDB($db); is run the php driver attempts to log into the admin db by default when the connection is made. This only happens if a db is not specified in the host string. See the docs http://www.php.net/manual/en/mongo.connecting.auth.php

#1 solves this issue by adding the db to the host string.

You can also solve this by adding the 'connection_options' element to you mongo_connections array in your settings file (which is kind of duplicative I know, but you don't have to hack the module :)).

$conf['mongodb_connections'] = array(
  'default' =>
    'host' => 'mongodb://example-db-server.com:27018',
    'db' => 'db-name',
    'connection_options' => array('db' => 'db-name', 'username' => 'user', 'password' => 'password'),

I'm wondering why we even have the db element in the the mongo_connection array at all, why not just do it the way the Mongo wants? Also, the patch for #1 won't work with replcaSets. :(

bigjim’s picture

Status:Active» Needs review
new518 bytes
new705 bytes
Test request sent.
[ View ]

Wrote a possible patch that utilized the db element of the $options array.

Added a D8 version, marked do not test.

fgm’s picture

Component:Watchdog» Miscellaneous

This appears to be related to the general driver, not to watchdog, so retagging.

majorrobot’s picture

Beautiful. Thanks for the tip re: changing settings.php, bigjim.

FWIW, we had this issue when we upgraded mongo from 2.2.x to 2.4.x and changed authorization of our users in Mongo to the new authorization structure introduced in 2.4. Unfortunately, once you wipe out your users with the old auth structure, so we had to change our Drupal settings. Works great so far.

SchnWalter’s picture

Issue summary:View changes
Status:Needs review» Reviewed & tested by the community

Tested D7 patch only, the D8 patch needs to be re-roled (if still needed).

SchnWalter’s picture

Title:Why using 'admin' db?» Specify a database to authenticate with
new726 bytes

When creating a database connection object if db is not specified, "admin" will be used. This causes issues if the user does not have permissions to access the admin database.

The database can be specified in two way, either in the $server info, or inside the $options array. @see http://php.net/manual/en/mongoclient.construct.php

The attached patch is only a re-rolling the patch from #6 which works perfectly. Please commit this D7 patch.


SchnWalter’s picture

Removed updated patch.

fgm’s picture

Priority:Critical» Normal
Status:Reviewed & tested by the community» Needs work

This is far from critical but seems rather sane overall.


  • The patch no longer applies, though (and its missing a test)
  • MongoDB tests just crash with this patch, because the static $mongo_objects is no longer initialized : not sure why you remove the selectDB() line ?

...so resetting to CNW.