This guide will run you through setting up MongoDB on gcloud, and connect to the database instance remotely.
MongoDB Compute Engine Tutorial
From the first link, 'MongoDB on Compute Engine'...
- Click 'Launch on Compute Engine'
- It should bring you to a page with 'New MongoDB deployment'
- Zone: us-west1-x
Servers Tier
- Instances Count: 2
- Machine type: small (1 shared vCPU)
- Data disk size in GB (20)
Arbiters Tier
- Instances Count: 1
- Machine type: small (1 shared vCPU)`
Wait for it to deploy... (You should get an e-mail when it is complete)
- SSH connect to any node and run
mongo
;rs.status()
to check the replica staus - Power off all machines when you are done
- In GCP, expand the hanburger menu
- Under Networking, VPC network > Firewall rules
- Create firewall rule
- Enter some name, like allow-psu
- Direction of traffic > ingress
- Targets > All instances in the network
- Source filter > IP range
- For PSU, enter 131.252.0.0/16 (16 is the CIDR subnet mask for 255.255.0.0, 24 is for 255.255.255.0)
- Else, go to https://who.is/ to see your public IP
- Protocols and ports > Specified protocols and ports
- In the box, enter: tcp:27017
- Save
- Download Visual Studios
- Create a new Project; Console App (.NET Core)
- Once created, go to Project > Manage NuGet packages
- Install MongoDB.Driver and MongoDB.Bson
- Install CsvHelper
- Copy the code from Program.cs
- Change the MongoClient connection string to use your primary node's external IP
- Must have a majority of the instances running, so that they can hold an election & elect a primary
- I just run one data-bearing instance and the arbiter node
- External IP is on Compute Engine > VM Instances
- You can verify if the instance is primary or secondary via gcloud ssh
- Be sure to copy the listings.csv file to the project folder
- I created a 'data' folder in the project root dir
- The program executes from rootdir/bin/Debug/netcoreapp3.0
- At the very least, be sure to change 'src' var to match the .csv file for listings
- Run