MongoDB 3.2 authentication failed

前端 未结 4 1600
萌比男神i
萌比男神i 2020-11-29 02:06

I create a user with the following set of commands. This should create user in both admin db as well as my target db (c2d):

# mongo 127         


        
4条回答
  •  自闭症患者
    2020-11-29 02:47

    If you log in through shell, make sure your create user under db "admin", NOT under a customized db. In your case you switched to "c2d".

    Here is what I have tried (Log in as "admin")

    1. This one will work:

    $ mongo -u admin -p --authenticationDatabase "admin"
    > use admin
    > db.createUser(
      {
        user: "user007",
        pwd: "YourP@ssw0rd",
        roles: [
           { role: "readWrite", db: "yourdb" },
        ]
      }
    )
    

    Output 1:

    root@mongo-server:/# mongo -u admin -p --authenticationDatabase "admin"
    MongoDB shell version v4.0.6
    Enter password:
    connecting to: mongodb://127.0.0.1:27017/?authSource=admin&gssapiServiceName=mongodb
    MongoDB server version: 4.0.6
    ----
    > use admin
    switched to db admin
    > db.createUser(
    ...   {
    ...     user: "user007",
    ...     pwd: "YourP@ssw0rd",
    ...     roles: [
    ...        { role: "readWrite", db: "yourdb" },
    ...     ]
    ...   }
    ... )
    Successfully added user: {
            "user" : "user007",
            "roles" : [
                    {
                            "role" : "readWrite",
                            "db" : "yourdb"
                    }
            ]
    }
    
    root@mongo-server:/# mongo -u user007 -p YourP@ssw0rd
    
    connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
    Implicit session: session { "id" : UUID("ceabf440-c584-4518-90f5-cc845eaec3b3") }
    MongoDB server version: 4.0.6
    ---
    >
    

    2. This one will fail:

    $ mongo -u admin -p --authenticationDatabase "admin"
    > use yourdb
    > db.createUser(
      {
        user: "user007",
        pwd: "YourP@ssw0rd",
        roles: [
           { role: "readWrite", db: "yourdb" },
        ]
      }
    )
    

    Output 2:

    root@mongo-server:/# mongo -u admin -p --authenticationDatabase "admin"
    MongoDB shell version v4.0.6
    Enter password:
    connecting to: mongodb://127.0.0.1:27017/?authSource=admin&gssapiServiceName=mongodb
    MongoDB server version: 4.0.6
    ----
    > use yourdb
    switched to db yourdb
    > db.createUser(
    ...   {
    ...     user: "user007",
    ...     pwd: "YourP@ssw0rd",
    ...     roles: [
    ...        { role: "readWrite", db: "yourdb" },
    ...     ]
    ...   }
    ... )
    Successfully added user: {
            "user" : "user007",
            "roles" : [
                    {
                            "role" : "readWrite",
                            "db" : "yourdb"
                    }
            ]
    }
    >
    
    root@mongo-server:/# mongo -u user007 -p YourP@ssw0rd
    MongoDB shell version v4.0.6
    connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
    2019-12-06T04:28:34.630+0800 E QUERY    [js] Error: Authentication failed. :
    connect@src/mongo/shell/mongo.js:343:13
    @(connect):1:6
    exception: connect failed
    

提交回复
热议问题