MongoError: listCollections failed

3.3k views Asked by At

I have setted up a mongo database, which works with authentication.

I'm trying to copy a database using mongodb native driver.


The problem is my command get rejected as :

MongoError: listCollections failed: { ok: 0.0, errmsg: "not authorized on SESSION to execute command { listCollections: 1, filter: { $or: [ { type: "collection" }, { type: { $exists: false } } ] }, cu...", code: 13, codeName: "Unauthorized" }

Here is how I run my database :

/usr/bin/mongod --quiet
                --setParameter authenticationMechanisms=SCRAM-SHA-1
                --auth
                --port 27017
                --dbpath /database

Here is the user I'm using to connect

  db.createUser({
      "user": "USER",
      "pwd": "PASS",
      "roles": [ "root" ]
  });

I also tried using :

  db.createUser({
      "user": "USER",
      "pwd": "PASS",
      "roles": [ { "role": "root", "db": "admin" } ]
  });

Here is how I connect (the connection succeed) :

const url = "mongodb://USER:[email protected]:27017/SESSION?authSource=admin";

MongoClient.connect(url, ...

Here is how I request the copy :

const mongoCommand = {
     copydb: 1,
     fromhost: "172.42.0.2",
     fromdb: "SESSION",
     todb: "SESSION_COPY",
};

// Perform the copy
this.db.admin().command(mongoCommand, function (err, data) {

I am using :

Mongodb (database) v3.4.7
Mongodb (node package) v2.2.31

Thanks :)


Some posts that get me through this :

mongodb-not-authorized-for-query-admin-system-users

how-can-i-execute-db-copydatabase-through-nodejss-mongodb-native-driver

1

There are 1 answers

0
Orelsanpls On BEST ANSWER

Ok, so first of all, here is the copydb command mongodb documentation.

enter image description here

We can see there that in case of using fromhost you need to specify username,nouce and key.


SOLUCE

If you want to copy a database in the same host: Do not use of fromhost.

Example :

const mongoCommand = {
    copydb: 1,
    fromdb: "SESSION",
    todb: "SESSION_SAVE",
};

If you want to copy a database using different hosts: Use of username, nonce and key here is how you create them.