mongodb 错误:无法添加用户:未授权测试执行命令 { createUser:

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27784956/
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:15:08  来源:igfitidea点击:

Error: couldn't add user: not authorized on test to execute command { createUser:

mongodb

提问by brb

I'm starting with MongoDB and I would like to user/pass access to the dbs. The first thing I did was to create and admin user and start mongodb with auth activate, this is my created user:

我从 MongoDB 开始,我想用户/传递对 dbs 的访问权限。我做的第一件事是创建和管理用户并使用 auth activate 启动 mongodb,这是我创建的用户:

db.getUser("admin") 
{
        "_id" : "admin.admin",
        "user" : "admin",
        "db" : "admin",
        "roles" : [
                {
                        "role" : "dbAdminAnyDatabase",
                        "db" : "admin"
                },
                {
                        "role" : "clusterAdmin",
                        "db" : "admin"
                }
        ] }
}

After that, I try to create users with the following commands:

之后,我尝试使用以下命令创建用户:

use newdb
db.createUser(
  {
    user: "newuser",
    pwd: "12345678",
    roles: [ { role: "readWrite", db: "newdb" } ]
  }
)

But I got this error:

但我收到了这个错误:

Error: couldn't add user: not authorized on newdb to execute command { createUser: "newuser", pwd: "xxx", roles: [ { role: "readWrite", db: "newdb" } ], digestPassword: false, writeConcern: { w: "majority", wtimeout: 30000.0 } } at src/mongo/shell/db.js:1004

错误:无法添加用户:未授权在 newdb 上执行命令 { createUser: "newuser", pwd: "xxx", roles: [ { role: "readWrite", db: "newdb" } ],digestPassword: false, writeConcern: { w: "majority", wtimeout: 30000.0 } } 在 src/mongo/shell/db.js:1004

I have googled about it but there are only two results, one on Chinese and the other not related, so I'm a little lost. Any idea ? It looks like my admin user doesn't have privilege, I access to the mongodb with the following command:

我用谷歌搜索过,但只有两个结果,一个是中文的,另一个没有相关性,所以我有点迷茫。任何的想法 ?看起来我的管理员用户没有权限,我使用以下命令访问 mongodb:

$mongo --port 27017 -u adminUser -p adminPass  --authenticationDatabase admin

Thanks in advance.

提前致谢。

回答by Kanagavelu Sugumar

Stop the running instance of mongod mongod --config /usr/local/etc/mongod.conf --auth

停止正在运行的 mongod 实例 mongod --config /usr/local/etc/mongod.conf --auth

then start without --authlike mongod --config /usr/local/etc/mongod.conf

然后开始不--auth喜欢 mongod --config /usr/local/etc/mongod.conf

then update your roles like

然后更新你的角色,比如

db.updateUser("admin",{roles : ["userAdminAnyDatabase","userAdmin","readWrite","dbAdmin","clusterAdmin","readWriteAnyDatabase","dbAdminAnyDatabase"]});

db.updateUser("admin",{roles : ["userAdminAnyDatabase","userAdmin","re​​adWrite","dbAdmin","clusterAdmin","re​​adWriteAnyDatabase","dbAdminAnyDatabase"]});

Now again start mongod with --authand try.

现在再次启动 mongod--auth并尝试。



The idea is you have to create user under "admin" database with all the ROLES mentioned above.

这个想法是你必须在“admin”数据库下创建具有上述所有角色的用户。

But this user is not part of other databases. So once you created the admin user,

但该用户不属于其他数据库。所以一旦你创建了管理员用户,

then login as that admin user,

然后以该管理员用户身份登录,

SERVER: mongod --config /usr/local/etc/mongod.conf --auth

CLIENT: ./mongodb/bin/mongo localhost:27017/admin -u adminUser -p 123456

服务器:mongod --config /usr/local/etc/mongod.conf --auth

客户端:./mongodb/bin/mongo 本地主机:27017/admin -u adminUser -p 123456

