【问题标题】:Cannot add data to MongoDB Atlas "not authorized to execute command { insert: ... }"无法将数据添加到 MongoDB Atlas“未授权执行命令 { insert: ... }”
【发布时间】:2019-10-04 08:22:41
【问题描述】:

可以使用 StringURI 连接到 MongoDB,但是如果我尝试添加架构,我会收到 错误消息

MongoDB Connected ..
{ MongoError: not authorized on admin to execute command { insert: "animals", documents: [[{isEndangered true} {dateOfEntry 2019-10-03 22:33:54.852 +0000 UTC} {_id ObjectIdHex("5d967752e762ed56dc4f1dba")} {name Red Panda} {__v 0}]], ordered: true, writeConcern: { w: "majority" }, lsid: { id: {4 [194 242 186 89 84 47 69 212 143 253 85 42 86 82 70 95]} }, txnNumber: 1.000000, $clusterTime: { clusterTime: 6743708686104920070, signature: { hash: [247 92 77 50 
194 115 41 36 114 64 156 130 20 135 223 112 71 207 137 248], keyId: 6728180188896559104.000000 } }, $db: "admin" }
    at Connection.<anonymous> (C:\Users\xxxx\Documents\04_Programmering\mongoDB_Node\node_modules\mongodb\lib\core\connection\pool.js:466:61)
    at Connection.emit (events.js:197:13)
    at processMessage (C:\Users\xxxx\Documents\04_Programmering\mongoDB_Node\node_modules\mongodb\lib\core\connection\connection.js:364:10)
    at TLSSocket.<anonymous> (C:\Users\xxxx\Documents\04_Programmering\mongoDB_Node\node_modules\mongodb\lib\core\connection\connection.js:533:15)
    at TLSSocket.emit (events.js:197:13)
    at addChunk (_stream_readable.js:288:12)
    at readableAddChunk (_stream_readable.js:269:11)
    at TLSSocket.Readable.push (_stream_readable.js:224:10)
    at TLSWrap.onStreamRead [as onread] (internal/stream_base_commons.js:150:17)
  ok: 0,
  errmsg:
   'not authorized on admin to execute command { insert: "animals", documents: [[{isEndangered true} {dateOfEntry 2019-10-03 22:33:54.852 +0000 UTC} {_id ObjectIdHex("5d967752e762ed56dc4f1dba")} {name Red Panda} {__v 0}]], ordered: true, writeConcern: { w: "majority" }, lsid: { id: {4 [194 242 186 89 84 47 69 212 143 253 85 42 86 82 70 95]} }, txnNumber: 1.000000, $clusterTime: { clusterTime: 6743708686104920070, signature: { hash: [247 92 77 50 194 115 41 36 114 64 156 130 20 135 223 112 71 207 137 248], keyId: 6728180188896559104.000000 } }, $db: "admin" }',
  code: 8000,
  codeName: 'AtlasError',
  name: 'MongoError',
  [Symbol(mongoErrorContextSymbol)]: {} }

我的连接字符串:(使用包'config'没有问题[试过没有])

{
    "mongoURI": "mongodb+srv://xxxx:xxxx@xxxx-xxxx.mongodb.net/admin?retryWrites=true&w=majority"
}

使用 Mongoose 驱动程序和连接:

mongoose
    .connect(db, { useNewUrlParser: true, useCreateIndex: true, useFindAndModify: false, useUnifiedTopology: true})
    .then(() => console.log('MongoDB Connected ..'))
    .catch(err => console.log(err));

我试过了:

  • 在网络访问中将我当前的 IP 和 0.0.0.0/0 列入白名单
  • 具有两个角色(具有正确的用户名和密码的 AtlasAdmin 和 ReadWrite-user)授权
  • 使用连接字符串:mongodb+srv://:@xxxx-xxxxx.mongodb.net/admin?retryWrites=true&w=majority
  • 我知道 Atlas 的局限性,但我试图像这样简单地添加表格:
const newAnimal = new Animal({
    name: 'Red Panda',
    isEndangered: true
});

newAnimal
    .save()
    .then(item => console.log(item))
    .catch(err => console.log(err));

这方面的新手,我什至不想在开始之前退出.. 免费的 Atlas 帐户似乎是小型演示和学习的良好开端,无需处理连接和架构等。

【问题讨论】:

    标签: node.js mongodb


    【解决方案1】:

    错误是说:

    MongoError: not authorized on admin to execute command { insert: ....
    

    这意味着您正在尝试插入admin 数据库,这是一个受限制的特殊数据库。

    更改您的连接字符串以使用另一个数据库,例如:

    "mongodb+srv://xxxx:xxxx@xxxx-xxxx.mongodb.net/test?retryWrites=true&w=majority"
    

    以便您连接到名为test 的数据库。您的插入应该可以工作。

    【讨论】:

    • 我已将我的 connString 更改为 mongodb+srv://xxxx:xxxx@xxxx-xxxx.mongodb.net/playground?retryWrites=true&amp;w=majority,但我仍然收到阻止我进行插入的错误。该用户属于readWriteAnyDatabase 角色。关于我缺少什么的任何想法?
    • 我尝试使用 .find() 而不是 .save(),然后我得到 MongoError: user is not allowed to do action [find] on [admin.zips]。好像它没有切换到playground 数据库?!我的 connectionString 肯定有...mongodb.net/playground?... 表示我想使用playground 数据库。
    • 呃哇没关系...原来问题出在mongoose的版本中。我更新了它,现在它工作正常!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-30
    • 1970-01-01
    • 2018-04-06
    • 1970-01-01
    • 2015-03-03
    • 1970-01-01
    • 2019-02-11
    相关资源
    最近更新 更多