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

user7593937 picture user7593937 · Feb 20, 2017 · Viewed 18.7k times · Source

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?

Answer

Zugwalt picture Zugwalt · Apr 25, 2017

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/