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
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)
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"]})
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
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"}
]
})
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.
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.