mongodb show dbs 给出“未授权执行命令”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35881662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:40:44  来源:igfitidea点击:

show dbs gives "Not Authorized to execute command" error

mongodb

提问by Anjou

I've spent some time trying to figure out what is wrong but as I could not find out, I decided to ask here.

我花了一些时间试图找出问题所在,但由于找不到,我决定在这里提问。

I am running MongoDB(Windows 64-bit 2008 R2+) version 3.2.3 on Windows 8, the paths are :

我在 Windows 8 上运行 MongoDB(Windows 64 位 2008 R2+)版本 3.2.3,路径是:

C:\MongoDB\binfor the installation

C:\MongoDB\bin用于安装

C:\data\dbfor the data folder

C:\data\db用于数据文件夹

I've installed following this videoand this tutorialfrom the official documentation.

我已经按照官方文档中的此视频本教程进行了安装。

The first issue might be, as I don't really know if it is an issue at all, the connection between the client(mongo.exe) and the server(mongod.exe).

第一个问题可能是,因为我根本不知道这是否是一个问题,客户端(mongo.exe)和服务器(mongod.exe)之间的连接。

I lauched mongod.exe via the command line (with administrator rights), everything went fine, I got the message :

我通过命令行(具有管理员权限)启动 mongod.exe,一切顺利,我收到消息:

waiting for connections on port 27017

but when I launch mongo.exe via a new instance of the command line, the server(mongod.exe) doesn't print a message saying there is a new connection (it was the case in the tutorials I watched)

但是当我通过命令行的新实例启动 mongo.exe 时,服务器(mongod.exe)不会打印一条消息说有一个新连接(在我观看的教程中就是这种情况)

On the other side, mongo.exe prints

另一方面,mongo.exe 打印

connecting to : test

I don't know if everything is correct at this point but I still tried some basics commands like :

我不知道此时一切是否正确,但我仍然尝试了一些基本命令,例如:

show dbsreturns not authorized on admin to execute command

show dbs返回 not authorized on admin to execute command

Basically, all the commands I tried had the same error message, even with "fresh" db I just created with use 'dbName'

基本上,我尝试的所有命令都有相同的错误消息,即使是我刚刚创建的“新鲜”数据库 use 'dbName'

Some answers online said I have to create a user with proper roles, I tried this one. Still the same error message not authorized to execute command

网上的一些答案说我必须创建一个具有适当角色的用户,我尝试了这个。还是一样的错误信息not authorized to execute command

My question is the following :

我的问题如下:

Is is normal that mongod.exe doesn't show a new connection when I launch mongo.exe ? If it is correct then what can I do to make even the basic commands work ?

当我启动 mongo.exe 时 mongod.exe 不显示新连接是否正常?如果它是正确的,那么我该怎么做才能使基本命令起作用?

Additional Informations :

附加信息:

I tried to uninstall/re-install few times, with the "Custom mode" and the "Complete mode" in the Windows installer but it always lead to the same problem.

我尝试使用 Windows 安装程序中的“自定义模式”和“完整模式”卸载/重新安装几次,但它总是导致同样的问题。

I also tried to create a MongoDB Service following the official documentation but I'm not really sure if it was a good idea. (I can't add more links but it is in a section in the second link I shared.

