使用 mongorestore,我尝试将 MongoDB 数据库恢复到新服务器(两个版本都是 3.4)。新服务器已启用 -auth,因此您需要登录。该数据库不存在,因此我希望 mongorestore 使用 --db 选项创建它。当未启用授权时,此方法有效,但如果我启用授权,恢复将失败并出现以下错误:
失败:连接到数据库服务器时出错:服务器在 SASL 身份验证步骤上返回错误:身份验证失败。
当我尝试恢复时,我使用的是具有 root 角色的管理员帐户。
备份生产环境并恢复到开发环境对我们来说是一项相当常规的活动,但我们不能因为上述错误而删除现有数据库并重新创建它,除非我们禁用没有多大意义的授权。有没有更好的方法来做到这一点/避免 SASL 错误/不必禁用身份验证?
我遇到了同样的错误,虽然我无法弄清楚用我的管理员用户恢复时出了什么问题(我的预感是密码中的
!
,转义没有帮助)我能够通过创建一个新用户来恢复专门针对这个角色。
在 mongo shell 中:
>use admin;
>db.createUser({
user: 'restoreuser',
pwd: 'restorepwd',
roles: ['restore']
});
在终端:
$mongorestore --host databasehost:12345 --username restoreuser --password restorepwd --authenticationDatabase admin --db targetdb ./path/to/dump/
感谢 Percona 的 Adamo Tonete 帮助我们解决了这个问题。如果您想使用具有 root 角色的管理员用户恢复数据库,则需要在 mongorestore 命令中指定身份验证数据库和用户。
mongorestore --host 主机名:27017 -u adminuser -p pass --authenticationDatabase admin -d TargetDatabase /Data/TargetDatabaseRestore
这告诉 mongo 使用管理数据库来验证您传入的用户。如果该用户分配了正确的权限,它将能够创建新数据库。
首先将您的数据库访问到 4366 端口,然后运行此命令
mongorestore --port 4366 -u admin -p password --authenticationDatabase admin -d dealmoney /home/yash/Desktop/prodDump/teatingToProductionLastDump/dealmoney .
initdbScripts:
restore.sh: |
mongorestore --drop --gzip --archive=/data/mongodbdump.gz --verbose=2 --authenticationDatabase admin -u $MONGODB_ROOT_USER -p $MONGODB_ROOT_PASSWORD
对于 bitnami/mongodb helmchart