This article was originally published at https://www.blog.duomly.com/mongodb-in-the-cloud-with-atlas/
Almost every application you are trying to build needs a database; depending on the type of data you’d like to keep inside, some developers select SQL databases like PostgreSQL or NoSQL databases like MongoDB.
Nowadays, when MERN and MEAN stacks for applications are more and more popular, many developers select MongoDB. To start a project, you’d need to set up MongoDB locally on your computer or online in the cloud. In my opinion, it’s much easier to set it up online because when the application is ready, you don’t need to move the database and avoid some issues.
That’s why I very often use MongoDB Altas to set up a MongoDB database for the projects I’m creating. It’s easy, and you can start using it for free and later upgrade when necessary.
In this article, I’d like to show you step-by-step how to setup MongoDB in the cloud with Atlas.
As always, if you are the person who prefers to watching than reading, feel free to check out our Youtube channel for the episode.
Before I start setting the account and creating a database, I’d like to briefly explain what MongoDB Atlas is and why it’s worth using it.
MongoDB Atlas is a global cloud service provided by MongoDB, and it was created with a thought about developers who’d instead focus on coding than managing databases. We can select between AWS, Microsoft Azure, or Google Cloud Platform as a cloud provider.
MongoDB Atlas can handle and simplify deploying or managing databases and save lots of time on application setup.
The main advantages of MongoDB Atlas are the possibility to select between a world-class cloud partner and an ecosystem that fits our application the most. It also offers built-in security for the data we will keep in the database.
Besides that, MongoDB Atlas also makes it easy to scale a database in any direction, so if the application grows or the number of users decreases, we can adjust the database.
There is also well-prepared documentation that will help you to keep the database optimized, well secured, and stable.
Let’s see how to set up the account at MongoDB Atlas and how to create a database for your application.
When entering the landing page of MongoDB you will have to create a new account if you don’t have one yet, and I assume you don’t.
To create an account, press the green button „Start free”. After that, you’ll be taken to the registration form, where you have two options. One is to create an account using Google email, and the other one to pass the data and register traditionally. Also, you can see the information that no credit card is required to start.
After the registration process passes correctly, you’ll get an email, and you will be redirected to the section, which will help you to set up the account.
First, you’re going to name the organization, no matter if you are a part of the organization, team, or individual.
Let’s assume that we are building our portfolio, so let’s make Portfolio the name of our organization.
The other field there is a Project Name, and here you can think about different environments when you are working on the bigger project where you’d like to separate the testing env, development env, and production env.
Let’s assume our project is a blog platform, and we are setting the development environment, so let’s call it blog_dev.
The next thing that we are going to set up is the language we are using to write our application's backend. This will help you to get useful code snippets to connect with your code, and you can change that later if you change your mind.
Next, you’ll be taken to the available plans, so you can also select the other options than the free one if you’d like to. We will select the free plan for the purpose of this tutorial.
Let’s select the Create a cluster button on the first tab. Now the account seems to be working fine, and we can create a cluster.
The application should show you the Create a Starter Cluster screen like the one on the image below, where we can start creating the cluster for our database.
So, let’s select the cloud provider and region. We should select the region depending on the location of the server and users.
We have three cloud providers to select from AWS, Microsoft Azure, and Google Cloud. For the purpose of this tutorial, let’s select AWS, but when you are going to try, you can also select other ones if they are more suitable for your project.
As a region, I’d select Frankfurt, as I assume that this part of Europe will be the most suitable for my application.
Below that, there’s another toggle, where you can select a cluster. Here, depending on your needs, you can select what’s more suitable. For the purpose of development and our tutorial, I’d select M0 Sandbox, which is free.
Then there’s an option for additional settings, and it allows us to set up backups for paid clusters. This option can be a great one if you are creating a production project, as doing a backup for your database can sometimes save your life.
And the last option is adding a name for your cluster. There’s a default one, but you can change it to make it more recognizable to you. For the purpose of this tutorial, let’s call our cluster portfolio_blog.
After that, press the Create Cluster button below. Creating the cluster can take a few minutes, and when it’s ready, there are some more steps to do.
Now we have to create a user for the database. To do it, let’s press the Database Access on the right sidebar menu.
Then, press the Add New Database User button, and you’ll see the modal window with a form. Let’s start by selecting the Authentication method. We can select between three methods, but for the purpose of this tutorial let's select Password as an authentication method.
Next, we have to set up the username and password for the database user. Keep in mind to set it up carefully and not too easy. You can use the option to generate a secure password, which in my opinion, is a good solution.
Below that, you can also set up privileges for the user. To make sure you selected the correct ones, you can read more about them in documentations. I’ll select Read and write to any database privileges for the purpose of this tutorial. Below that, we can also restrict access for the specified cluster to the user we’ve just created, and I’m going to do this because of security reasons.
The last thing you can do is checking the user as a temporary one, which means the user will be deleted after some time.
When it’s ready, you can press the Add User button. Then you will be redirected to the previous dashboard, and you will be able to see a user you’ve created.
The next step in setting our MongoDB is to restrict access to the database and access our cluster only from a particular IP address to avoid security issues.
Let’s open the Network Access tab on the right sidebar menu and press the green button Add IP Address. Next, you’d see the modal with two fields to fill in. Put the IP address from which you’d like to access the database. For your localhost, it should be your IP, and in the case of the production environment, it should be your server IP. Here you can also mark the entry as temporary if you need it. If that’s ready, press the Confirm button.
The next step is optional but can be useful; adding sample data can be helpful when you are going to connect the database with the code.
To Load Sample Data, go to the main dashboard, and in the cluster table, press three dots just after the Collections button. When the menu will open, select the Load Sample Data option. Then you’ll see the modal, and you’ll be taken through the process easily.
Loading can take a few minutes, but when it’s ready, you’ll be able to open Collections and see the sample of the data.
When it’s there, you can connect the database with your code.
To connect to your cluster, go to the dashboard, and in the cluster press, the button Connect. Then you will see the modal with two points, one about the IP address and the other one about the user. If they are marked as correct, you can choose the method of connection.
Then you can select the method of connection. You can choose among the three ways, connecting with the mongo shell, connecting with the application through the URL, or using Compass.
For the purpose of this tutorial, let’s connect our application with the second option.
All you need to pass is the password. And that’s it!
Congratulations! In this tutorial, I showed you how to setup MongoDB in the cloud step by step. We started by creating the account, then creating the cluster, and finally creating the database user. I also showed you how to load some sample data, so you can check that everything works when you’d connect.
We also went through what MongoDB Atlas is and why it’s good to use it.
I hope this tutorial was useful. Now you won’t waste any more time setting up a local database to start developing an application, especially using MERN or MEAN stack.
Thank you for reading,
Anna from Duomly