When creating first admin user on mongdb cluster getting error “couldn't add user: not authorized on admin to execute command”

后端 未结 6 882
既然无缘
既然无缘 2021-02-04 19:10

I am using mongoDB Cluster with version 3.4 in google cloud compute engine, actually past week my database got attacked by hackers that\'s why i thought about using authorizatio

相关标签:
6条回答
  • 2021-02-04 19:48

    johnlowvale's answer is correct, but

    keyFile implies security.authorization.
    

    source: https://docs.mongodb.com/manual/reference/configuration-options/#security.keyFile

    You have to disable authorization AND the keyFile.

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

    (insufficient rep or I'd have just commented this on johnlowvale's answer)

    0 讨论(0)
  • 2021-02-04 19:49

    edit vim /lib/systemd/system/mongod.service

    remove --auth 
    restart
    
    #ExecStart=/usr/bin/mongod --quiet --auth   --config /etc/mongod.conf
    ExecStart=/usr/bin/mongod --quiet  --config /etc/mongod.conf
    use admin
    db.createUser({user:"RootAdmin",pwd:"blahblah",roles:["root"]})
    
    0 讨论(0)
  • 2021-02-04 19:55

    To be able to create a new user, you need to first disable security in /etc/mongod.conf

    // security: // authorization: enabled Then restart Mongodb server sudo service mongo restart

    After this you can add the user and role that you want from the shell.

    db.createUser({ user: 'test_user', pwd: 'test', roles: [ { role: "userAdmin", db: "test" }, { role: "dbAdmin", db: "test" }, { role: "readWrite", db: "test" } ] })

    To enable authenticated connection Uncomment the line again in /etc/mongod.conf

    security: authorization: enabled and restart the server again

    0 讨论(0)
  • 2021-02-04 20:03

    Once you are connected to this first node, you can initiate the replica set with rs.initiate(). Again, this command must be run from the same host as the mongod to use the localhost exception.

    We can create our admin user with the following commands:

    rs.initiate()
    use admin
    db.createUser({
      user: "admin",
      pwd: "pass",
      roles: [
        {role: "root", db: "admin"}
      ]
    })
    
    0 讨论(0)
  • 2021-02-04 20:09

    You have to change your mongod.conf file to disable authorization before creating such admin user

    security:
      authorization: disabled
    

    After that, restart the mongod service and open mongodb shell to create the admin user

    use admin
    db.createUser({user:"RootAdmin",pwd:"blahblah",roles:["root"]})
    

    Remember to enable authorization back on after creating user.

    0 讨论(0)
  • 2021-02-04 20:14

    When a new database is setup with authorisation/security enabled but no users set up, you can only connect to it from the localhost. In your config file you should have bind ip set to 127.0.0.1 I think in order to make sure you connect to it with the correct authorisation to create new users.

    This is what it says in Mongo course M103

    By default, a mongod that enforces authentication but has no configured users only allows connections through the localhost.

    0 讨论(0)
提交回复
热议问题