我最终在MongoDB大学讨论版上提出了这个问题。以防有人遇到同样的情况,这似乎是因为我使用的是免费的集群。
当我尝试使用以下链接连接到一个Node.js版本3.0或更高的集群时,遇到了相同的问题:
mongodb+srv://username:password@cluster0-eoowo.mongodb.net/test?retryWrites=true
通过选择版本2.2.12或更高并使用提供的链接,一切都进行得很顺利。
mongodb://username:password@cluster0-shard-00-00-eoowo.mongodb.net:27017,cluster0-shard-00-01-eoowo.mongodb.net:27017,cluster0-shard-00-02-eoowo.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true
这让我疯了...原来MongoDB提供的标准链接试图将我的默认数据库命名为“admin”。请更改以下链接:
mongodb+srv://username:password@portfoliosite-ezeot.mongodb.net/admin?retryWrites=true&w=majority
到
mongodb+srv://username:password@portfoliosite-ezeot.mongodb.net/test?retryWrites=true&w=majority
你可以测试任何单词,但不能使用“admin”。希望这可以帮到你!
导致此错误的另一个原因是登录的用户与启动有问题操作的用户不同--要使用db.killOp
,您需要以与启动该操作的用户相同的身份登录到mongo shell中。
在Atlas上,数据库用户可以使用
db.killOp()
命令来终止自己的操作。您是正确的,拥有atlasAdmin权限的帐户无法终止由其他账户在集群上启动的操作。如果您在mongo shell中使用应用程序用于启动有问题操作的用户进行身份验证,然后您就可以使用db.killOp()来终止该操作。
(来源为Atlas支持票据)
我曾经面临过类似的障碍并花费了数小时,问题出在mongodb atlas提供的连接字符串上,它是:mongodb+srv://username:password@clusterName-jynkd.mongodb.net/test?retryWrites=true
我使用了一个不同的连接字符串,完美地解决了这个问题。这就是它:
mongodb://username:password@clusterName+port/yourdb?retryWrites=true&ssl=true&authSource=admin
。
确保clustername + port像这样:
clusterName-shard-00-00-jynkd.mongodb.net:27017
这个错误是因为你正在尝试在管理员之上创建集合,在Atlas中这是不可能的,因为安全性。
在这种情况下,你需要使用单词键使用[collection_name]
这是这个答案的链接。
它是用西班牙语写的。
https://victorroblesweb.es/2016/12/24/crear-una-base-datos-mongodb/
这个答案并不完全针对原始问题,但是这是我在谷歌搜索我的错误信息“mongodb atlas errmsg: '(unauthorized) not authorized on admin to execute command”时一直看到的主要帖子。
如果你在2021年遇到了这个错误,并且正在使用Mongoose V4或更低版本连接MongoDB Atlas,请尝试将Mongoose更新到最新版本(发布此答案时为V5.12.13)。
打开终端,导航到您的项目目录,并运行
npm i mongoose@latest
我之前一直在使用一门关于Node.js和Express的在线课程提供的起始代码,该代码是在2018年中期制作的。这个起始代码使用了Mongoose V4,而且对我来说非常有效。我的MongoDB Atlas集群是M0免费版。