getredash/redash · error · Exception

MongoDB connection error

Error message

MongoDB connection error

What it means

Raised by MongoDBQueryRunner.test_connection() when the connectionStatus command on the connected database returns ok != 1. It indicates the server accepted a socket but the status command failed — typically auth/permission problems on the database rather than unreachable host.

Source

Thrown at redash/query_runner/mongodb.py:231

            kwargs["replicaSet"] = self.configuration["replicaSetName"]
            readPreference = self.configuration.get("readPreference")
            if readPreference:
                kwargs["readPreference"] = readPreference

        if self.configuration.get("username"):
            kwargs["username"] = self.configuration["username"]

        if self.configuration.get("password"):
            kwargs["password"] = self.configuration["password"]

        db_connection = pymongo.MongoClient(self.configuration["connectionString"], **kwargs)

        return db_connection[self.db_name]

    def test_connection(self):
        db = self._get_db()
        if not db.command("connectionStatus")["ok"]:
            raise Exception("MongoDB connection error")

        return db

    def _merge_property_names(self, columns, document):
        for property in document:
            if property not in columns:
                columns.append(property)

    def _is_collection_a_view(self, db, collection_name):
        if "viewOn" in db[collection_name].options():
            return True
        else:
            return False

    def _get_collection_fields(self, db, collection_name):
        # Since MongoDB is a document based database and each document doesn't have
        # to have the same fields as another documet in the collection its a bit hard to
        # show these attributes as fields in the schema.

View on GitHub (pinned to ca79fe988d)

Solutions

  1. Verify credentials and add the correct auth source, e.g. mongodb://user:pass@host/db?authSource=admin
  2. Confirm the user has at least read access (and clusterMonitor for status commands) on the target database
  3. Check server/replica-set health and that the URI points at the right deployment
  4. If behind a proxy/Atlas, ensure the client IP is allow-listed

Example fix

// before
mongodb://user:pass@host/mydb
// after
mongodb://user:pass@host/mydb?authSource=admin
Defensive patterns

Strategy: retry

Validate before calling

# Pre-flight: parse the URI and confirm authSource present for credentialed users
from urllib.parse import urlparse
u = urlparse(uri)
assert u.username is None or 'authSource' in (u.query or ''), 'add ?authSource=<db>'

Try / catch

try:
    runner.test_connection()
except Exception as e:
    if 'MongoDB connection error' in str(e):
        check_credentials_and_authsource(); retry_once_after_fix()

Prevention

When it happens

Trigger: Clicking "Test Connection" on a MongoDB data source where credentials lack permission to run connectionStatus, the user is authenticated to the wrong database, or the server/HA-proxy returns an error status.

Common situations: Wrong username/password (auth source admin not specified in the connection string), MongoDB user created in a different auth database, or connecting to a mongos/Atlas tier where connectionStatus is disallowed for the role.

Related errors


AI-assisted analysis of getredash/redash@ca79fe988d (2026-08-28). Data as JSON: /api/errors/635430d69c2c6b09. Report an issue: GitHub.