This manual will go over setting up MongoDB manually in GCP. A lot of this information comes from the following two websites:
https://www.linode.com/docs/databases/mongodb/build-database-clusters-with-mongodb/
https://www.bmc.com/blogs/how-to-setup-mongodb-cluster/
However, this manual will cover how to do it specifically in GCP as simply as possible without using any serverless options.
The MongoDB setup will look like this by the end of the tutorial, but you should be able to configure it however you like by the end of this.
Before anything else, we're going to want to create firewall rules to allow the following ports 27017, 27018, 27019
Note - you can read more about why we are creating firewall rules for these ports here: https://docs.mongodb.com/manual/reference/default-mongodb-port/
to communicate with our soon to be created VM instances.
IMPORTANT UPDATE: In the images below (for each port), you will notice that the source IP range is 0.0.0.0/0 which is the entire internet. Do not enter this! This is an easy way to get hacked as hackers often scan the internet for such vulnerabilities. Instead, use your own IP address and the IP addresses of your teammates who are working on the same database.
27017
27018
27019
Menu
🡪 Compute Engine
🡪 VM Instance
This represents the setup from the image in Part 1 of this codelab, but, as mentioned before, you should be able to configure the installation differently. (You just need to have a router and config server; the number of shard sets is up to you.)
mongo-config-1
. The next two VM instances I will name are mongo-config-2
and mongo-config-3
respectively. wget https://repo.mongodb.org/apt/ubuntu/dists/xenial/mongodb org/4.0/multiverse/binary-amd64/mongodb-org-server_4.0.5_amd64.deb
sudo dpkg -i mongodb-org-server_4.0.5_amd64.deb
wget https://repo.mongodb.org/apt/ubuntu/dists/xenial/mongodb org/4.0/multiverse/binary-amd64/mongodb-org-shell_4.0.5_amd64.deb
sudo dpkg -i mongodb-org-shell_4.0.5_amd64.deb
wget https://repo.mongodb.org/apt/ubuntu/dists/xenial/mongodb org/4.0/multiverse/binary-amd64/mongodb-org-mongos_4.0.5_amd64.deb
sudo dpkg -i mongodb-org-mongos_4.0.5_amd64.deb
sudo vim /etc/mongodConfig.conf
storage: dbPath: /var/lib/mongodb journal: enabled: true systemLog: destination: file logAppend: true path: /var/log/mongodb/mongodConfig.log net: port: 27019 bindIp: 172.31.46.15 sharding: clusterRole: configsvr replication: replSetName: ConfigReplSet
sudo mongod --config /etc/mongodConfig.conf&
mongo 10.138.0.5:27019
rs.initiate( { _id: "ConfigReplSet", configsvr: true, members: [ { _id: 0, host: "10.138.0.5:27019" }, { _id: 1, host: "10.180.0.2:27019" }, { _id: 2, host: "10.128.0.4:27019" } ] } )
mongo-query-router
) and populate it with the template that we created. Then SSH into it. wget https://repo.mongodb.org/apt/ubuntu/dists/xenial/mongodb org/4.0/multiverse/binary-amd64/mongodb-org-server_4.0.5_amd64.deb
sudo dpkg -i mongodb-org-server_4.0.5_amd64.deb
wget https://repo.mongodb.org/apt/ubuntu/dists/xenial/mongodb org/4.0/multiverse/binary-amd64/mongodb-org-shell_4.0.5_amd64.deb
sudo dpkg -i mongodb-org-shell_4.0.5_amd64.deb
wget https://repo.mongodb.org/apt/ubuntu/dists/xenial/mongodb org/4.0/multiverse/binary-amd64/mongodb-org-mongos_4.0.5_amd64.deb
sudo dpkg -i mongodb-org-mongos_4.0.5_amd64.deb
sudo vim /etc/mongoRouter.conf
systemLog: destination: file logAppend: true path: /var/log/mongodb/mongoRouter.log net: port: 27017 bindIp: 10.128.0.8 sharding: configDB: ConfigReplSet/10.128.0.5:27019,10.128.0.6:27019,10.128.0.7:27019
sudo mongos --config /etc/mongoRouter.conf&
mongo 10.128.0.8:27017
mongo-shard-1-1
, mongo-shard-1-2
, mongo shard-1-3
. It should look like the following below: wget https://repo.mongodb.org/apt/ubuntu/dists/xenial/mongodb org/4.0/multiverse/binary-amd64/mongodb-org-server_4.0.5_amd64.deb
sudo dpkg -I mongodb-org-server_4.0.5_amd64.deb
wget https://repo.mongodb.org/apt/ubuntu/dists/xenial/mongodb org/4.0/multiverse/binary-amd64/mongodb-org-shell_4.0.5_amd64.deb
sudo dpkg -I mongodb-org-shell_4.0.5_amd64.deb
wget https://repo.mongodb.org/apt/ubuntu/dists/xenial/mongodb org/4.0/multiverse/binary-amd64/mongodb-org-mongos_4.0.5_amd64.deb
sudo dpkg -i mongodb-org-mongos_4.0.5_amd64.deb
sudo vim /etc/mongodShard.conf
storage: dbPath: /var/lib/mongodb journal: enabled: true systemLog: destination: file logAppend: true path: /var/log/mongodb/mongodShard.log net: port: 27018 bindIp: 10.128.0.9 sharding: clusterRole: shardsvr replication: replSetName: ShardReplSet1
sudo mongod –config /etc/mongodShard.conf&
mongo 10.128.0.9:27018
rs.initiate( { _id: "ShardReplSet1", version: 1, members: [ { _id: 0, host: "10.128.0.9:27018" }, { _id: 1, host: "10.168.0.2:27018" }, { _id: 2, host: "10.138.0.6:27018" } ] } )
After executing the above command, the response should look similar to the below screenshot.
mongo 10.128.0.8:27017
sh.addShard( "ShardReplSet1/10.128.0.9:27018")
sh.addShard( "ShardReplSet1/10.168.0.2:27018")
sh.addShard( "ShardReplSet1/10.138.0.6:27018")
After executing the above command, the response should look similar to the below screenshot.
mongo-shard-2-1
, mongo-shard-2-2
, mongo-shard-2-3
. Finally, all instances are created and configured properly.This codelab should help get you started on creating and configuring your MongoDB in GCP. There are a few things to mention. We didn't add any authentication to the system. If you open up your firewall to all IP addresses, anyone with your external IP address will be able to connect to your database (and there are bots out there crawling IP addresses looking for MongoDB installs. Adding authentication is pretty straightforward – the first link provided in Part 1 of the codelab walks through it.
As for sharding data, here is a good video that explains how to do it:
https://www.youtube.com/watch?v=Rwg26U0Zs1o
Also, you'll want to stop all your instances when you're not using them, otherwise they will burn through your billing credits. Keep in mind when you do this then your processes will stop, and you will have to start up the process for each instance when they start. An easy way to automate it is to use a startup script. GCP has good documentation for this. Here are the commands for starting up the processes:
Config Servers
sudo mongod –config /etc/mongodConfig.conf&
Router Server
sudo mongos –config /etc/mongoRouter.conf&
Shard Servers
sudo mongod –config /etc/mongodShard.conf&
Note - When you're completely done with your project you can delete the entire project from GCP and that will release all of the resources being used.