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).

Comments

In mongodb.module changed this:

<?php
$mongo
= new Mongo($host, $options);
?>

to this:

<?php
$mongo
= new Mongo($host . '/' . $db, $options);
?>

And it works.

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->setSlaveOkay(TRUE);
      }
      $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.

from #1 work for me too

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

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' =>
  array(
    '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. :(

Status:Active» Needs review
StatusFileSize
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.

Component:Watchdog» Miscellaneous

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

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.