简体   繁体   English

无法使用 --db 创建备份 mongodump。 认证失败

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

When I create backup of all databases in MongoDB (version 3):当我在 MongoDB(版本 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.失败:连接到数据库服务器时出错:服务器在 SASL 身份验证步骤中返回错误:身份验证失败。

work with this:使用这个:

--authenticationDatabase admin

mongodump and mongorestore commands need the name of database where mongodb user's credentials are stored. mongodump 和 mongorestore 命令需要存储 mongodb 用户凭据的数据库名称。 (thanks @Zubair Alam) (感谢@Zubair Alam)

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.如果用户名或密码错误,也会弹出此错误。

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

经过所有的跟踪,我发现上面的工作命令可以从 mongdb 转储。

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

here root is username and yourpassword is your password.这里 root 是用户名,而 yourpassword 是你的密码。

This command will backup/dump all database back-up in current directory.此命令将备份/转储当前目录中的所有数据库备份。

If you still get same error with --authenticationDatabase admin , than probably your username and password are incorrect.如果您仍然遇到与 --authenticationDatabase admin 相同的错误,那么您的用户名和密码可能不正确。 Try adding a user db.createUser() , with appropriate role ( i gave write permission as well)尝试添加一个用户 db.createUser() ,具有适当的角色(我也给了写权限)

than run below command : (ignore -h if you are running on local)比在命令下运行:(如果您在本地运行,请忽略 -h)

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

Hope this helps...希望这可以帮助...

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

The following steps worked for me on MongoDB 3.2:以下步骤在 MongoDB 3.2 上对我有用:

  1. Check of course if your admin username and pw are correct.当然,请检查您的管理员用户名和密码是否正确。 You can do this with the mongo shell:您可以使用 mongo shell 执行此操作:

mongo蒙戈

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

If this returns 1, the password is correct.如果返回 1,则密码正确。

  1. Then add the role "backup" to your admin (or make sure this role is already added).然后将角色“备份”添加到您的管理员(或确保已添加此角色)。 db.grantRolesToUser("admin", [{ role: "backup", db: "admin" }]) db.grantRolesToUser("admin", [{ role: "backup", db: "admin" }])

  2. Finally, the mongodump command.最后是 mongodump 命令。 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 mongodump --username admin --authenticationDatabase admin --db yourmongodatabase

Then add your password when it promts for it.然后在提示时添加您的密码。

This works for me...这对我有用...

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 mongodump -d 数据库名称 -u 用户名 -p '密码' --out directory_name

In my case, mongodump was not correctly processing the password.就我而言, mongodump没有正确处理密码。 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>

userAdminAnyDatabase is not enough to do mongodump on all the dbs that's why you are getting this error. userAdminAnyDatabase 不足以对所有 dbs 执行 mongodump,这就是您收到此错误的原因。 You will need a super user that has:您将需要一个具有以下特性的超级用户:

userAdminAnyDatabase
readWriteAnyDatabase
clusterAdmin

privileges to run mongodump on all dbs.在所有数据库上运行 mongodump 的权限。

OR you just need 'backup' privilege或者您只需要“备份”权限

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

I was having this same problem when trying to dump my database info from mLab.我在尝试从 mLab 转储我的数据库信息时遇到了同样的问题。 It was happening because I had mongo 2.x locally and 3.x in mLab.这是怎么回事,因为我有蒙戈2.x本地和3.x在MLAB。 Upgrading my local mongo to the same major version as mLab allowed me to do the dump, thus solving the problem.将我的本地 mongo 升级到与 mLab 相同的主要版本允许我进行转储,从而解决了问题。

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)如果您使用--uri选项,则需要将authSource=admin作为参数添加到您的连接字符串中(假设您的用户在 admin 数据库中)

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"

If you are using mLab then it could be the version in your local mongo is not match with mLab.如果您使用的是 mLab,则可能是您本地 mongo 中的版本与 mLab 不匹配。 By default, Ubuntu will install mongo v2.x and mLab is v3.x .默认情况下,Ubuntu 将安装 mongo v2.x而 mLab 是v3.x You could check with this command:你可以用这个命令检查:

mongo --version

Install new mongo version:安装新的 mongo 版本:

  1. Remove your old local mongo ( be careful, it may remove all your local database )删除旧的本地 mongo(小心,它可能会删除所有本地数据库
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.为 MongoDB 创建一个列表文件。

    • Ubuntu 14.04 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 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安装 MongoDB 包
sudo apt-get install -y mongodb-org

Ref: https://docs.mongodb.com/v3.2/tutorial/install-mongodb-on-ubuntu/参考: 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM