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

14

79

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.

Drama answered 13/8, 2015 at 15:59 Comment(0)
D
140

work with this:

--authenticationDatabase admin

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

Drama answered 14/8, 2015 at 15:36 Comment(5)
Can you add some explanation as to why this is a solution?Olshausen
mongodump and mongorestore commands need the name of database where mongodb user's credentials are stored.Bacchic
usually the credentials are stored in the same database I want to backup/restore, am I wrong?Thinia
@Nate if you don't specify authenticationDatabase, mongodump will use the database you want to back up as authentication database. If the user you are authenticating as belongs to other database than the one you want to back up, you will get authentication errorMarienbad
This answer works for me, full command is like this: mongodump -u admin -p pwdxxx --authenticationDatabase admin -d mydb -o mydb_backupOlder
A
36

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.

Ailee answered 11/5, 2016 at 13:55 Comment(2)
this worked for me thanks, as many suggested executing mongodump with authenticationDatabase param does not work for meBuryat
This simplified command worked perfectly. I tried the other options of using --authenticationMechanism and --authenticationDatabase setting the correct values but continued to get issues with authentication failed.Assure
M
20
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.

Montero answered 18/2, 2020 at 14:2 Comment(0)
G
7
mongodump --collection coll_name --db DBname  -u UName -p *** 
          --authenticationDatabase <admin/privileged> --host ip  
          --port portNo  --out foldName
Genous answered 16/11, 2016 at 10:6 Comment(0)
V
7
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.

Villosity answered 29/4, 2020 at 22:44 Comment(0)
I
5

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
Islam answered 18/4, 2016 at 14:50 Comment(1)
Hello, welcome to SO. This answer would be clearer if you added a little explanation/commentary. Thank you!Edan
B
5

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

Blackthorn answered 26/9, 2016 at 17:1 Comment(1)
mlabs has an online utility to create new users as well. the new user worked for me.Portuguese
A
4

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

Adjacent answered 20/4, 2017 at 9:10 Comment(2)
It's critical that user has an appropriate role: backup or rootGuarantee
This happened to me too, only when I remove the password and then type it on next line, it worked. The problem was because I had special characters then I had to use single quote instead of double quote... thanks @Sambar for his answer.Faceharden
V
3

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>

Vine answered 20/11, 2019 at 20:46 Comment(0)
S
2

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

Sambar answered 5/3, 2018 at 11:52 Comment(0)
E
2

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"
Euphoria answered 2/3, 2020 at 8:18 Comment(0)
M
2

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'
}])
Marillin answered 20/3, 2020 at 5:22 Comment(0)
D
0

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.

Dignitary answered 29/7, 2018 at 14:43 Comment(0)
N
-1

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>
Nomenclature answered 13/5, 2019 at 2:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.