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.
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” } ] } )
Enable the authorization in the MongoDB config as follows.
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.
I wrote an article about MongoDB replication before. Please check it in this url: https://medium.com/@xdxxxx4713/mongodb-replication-45ac3e6d6b30
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.