问题
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.
回答1:
work with this:
--authenticationDatabase admin
mongodump and mongorestore commands need the name of database where mongodb user's credentials are stored. (thanks @Zubair Alam)
回答2:
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.
回答3:
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...
回答4:
mongodump --collection coll_name --db DBname -u UName -p ***
--authenticationDatabase <admin/privileged> --host ip
--port portNo --out foldName
回答5:
The following steps worked for me on MongoDB 3.2:
- 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.
Then add the role "backup" to your admin (or make sure this role is already added). db.grantRolesToUser("admin", [{ role: "backup", db: "admin" }])
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...
回答6:
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
回答7:
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
回答8:
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>
回答9:
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.
回答10:
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:
- Remove your old local mongo (be careful, it may remove all your local database)
sudo apt remove mongo-clients mongodb
- Import the public key used by the package management system.
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv D68FA50FEA312927
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
- 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>
来源:https://stackoverflow.com/questions/31993168/cant-create-backup-mongodump-with-db-authentication-failed