codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🍃

MongoDB

61 / 65 topics
58Cloud Deployments59AWS MongoDB60Azure Cosmos DB61Google Cloud Platform62MongoDB Atlas63On-Premises Deployments64High Availability Strategies65Disaster Recovery Plans
Tutorials/MongoDB/Google Cloud Platform
🍃MongoDB

Google Cloud Platform

Updated 2026-04-20
4 min read

Google Cloud Platform for MongoDB Deployment

Introduction

Google Cloud Platform (GCP) offers a robust set of tools and services that can be leveraged for deploying and hosting MongoDB databases. This tutorial will walk you through the process of setting up a MongoDB deployment on GCP, including creating a virtual machine, installing MongoDB, configuring security settings, and optimizing performance.

Prerequisites

Before you begin, ensure you have the following:

  • A Google Cloud Platform account.
  • The Google Cloud SDK installed on your local machine.
  • Basic knowledge of MongoDB and cloud computing concepts.

Step 1: Setting Up Your GCP Environment

Create a Project

  1. Log in to the Google Cloud Console: https://console.cloud.google.com/
  2. Create a New Project:
    • Click on the project dropdown at the top of the page.
    • Select "New Project."
    • Enter a name for your project and click "Create."

Enable Billing

Ensure that billing is enabled for your project to use GCP services.

  1. Navigate to the "Billing" section in the Google Cloud Console.
  2. Click on "Enable Billing."
  3. Follow the prompts to link your payment method.

Step 2: Creating a Virtual Machine Instance

Launch a VM Instance

  1. Navigate to Compute Engine:
    • In the Google Cloud Console, go to "Compute Engine" > "VM instances."
  2. Create an Instance:
    • Click on "Create Instance."
  3. Configure the Instance:
    • Name: Enter a name for your instance (e.g., mongodb-instance).
    • Region and Zone: Choose a region and zone close to your users or where you plan to deploy other services.
    • Machine Type: Select an appropriate machine type based on your performance requirements. For MongoDB, consider using a machine with at least 4 vCPUs and 15 GB of RAM.
    • Boot Disk: Choose a boot disk image (e.g., Ubuntu 20.04 LTS).
    • Firewall: Allow HTTP traffic (port 80) and SSH access (port 22).

Start the Instance

After configuring your instance, click "Create" to start it. Once the instance is running, note its external IP address.

Step 3: Installing MongoDB on the VM

  1. SSH into Your Instance:

    • Use the Google Cloud Console or a terminal with the gcloud command-line tool.
    gcloud compute ssh mongodb-instance
    
  2. Import the Public Key for MongoDB:

    wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
    
  3. Create a List File for MongoDB:

    echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
    
  4. Reload Local Package Database:

    sudo apt-get update
    
  5. Install MongoDB:

    sudo apt-get install -y mongodb-org
    
  6. Start and Enable MongoDB Service:

    sudo systemctl start mongod
    sudo systemctl enable mongod
    

Step 4: Configuring Security Settings

Configure Firewall Rules

  1. Allow MongoDB Port (27017):
    • In the Google Cloud Console, navigate to "VPC network" > "Firewall."
    • Click on "Create Firewall Rule."
    • Enter a name for the rule (e.g., allow-mongodb).
    • Set the target to "All instances in the network."
    • Add a new rule with IP ranges set to 0.0.0.0/0 and specify port 27017.

Secure MongoDB

  1. Edit MongoDB Configuration:

    sudo nano /etc/mongod.conf
    
  2. Bind to Localhost:

    • Find the net section and set bindIp to 127.0.0.1.
    net:
      bindIp: 127.0.0.1
    
  3. Enable Authentication:

    • Under the security section, enable authentication.
    security:
      authorization: enabled
    
  4. Restart MongoDB Service:

    sudo systemctl restart mongod
    
  5. Create an Admin User:

    mongo
    use admin
    db.createUser({
      user: "admin",
      pwd: "yourStrongPassword",
      roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
    })
    exit
    

Step 5: Connecting to MongoDB

  1. Connect Using the Admin User:

    mongo -u admin -p --authenticationDatabase admin
    
  2. Create a New Database and Collection:

    use mydatabase
    db.createCollection("mycollection")
    
  3. Insert Data:

    db.mycollection.insertOne({ name: "John Doe", age: 30 })
    
  4. Query Data:

    db.mycollection.find().pretty()
    

Step 6: Optimizing Performance

Monitor Resource Usage

  1. Use Google Cloud Monitoring:

    • Navigate to "Operations" > "Monitoring."
    • Set up dashboards to monitor CPU, memory, and disk usage.
  2. Optimize MongoDB Configuration:

    • Adjust parameters like wiredTigerCacheSizeGB in the /etc/mongod.conf file based on your instance's RAM.
    storage:
      wiredTiger:
        engineConfig:
          cacheSizeGB: 8
    

Backup and Recovery

  1. Enable MongoDB Backups:

    • Use Google Cloud's managed database services like Cloud SQL or third-party solutions for automated backups.
  2. Regularly Test Restore Procedures:

    • Ensure you can restore your database from backups to prevent data loss.

Conclusion

Deploying MongoDB on Google Cloud Platform provides a scalable and secure environment for hosting your databases. By following the steps outlined in this tutorial, you can set up a production-ready MongoDB instance with proper security configurations and performance optimizations. Remember to regularly monitor your resources and maintain backups to ensure high availability and reliability of your database.

Additional Resources

  • Google Cloud Compute Engine Documentation
  • MongoDB Official Documentation
  • Setting Up MongoDB on Google Cloud

PreviousAzure Cosmos DBNext MongoDB Atlas

Recommended Gear

Azure Cosmos DBMongoDB Atlas