Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use mongorestore/mongodump with username and password?

Tags:

mongodb

I know this is a basic security measure, but I can't make it works with my mongodb installation. Each time I run the command :

mongorestore dump_folder -u "username" -p "password"

It returns:

error reading database: not authorized on DATABASE_NAME to execute command { listCollections: 1, cursor: { batchSize: 0 } }

My question is : What is the permission that I need to enable and how ?

For the time being, I just turn off the Mongo's security in /etc/mongod.conf and start it back after I restore/dump my databases.

Additional Note: This is how I prepared my databases and its user

  1. I log in with my admin account, create a database, named DATABASE001
  2. And create a user:

    use DATABASE001
    db.createUser({ 
      user: "USER001", 
      pwd: "mypassword",
      roles:[ { role: "readWrite", db: "DATABASE001" },"dbAdmin"  ]
    })
    

For roles, I add readWrite to DATABASE001 and also dbAdmin I want this user (USER001) be able to backup or restore his database (DATABASE001).

  1. I tested it by login with this command. It successfully logged in

    mongo localhost -u USER001 -p mypassword --authenticationDatabase DATABASE001
    
  2. I want to dump this database content with:

    mongodump  -u USER001 -p mypassword --authenticationDatabase DATABASE001
    

    it throws me an error: Failed: error getting database names: not authorized on admin to execute command { listDatabases: 1 }

What is this listDatabases ? how can I add it to my roles?

like image 538
DennyHiu Avatar asked May 29 '17 08:05

DennyHiu


People also ask

How do I 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.

What is the difference between Mongoexport and Mongodump?

mongoexport is a command-line tool that produces a JSON or CSV export of data stored in a MongoDB instance. mongodump is a utility for creating a binary export of the contents of a database.


1 Answers

$ mongorestore --username adminUser --password pass123 --authenticationDatabase admin --db exampleDB dump/exampleDB
like image 142
martinho Avatar answered Oct 14 '22 20:10

martinho