Configure MongoDB server with authentication and replica set.
-
Install all the required applications. Make sure to check if environment variables are properly setup/installed to run
mongod
,mongosh
andopenssl
from command-line. -
Now lets enable replica set.
Open
mongod.cfg
file in your favorite text editor and edit the replica set sectionreplication: replSetName: "[name]"
Note: You may set any name for
replSetName
property.Restart MongoDB server.
Run a termimal and type following command to enable replica set.
#run mongo shell without authentication mongosh #to initiate replica set rs.initiate()
-
Create key file. This key file is use to allow authentication with replica set.
Suppose you want to create the key to "C:\keyfile" directory with "key.pem" key file name, run the following commands:
mkdir C:\keyfile openssl rand -base64 756 > C:\keyfile\key.pem icacls.exe C:\keyfile\key.pem /reset icacls.exe C:\keyfile\key.pem /GRANT:R "$($env:USERNAME):(R)" icacls.exe C:\keyfile\key.pem /inheritance:r
Note: You may change filename and file path location as much as you like.
-
Run
mongo shell
to add new user with admin previlege with the following command:mongosh use admin db.createUser( { user:'[user]', pwd:'[password]', roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ] } )
Note: Change user and password as needed.
To check newly added user, run the following commands:
use admin db.getUsers()
To add user for specific database, run the following command:
use [database] db.createUser( { user:'[user]', pwd:'[password]', roles: [ { role: "readWrite", db: '[database]'} ] } )
-
Now its time to enable authentication.
Copy the key file from Step 3 to where mongod.exe is located i.e.
C:\Program Files\MongoDB\Server\[version]\bin
.Open
mongod.cfg
using text editor with admin previledge and update the security section:security: authorization: enabled keyFile: C:\Program Files\MongoDB\Server\[version]\bin\key.pem
-
If you install MongoDB as a service without authentication, remove the existing service by running this command
mongod --remove
-
Then it is necessary to create a new service for MongoDB
mongod --journal --config "C:\Program Files\MongoDB\Server\[version]\bin\mongod.cfg" --dbpath "C:\Program Files\MongoDB\Server\[version]\data" --auth --install
-
Make sure MongoDB service is running
-
Once MongoDB service is up running, open a terminal and run
mongo shell
with the following commands:#Supply user and password setup on step 6. mongosh -u [user] #Then enter user password
-
Congratulations!!! You have successfully configure MongoDB Server 🎉👏👏👏👏
Make sure you install MongoDb Database Tools
-
Backup MongoDB database
To back MongoDB database use the following command:
#To create directory mkdir "C:\back-up" cd "C:\back-up" #To backup database mongodump -u [user] --authenticationDatabase admin --db [database name] #Then enter user password
You can also specified output location with "
--out=[output folder path]
".Note: When output directory is not specified backup database will be created in "
.\dump\[database name]
" folder. -
Restore MongoDB database
To restore MongoDB database use the following command
mongorestore -u [user] --authenticationDatabase admin --db [database name] [database folder path] #Then enter user password
This is created for personal use. In no event shall the author be liable for any claim, damages or other liability in connection with this instruction. Use this at your OWN RISK.