user7593937
user7593937

Reputation: 555

Use mongorestore to restore a database to MongoDB (3.4) with --auth enabled, SASL error

Using mongorestore, I am trying to restore a MongoDB database to a new server (both version are 3.4). The new server has -auth enabled, so you are required to login. The database does not exist so I want mongorestore to create it using the --db option. This works when authorization is not enabled but if I enable authorization the restore fails with the following error:

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

I am using an admin account with the root role when I attempt the restore.

Backing up prod and restoring to dev is a fairly regular activity for us, but we can't just drop the existing database and recreate it because of the error above, not unless we disable authorization which doesn't make much sense. Is there a better way to do this/avoid the SASL errors/not have to disable auth?

Upvotes: 13

Views: 29448

Answers (4)

Henadzi Rabkin
Henadzi Rabkin

Reputation: 7033

initdbScripts:
  restore.sh: |
    mongorestore --drop --gzip --archive=/data/mongodbdump.gz --verbose=2 --authenticationDatabase admin -u $MONGODB_ROOT_USER -p $MONGODB_ROOT_PASSWORD

for bitnami/mongodb helmchart

Upvotes: 0

Yash
Yash

Reputation: 114

First Access your db to 4366 port then run this command

mongorestore --port 4366 -u admin -p password --authenticationDatabase admin -d dealmoney /home/yash/Desktop/prodDump/teatingToProductionLastDump/dealmoney .

Upvotes: 5

Aaron Silverman
Aaron Silverman

Reputation: 22635

I was getting the same error and while I couldn't figure out what was wrong restoring with my admin user (my hunch is a ! in the password which escaping did not help) I was able to restore by creating a new user specifically for the role.

In mongo shell:

>use admin;

>db.createUser({
  user: 'restoreuser',
  pwd: 'restorepwd',
  roles: ['restore']
});

In terminal:

$mongorestore --host databasehost:12345 --username restoreuser --password restorepwd --authenticationDatabase admin --db targetdb ./path/to/dump/

Upvotes: 27

user7593937
user7593937

Reputation: 555

Thanks to Adamo Tonete over at Percona, he helped us solve this problem. If you want to restore a database using your admin user with the root role, you need to specify the authentication database and user in the mongorestore command.

mongorestore --host hostname:27017 -u adminuser -p pass --authenticationDatabase admin -d TargetDatabase /Data/TargetDatabaseRestore

That tells mongo to use the admin database to authenticate the user you are passing in. If that user has the correct rights assigned, it will be able to create the new database.

Upvotes: 11

Related Questions