Search code examples
mongodbmongodump

Mongodump fails with "a direct connection cant be made if multiple hosts are specified"


I have a dev & staging database which i connect to regularly (using mongoose.connect(<uri>)).

I would like to mongodump them. Im using the following syntax:

mongodump --uri=<uri>

This works fine for dev and I was able to dump the dev database using this command. The staging database however produces the following error:

Failed: can't create session: error configuring the connector: a direct connection cannot be made if multiple hosts are specified

Does anyone know or have any insight on what the issue could be? I haven't found any docs or posts about this error.


Solution

  • Historically, when given a URI with one host in it (or otherwise a single host to connect to), some MongoDB drivers performed a direct connection to that host only and some drivers inspected the type of the server and, if it was a replica set node, connected to replica set (which may have caused none of the queries or commands to be sent to the host which was originally given in the URI).

    In the MongoDB 4.4-compatible drivers the behavior was unified, and directConnection URI option added to force either behavior (true to connect to the specified host, false to perform discovery; if not set, the driver would do whatever it did historically).

    When direct connection is used, you can only specify one host. Your URI contains or resolves to more than one.

    For the gritty details, see https://github.com/mongodb/specifications/pull/739.