use APPLICATION_DB

again create a new user (say APP_USER) for this APPLICATION_DB. This time for this application user you need only "dbOwner" role.

再次为这个 APPLICATION_DB 创建一个新用户(比如 APP_USER)。这次对于这个应用程序用户,您只需要“dbOwner”角色。

db.createUser({user:"test", pwd:"test", roles:['dbOwner']})

Now your application has to use this APP_USER and APP_DB.

现在您的应用程序必须使用这个 APP_USER 和 APP_DB。

回答by Adithya

Below command says, you have created admin user and password.

下面的命令说,您已经创建了管理员用户和密码。

db.getUser("admin")      
{    
        "_id" : "admin.admin",   
        "user" : "admin",    
        "db" : "admin",    
        "roles" : [    
                {    
                        "role" : "dbAdminAnyDatabase",    
                        "db" : "admin"    
                },    
                {    
                        "role" : "clusterAdmin",    
                        "db" : "admin"    
                }    
        ] }    
}    

Once you create the admin user and password, Mongo will not allow you to do any schema update as normal a admin user which you used to login previously.

创建管理员用户和密码后,Mongo 将不允许您像以前登录的普通管理员用户一样进行任何架构更新。

Use newly created admin user and password to create new users or do any updates. It should work.

使用新创建的管理员用户和密码来创建新用户或进行任何更新。它应该工作。

回答by Toan Le

You should exit the current mongod process and restart it again without parameter --auth.

您应该退出当前的 mongod 进程并在没有参数的情况下重新启动它--auth

On Terminal type: mongod

在终端类型上: mongod

After the process of mongod gets started then you can open new terminal tab to run mongo shell command :

mongod 进程启动后,您可以打开新的终端选项卡来运行 mongo shell 命令:

mongo --shell
use new_your_database
db.createUser(
   {
     user: "mongo_admin",
     pwd: "mongo_admin",
     roles: [ "readWrite", "dbAdmin" ]
   }
)

it should be done successfully....

它应该成功完成......

回答by Bala

I fixed the same problem just exit and login as admin

我解决了同样的问题,只是退出并以管理员身份登录

mongo -u admin

enter your admin user password

after login, create a user as usual you mentioned

输入您的管理员用户密码

登录后,像往常一样创建一个用户

use newdb
db.createUser(
  {
    user: "newuser",
    pwd: "12345678",
    roles: [ { role: "readWrite", db: "newdb" } ]
  }
)

回答by whoami

If you're here with the same issue & having a config file to trigger mongod instances- then go ahead and exit from the running process of mongod & in your config file make sure you make security.authorization : disabled, in addition if you've keyFile (i.e; path to a cert file), then comment that spec as well, now start you mongod process using that config file - you should be able to create an user using admin db.

如果你在这里遇到同样的问题并且有一个配置文件来触发 mongod 实例 - 然后继续并退出 mongod 的运行过程 & 在你的配置文件中确保你使 security.authorization : disabled,另外如果你' ve keyFile(即证书文件的路径),然后对该规范进行评论,现在使用该配置文件启动 mongod 进程 - 您应该能够使用 admin db 创建用户。

If you work on replica set : Once if you're done with creating a primary node + a user, make sure you initiate & connect to replica set (which again internally connects to primary node),here you can do anything with earlier created user, but when you connect directly to primary node again to execute few commands like rs.initiate()/rs.status()/show dbs/show users, you would end-up getting errors like (there are no users authenticated).

如果您在副本集上工作:一旦完成创建主节点 + 用户,请确保启动并连接到副本集(再次在内部连接到主节点),在这里您可以对先前创建的用户执行任何操作,但是当您再次直接连接到主节点以执行像rs.initiate()/rs.status()/show dbs/show users这样的几个命令时,您最终会收到类似的错误(没有用户通过身份验证)。

security:
  authorization: disabled
# keyFile: /opt/mongodb/keyfile