Andrew Mao
Andrew Mao

Reputation: 36930

Using mongodump with a standard Mongo URI

The mongo client can connect with a standard URI:

mongo mongodb://<dbuser>:<dbpassword>@<server>:<port>/<db>

However, mongodump seems to require an awkward syntax breaking this up into different arguments:

mongodump -u dbuser -p dbpassword -h server -p port -d db ...

Is there a quick and easy way to pass a URI to mongodump as well?

Upvotes: 19

Views: 36311

Answers (6)

Binson Selvin
Binson Selvin

Reputation: 49

As the answer is already posted but if RBAC is enabled then use the following syntax.

mongodump --uri url/databaseName --port mongoport -u username --authenticationDatabase admin

--authenticationDatabase should point to admin as user details and credentials will be stored in admin database by mongodb

provided a tested and working example below on v7.0.5

mongodump --uri "mongodb://127.0.0.1/SK_Mongo_Application" --port 27001 -u root --authenticationDatabase admin

Upvotes: 0

Hrishikesh Baidya
Hrishikesh Baidya

Reputation: 567

Easy steps to getting dump and restore whole database at once with any data loses

take dump in the local machine

mongodump --uri "mongodb+srv://username:[email protected]/dbname" --out "C:\databaseDump"

**locate to the dumped folder and restore to local database **

mongorestore --db dbname C:\databaseDump\dbname

restore to remote database

mongorestore --uri "mongodb+srv://username:[email protected]/dbname" --db dbname C:\databaseDump\dbname

Upvotes: 16

Joe
Joe

Reputation: 1342

A simple and easy string to understand

mongodump --uri='mongodb://...url/...db-name' --out $(pwd)

or

mongodump --uri='mongodb+srv://...url/...db-name' --out $(pwd)

Depending on your implementation, will dump the whole database of $(db-name) located at $(url) and place the contents into your current working directory $(pwd)

Upvotes: 1

igorkf
igorkf

Reputation: 3565

Answer: 05-15-2020

I know this is a late answer, but it solved my problem.
To dump using an URI, do:

mongodump --uri=[uri]/[db]

where:

  • uri is your URI (e.g. mongodb+srv://john:[email protected])
  • db is the database that you want to dump (e.g. sales)

So in this artificial example, it would be like this:

mongodump --uri=mongodb+srv://john:[email protected]/sales

So if this really did not work, edit the file /etc/resolv.conf and change nameserver value to e.g. 8.8.8.8, like this:

# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. This file lists all
# configured search domains.
#
# Run "systemd-resolve --status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

# nameserver 127.0.0.53
nameserver 8.8.8.8
options edns0

Now it should works.

Upvotes: 18

jbustamovej
jbustamovej

Reputation: 589

Even if you supply the uri option, you still need to supply the db option. The documentation at https://docs.mongodb.com/manual/reference/program/mongorestore/ is incorrect when it says that the db option is "incompatible" with the uri option. If you don't supply the db option together with the uri option you get a don't know what to do with file error for each .bson file that you're trying to restore. I tested using mongorestore version 3.6.9 on ubuntu and from mac osx.

Upvotes: -2

Neil Lunn
Neil Lunn

Reputation: 151132

The --uri option was added within a minor release of MongoDB 3.4.6. This was referenced in the JIRA issue TOOLS-1587.

It actually did not get official documentation until the MongoDB 3.6 release, but it's now in the manual page

--uri
New in version 3.4.6.

Specify a resolvable URI connection string for the mongod to which to connect.

The following is the standard URI connection scheme:

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

For detailed explanations of the components of this string, refer to the Connection String URI Format documentation.

The same --uri option is added to other tools such as mongoexport, mongoimport and mongorestore.

Upvotes: 20

Related Questions