Authentication
It’s dangerous to deploy a DB server without authentication. People could just access your DB by knowing where you host your DB. At least we should create a username and password to do the basic authentication.
create root user
I would like to take a quick exercise about authentication, so let’s cut the shit about different kinds of users and permission.
In the mongo shell, create a root user to handle everything as follows.
$ use admin
$ db.createUser( { user: “superuser”, pwd: “yourpwd”, roles: [ { role: “root”, db: “admin” } ] } )
mongod.cfg
Enable the authorization in the MongoDB config as follows.
connection
Let’s use pymongo as the driver in python back-end for example.
'mongodb://'+db_root_user+':'+db_root_password+'@'+db_host+':'+str(db_port)+'?authSource=admin' would pass the username and password by url.
Replication
connect DB in the back-end
To use both authentication and replication techniques, we would use pymongo as follows.
Set the db_host as the combination of the hosts of primary servers and secondary servers. Then we can use the both authentication and replication technique right now.