google-templatesget-started-mongodb-replication

Get Started with MongoDB Replication using GCP Marketplace

Introduction

MongoDB is an open source, document-oriented scalable NoSQL database designed for high performance and scalability. The Bitnami MongoDB template for GCP Marketplace allows you to create a MongoDB replica set that provides redundancy and high availability in production environments.

This guide describes how to configure and use the Bitnami MongoDB template with GCP Marketplace.

NOTE: The steps below assume that you already have a Google Account and project configured for use with Google Compute Engine.

Step 1: Configure MongoDB

In a MongoDB replica set, copies of the MongoDB database are maintained on separate server nodes. Spreading the data across multiple nodes significantly reduces the risk of application failure and data loss in the event of a hardware failure.

A MongoDB replica set consists of two types of nodes:

  • Server nodes: Each server node hosts its own copy of the MongoDB data. One node is designated as the primary node and receives all write operations, while other nodes are designated as secondary nodes and replicate the operations performed by the primary node on their own copies of the data set. If a primary node fails, one of the secondary nodes will become the new primary after an internal “election”.

  • Arbiter nodes: Arbiter nodes do not host any data. Their function is to provide an additional vote in replica set elections.

NOTE: In a replica set, you can have one primary node, zero or more secondary nodes, and zero or one arbiter node. The total number of server nodes in a replica set cannot be higher than 6 (1 primary and 5 secondaries). You can also add an optional extra one as arbiter.

To use the Bitnami Multi-VM MongoDB template, launch it from the GCP Marketplace and you will be presented with a configuration screen like the one below:

Template configuration

Configure the template using the information below:

  • Deployment name: Enter a unique identifier for the deployment.
  • Zone: Select the geographic region of the data center hosting the deployment.
  • Machine and disk type: Select the CPU, RAM and disk type appropriate to your requirements.
  • Disk size in GB: Select storage capacity after carefully considering your data storage requirements.
  • MongoDB server nodes: Select an odd number of server nodes or, using an even number of nodes, add an arbiter node
  • MongoDB arbiter nodes: Add an arbiter node to ensure the total number of nodes in the replica set is an odd number

NOTE: Ensuring an odd number of total nodes avoids the risk of tied elections if a primary node becomes unavailable.

For more information on MongoDB replica sets, refer to the official documentation.

Step 2: Configure SSH access

The GCP Marketplace requires the user to manually add a public SSH key using the server administration page. It then uses the user@hostname comment at the end of the public SSH key to decide which user account on the server should be associated with the key.

The steps below walk you through the process of configuring the MongoDB server for use with an external SSH client. It assumes that you will be logging in as the bitnami user. The same process can be followed for other users and server nodes as well.

Follow the steps:

  • Prepare an SSH key pair for use.
  • Log in to the Google Cloud Console and select your project.
  • Navigate to the “Compute Engine -> VM Instances” page and select the MongoDB server node you wish to connect to. Note the public IP address of the instance.

    SSH configuration

  • Click the “Edit” link in the top control bar.

  • On the resulting page, copy and paste your public SSH key into the “SSH Keys” field.

  • Update the user@hostname comment at the end of the SSH key content to bitnami. This will associate the SSH key with the bitnami user account that is already present on the server. The “Username” next to the form field will update accordingly.

    SSH configuration

  • Add more keys as needed by clicking the “Add Item” button. Once done, save the changes by clicking the “Save” button.

You should now be able to use any third-party SSH client and the corresponding private SSH key to log in to the server as the bitnami user.

SSH configuration

Step 3: Connect and create a MongoDB database

At the server console, connect to MongoDB using a command like the one below.

$ mongo --host SERVER-IP --port 27017

This should launch the MongoDB shell.

Here are the basic commands to create a database for your application using the MongoDB shell. Note that this example creates a database name mydb and a user named myuser with password mypassword. Replace these example values with values suitable for your intended use when executing the commands below.

MongoDB shell version: 3.2.7
connecting to: 127.0.0.1:27017/admin
> db = db.getSiblingDB('mydb')
mydb
> db.createUser( { user: "myuser", pwd: "mypassword", roles: [ "readWrite", "dbAdmin" ]} )
Successfully added user: { "user" : "myuser", "roles" : [ "readWrite", "dbAdmin" ] }
> exit

Some applications may require specific privileges in the database. Consult the official installation steps in the application documentation.

To learn more, refer to the official MongoDB documentation.