Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 929
user7593937 Avatar asked Feb 20 '17 15:02

user7593937


People also ask

How do you use Mongorestore?

Basic mongorestore syntax The basic way to restore a database is to use the mongorestore command to specify the backup directory (dump directory) without any options. This option is suitable for databases located in the localhost (127.0. 0.1) using the port 27017.

How do I restore my MongoDB database?

The mongorestore utility restores a binary backup created by mongodump . By default, mongorestore looks for a database backup in the dump/ directory. The mongorestore utility restores data by connecting to a running mongod directly. mongorestore can restore either an entire database backup or a subset of the backup.

What is Mongodump and Mongorestore?

Database backup is a copy of a database that already exists. In MongoDB, mongodump tool is used to take the data backup. And mongorestore tool is used to restore the backup data.

Does Mongorestore overwrite?

No. From mongorestore: If you restore to an existing database, mongorestore will only insert into the existing database, and does not perform updates of any kind. If existing documents have the same value _id field in the target database and collection, mongorestore will not overwrite those documents.


3 Answers

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/
like image 95
Aaron Silverman Avatar answered Nov 07 '22 03:11

Aaron Silverman


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 .
like image 25
Yash Avatar answered Nov 07 '22 02:11

Yash


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.

like image 25
user7593937 Avatar answered Nov 07 '22 03:11

user7593937