我还尝试按照官方文档创建 MongoDB 服务,但我不确定这是否是个好主意。(我无法添加更多链接,但它位于我共享的第二个链接的一个部分中。

Edit section :

编辑部分:

I decided to try it on another computer which I have not touched for years, running on Windows 7 64-bit.

我决定在另一台我多年没有接触过的计算机上尝试它,它运行在 Windows 7 64 位上。

I copied the MongoDB installation folder at the root of this computer, created \data\db folder and launched mongod.exe.

我复制了这台计算机根目录下的 MongoDB 安装文件夹,创建了 \data\db 文件夹并启动了 mongod.exe。

Then I launched mongo.exe and this time, mongod.exe printed a message saying there is a new open connection which it doesn't on my actual computer. I think the problem is here because I was able to start the basic tutorial from the official documentation and perform simple commands like create a new db, insert, find, show dbs, etc. Everything that I am not able to do on my actual computer.

然后我启动了 mongo.exe,这一次,mongod.exe 打印了一条消息,说有一个新的开放连接,但它在我的实际计算机上没有。我认为问题出在这里,因为我能够从官方文档开始基本教程并执行简单的命令,例如创建新数据库、插入、查找、显示数据库等。我在实际计算机上无法执行的所有操作.

So I think the problem is coming from the connection between mongod.exe and mongo.exe

所以我认为问题来自 mongod.exe 和 mongo.exe 之间的连接

Do you have any idea how I could solve this problem as I have tried uninstalling few times.

你知道我如何解决这个问题,因为我已经尝试卸载了几次。

采纳答案by Liam

Copy of answer OP posted in question:

有问题的答案 OP 副本:

Solution

解决方案

After the update from the previous edit, I looked a bit about the connection between client and server and I found out that even when mongod.exe was not running, there was still somethinglistening on port 27017 with netstat -a

在从之前的编辑更新之后,我查看了客户端和服务器之间的连接,我发现即使 mongod.exe 没有运行,仍然有一些东西在侦听端口 27017netstat -a

So I tried to launch the server with a random port using

所以我尝试使用随机端口启动服务器

[dir]mongod.exe --port 2000

Then the shell with

然后外壳与

[dir]mongo.exe --port 2000

And this time, the server printed a message saying there is a new connection. I typed few commands and everything was working perfectly fine, I started the basic tutorial from the documentation to check if it was ok and for now it is.

而这一次,服务器打印了一条消息,说有一个新的连接。我输入了一些命令,一切都运行得很好,我从文档中开始了基本教程,以检查它是否正常,现在是。

回答by Yuci

You should have started the mongod instance with access control, i.e., the --auth command line option, such as:

您应该已经使用访问控制启动了 mongod 实例,即--auth 命令行选项,例如:

$ mongod --auth

Let's start the mongo shell, and create an administrator in the admin database:

让我们启动 mongo shell,并在 admin 数据库中创建一个管理员:

$ mongo
> use admin
> db.createUser(
  {
    user: "myUserAdmin",
    pwd: "abc123",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

Now if you run command "db.stats()", or "show users", you will get error "not authorized on admin to execute command..."

现在,如果您运行命令“db.stats()”或“show users”,您将收到错误“管理员无权执行命令...”

> db.stats()
{
        "ok" : 0,
        "errmsg" : "not authorized on admin to execute command { dbstats: 1.0, scale: undefined }",
        "code" : 13,
        "codeName" : "Unauthorized"
}

The reason is that you still have not granted role "read" or "readWrite" to user myUserAdmin. You can do it as below:

原因是您还没有向用户 myUserAdmin 授予“read”或“readWrite”角色。你可以这样做:

> db.auth("myUserAdmin", "abc123")
> db.grantRolesToUser("myUserAdmin", [ { role: "read", db: "admin" } ])

Now You can verify it (Command "show users" now works):

现在您可以验证它(命令“show users”现在有效):

> show users
{
        "_id" : "admin.myUserAdmin",
        "user" : "myUserAdmin",
        "db" : "admin",
        "roles" : [
                {
                        "role" : "read",
                        "db" : "admin"
                },
                {
                        "role" : "userAdminAnyDatabase",
                        "db" : "admin"
                }
        ]
}

Now if you run "db.stats()", you'll also be OK:

现在,如果你运行“db.stats()”,你也可以:

> db.stats()
{
        "db" : "admin",
        "collections" : 2,
        "views" : 0,
        "objects" : 3,
        "avgObjSize" : 151,
        "dataSize" : 453,
        "storageSize" : 65536,
        "numExtents" : 0,
        "indexes" : 3,
        "indexSize" : 81920,
        "ok" : 1
}

This user and role mechanism can be applied to any other databases in MongoDB as well, in addition to the admin database.

除了 admin 数据库之外,此用户和角色机制也可以应用于 MongoDB 中的任何其他数据库。

(MongoDB version 3.4.3)

(MongoDB 版本 3.4.3)

回答by Istiak Morsalin

Create a user like this:

创建一个这样的用户:

db.createUser(
      {
        user: "myUserAdmin",
        pwd: "abc123",
        roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
      }
    )

Then connect it following this:

然后按照以下方式连接它:

mongo --port 27017 -u "myUserAdmin" -p "abc123" --authenticationDatabase "admin"

Check the manual :

检查手册:

https://docs.mongodb.org/manual/tutorial/enable-authentication/

https://docs.mongodb.org/manual/tutorial/enable-authentication/

回答by Haisheng Yu

one more, after you create user by following cmd-1, please assign read/write/root role to the user by cmd-2. then restart mongodb by cmd "mongod --auth".

另外,按照cmd-1创建用户后,请通过cmd-2为用户分配读/写/root角色。然后通过cmd“mongod --auth”重新启动mongodb。

The benefit of assign role to the user is you can do read/write operation by mongo shell or python/java and so on, otherwise you will meet "pymongo.errors.OperationFailure: not authorized" when you try to read/write your db.

给用户分配角色的好处是你可以通过mongo shell或python/java等进行读/写操作,否则当你尝试读/写你的数据库时会遇到“pymongo.errors.OperationFailure: not authorized” .

cmd-1:

cmd-1:

use admin
db.createUser({
  user: "newUsername",
  pwd: "password",
  roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
})

cmd-2:

cmd-2:

db.grantRolesToUser('newUsername',[{ role: "root", db: "admin" }])

回答by Amit Shah

There are two things,

有两件事,

1) You can run the mongodb instance without username and password first.

1)您可以先运行没有用户名和密码的mongodb实例。

2) Then you can add the user to the system database of the mongodb which is default one using the query below.

2)然后你可以使用下面的查询将用户添加到mongodb的系统数据库中,这是默认的。

db.createUser({
  user: "myUserAdmin",
  pwd: "abc123",
  roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
})

Thanks.

谢谢。

回答by Mehmet N. Yarar

It was Docker running in the background in my case. If you have Docker installed, you may wanna close it and try again.

就我而言,它是在后台运行的 Docker。如果您安装了 Docker,您可能想关闭它并重试。

回答by Francisco Valadares

for me it worked by adding

对我来说,它通过添加

1) "You can run the mongodb instance without username and password first.---OK

1)“你可以先运行没有用户名和密码的mongodb实例。---OK

2) "Then you can add the user to the system database of the mongodb which is default one using the query below".---OK

2)“然后你可以使用下面的查询将用户添加到mongodb的系统数据库中”。---OK

db.createUser({
  user: "myUserAdmin",
  pwd: "abc123",
  roles: [ { role: "userAdminAnyDatabase", db: "admin" } ],
  mechanisms:[ "SCRAM-SHA-1" ] // I added this line

})