MongoError:listCollections失败

时间:2017-09-13 11:31:11

标签: node.js mongodb node-mongodb-native

我已经建立了一个mongo数据库,它可以用于身份验证。

我尝试使用mongodb native driver复制数据库。

问题是我的命令被拒绝为:

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" }

以下是我运行数据库的方法:

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

以下是我用来连接的用户

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

我也尝试过使用:

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

以下是我的连接方式(连接成功):

const url = "mongodb://USER:PASS@172.42.0.2:27017/SESSION?authSource=admin";

MongoClient.connect(url, ...

以下是我如何申请副本:

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) {

我正在使用:

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

谢谢:)

一些帖子让我了解了这一点:

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

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

1 个答案:

答案 0 :(得分:0)

好的,首先,这里是copydb command mongodb documentation

enter image description here

我们可以看到,如果使用fromhost,您需要指定usernamenoucekey

<强> SOLUCE

如果您要在同一主机中复制数据库:请勿使用 fromhost

示例

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

如果您想使用不同的主机复制数据库:使用usernamenoncekey here is how you create them