Install & Secure MongoDB on Google Cloud Platform
Hey Database Explorers!
Here I am about to explain how to install MongoDB and secure MongoDB on Google Cloud Platform. You can follow the same method on any virtual machine provider who allows you to start Ubuntu machines.
Let’s start installing MongoDB as we have successfully spin up our GCP Instance. Login to your instance and follow below commands to install mongodb.
Login to your GCP Cloud Console here. After successful login, Go to your navigation panel and then :
Compute Engine > VM Instances
*You may have to enable billing here.
Once your VM Instances screen is there. Let’s spin up a machine for you.
Here are the configuration I have used, you can use as per your convenience :
- Appropriate VM Name
- Properly tag your VM
- Basic memory required is 2 GB, so you can go ahead and select your instance type.
- OS type – ubuntu, version – 18.04 LTS
- Allow HTTP & HTTPS Traffic (conditional)
Click on “Create“
That’s it you are good to go.
First, update the packages list to have the most recent version of the repository listings:
Let’s install mongodb package now:
I hope MongoDB installation went well, if not check for errors and just google it. Otherwise let’s check for the status of MongoDB.
Status should be Active (Running). Check below image for reference.

To check if you can Run your mongo service, just type following command in your terminal:
Just check how many databases are there by default in mongo console.
Let’s create a first user in your mongodb “admin” database.
db.createUser({
user: “jimish”,
pwd: “Pa$$Tutorial”,
roles: [ { role: “userAdminAnyDatabase”, db: “admin” } ]
})
You should receive successfully created a user message. You can now logout from the mongo console. We will go ahead and secure your mongo service.
Traverse through your file management. open /etc/mongodb.conf in editable format. (via vim or vi or nano)
port = 27017
auth = true
Please restart your mongodb service by following command
Try to login without providing information about credentials to mongodb
mongo
Let’s login to your secure mongodb now.
This will ask for password prompt. Just enter your password and play around.
Happy Coding!!
Useful links :
Open firewalls in GCP: https://cloud.google.com/vpc/docs/using-firewalls
Login to GCP via Browser: https://cloud.google.com/compute/docs/ssh-in-browser?hl=en#copypaste
(1) Comment