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

21,412

Solution 1

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/

Solution 2

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.

Solution 3

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 .
Share:
21,412
user7593937
Author by

user7593937

Updated on April 13, 2020

Comments

  • user7593937
    user7593937 about 4 years

    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?