Can't create backup mongodump with --db. Authentication failed

MongodbAuthenticationMongodump

Mongodb Problem Overview


When I create backup of all databases in MongoDB (version 3):

mongodump --username bacUser --password 12345

It's OK. But when I try to create backup of a selected db:

mongodump --username bacUser --password 12345 --db test

It gives me this error:

> Failed: error connecting to db server: server returned error on SASL authentication step: Authentication failed.

Mongodb Solutions


Solution 1 - Mongodb

work with this:

--authenticationDatabase admin

mongodump and mongorestore commands need the name of database where mongodb user's credentials are stored. (thanks @Zubair Alam)

Solution 2 - Mongodb

This should work.

mongodump -h SERVER_NAME:PORT -d DATABASE_NAME -u DATABASE_USER -p PASSWORD

Also this error can popup if username or password are wrong.

Solution 3 - Mongodb

mongodump --host <host-ip> --port 27017 --db <database>  --authenticationDatabase admin --username <username> --password <password> --out ./Documents/

After all the trail, I found above working command to dump from mongdb.

Solution 4 - Mongodb

mongodump --collection coll_name --db DBname  -u UName -p *** 
          --authenticationDatabase <admin/privileged> --host ip  
          --port portNo  --out foldName

Solution 5 - Mongodb

mongodump   --authenticationDatabase admin -uroot -pyourpassword

here root is username and yourpassword is your password.

This command will backup/dump all database back-up in current directory.

Solution 6 - Mongodb

for dump and restore

mongodump --db nameDatabase --username userName --password password --authenticationDatabase admin --out mongodb\
mongorestore --db nameDatabase --username userName --password password --authenticationDatabase admin <path backup> --drop

Solution 7 - Mongodb

If you still get same error with --authenticationDatabase admin , than probably your username and password are incorrect. Try adding a user db.createUser() , with appropriate role ( i gave write permission as well)

than run below command : (ignore -h if you are running on local)

 mongodump -h <ip>:<port_number> -d db_name -u newUser -p newPassword -o /home/mongodump/

Hope this helps...

Solution 8 - Mongodb

The following steps worked for me on MongoDB 3.2:

  1. Check of course if your admin username and pw are correct. You can do this with the mongo shell:

mongo

use admin db.auth("admin", "yourpassword")

If this returns 1, the password is correct.

  1. Then add the role "backup" to your admin (or make sure this role is already added). db.grantRolesToUser("admin", [{ role: "backup", db: "admin" }])

  2. Finally, the mongodump command. It did not work for me when I tried to pass the password as an argument. Instead do this:

mongodump --username admin --authenticationDatabase admin --db yourmongodatabase

Then add your password when it promts for it.

This works for me...

Solution 9 - Mongodb

Use signle Quotation around password. if you are using any special character in your password. That will solve your issue. Use following command.

mongodump -d database_name -u userName -p 'password' --out directory_name

Solution 10 - Mongodb

In my case, mongodump was not correctly processing the password. The solution was to escape the password literal.

This did not work:

mongodump -p my$password -o <output directory>

This did work:

mongodump -p 'my$password' -o <output directory>

Solution 11 - Mongodb

userAdminAnyDatabase is not enough to do mongodump on all the dbs that's why you are getting this error. You will need a super user that has:

userAdminAnyDatabase
readWriteAnyDatabase
clusterAdmin

privileges to run mongodump on all dbs.

OR you just need 'backup' privilege

db.grantRolesToUser('username', [{
  role: 'backup',
  db: 'name of ur authentication db'
}])

Solution 12 - Mongodb

If you use the --uri option, you need to add authSource=admin as a parameter to your connection string (assuming your user is on the admin database)

mongodump --uri="mongodb://user:pass@host1:27017,host2:27017,host3:27017/?replicaSet=rs0"

Becomes:

mongodump --db=test --uri="mongodb://user:pass@host1:27017,host2:27017,host3:27017/?replicaSet=rs0&authSource=admin"

Solution 13 - Mongodb

I was having this same problem when trying to dump my database info from mLab. It was happening because I had mongo 2.x locally and 3.x in mLab. Upgrading my local mongo to the same major version as mLab allowed me to do the dump, thus solving the problem.

Solution 14 - Mongodb

If you are using mLab then it could be the version in your local mongo is not match with mLab. By default, Ubuntu will install mongo v2.x and mLab is v3.x. You could check with this command:

mongo --version

Install new mongo version:

  1. Remove your old local mongo (be careful, it may remove all your local database)
sudo apt remove mongo-clients mongodb
  1. Import the public key used by the package management system.
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv D68FA50FEA312927
  1. Create a list file for MongoDB.
  • Ubuntu 14.04
echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
  • Ubuntu 16.04
echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
  1. Install the MongoDB packages
sudo apt-get install -y mongodb-org

Ref: https://docs.mongodb.com/v3.2/tutorial/install-mongodb-on-ubuntu/

Now you can dump your database with this command:

mongodump -h <host>:<port> -d <database-name> -u <user> -p <password> -o <output directory>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVladislav OrilloView Question on Stackoverflow
Solution 1 - MongodbVladislav OrilloView Answer on Stackoverflow
Solution 2 - MongodbMilos MaticView Answer on Stackoverflow
Solution 3 - MongodbGaneshView Answer on Stackoverflow
Solution 4 - MongodbGyanendro LoitongbamView Answer on Stackoverflow
Solution 5 - MongodbJoshiView Answer on Stackoverflow
Solution 6 - Mongodbuser3784566View Answer on Stackoverflow
Solution 7 - MongodbShirish SinghView Answer on Stackoverflow
Solution 8 - Mongodbuser2996950View Answer on Stackoverflow
Solution 9 - MongodbadeelView Answer on Stackoverflow
Solution 10 - Mongodbuser2722165View Answer on Stackoverflow
Solution 11 - MongodbAero WangView Answer on Stackoverflow
Solution 12 - MongodbGert van den BergView Answer on Stackoverflow
Solution 13 - MongodbMarcelo LazaroniView Answer on Stackoverflow
Solution 14 - MongodbwindyzboyView Answer on Stackoverflow