MongoDB has a new cloud-based Atlas feature that has replaced M Lab. Unfortunately, it's also much more complicated for setting up simple databases.
Cluster basically means database. If you already have a cluster created (You see text like Cluster0), you can skip all these steps and go to "Create your database user".
-
Sign up for http://mongodb.com/cloud
-
Go to "Create New Cluster" (if it isn't already selected)
-
By default, typically, AWS, and a FREE TIER AVAILABLE cluster should be selected, but if it isn't, make sure you select those.
-
At the bottom of your screen, there should be a bar that has a
Create Cluster
, there should be the words "FREE" and "Free forever!" or something -
Click the
Create Cluster
button. -
It will then take you a "creating cluster page". It will warn you that it could take 5-10 minutes to create your cluster and get it ready. Some students have reported it has taken even longer than that. Cross your fingers that it will only take a few minutes!
-
Refresh after a bit of time to see if it has been created. If it was successful you should see something like
Cluster0
in big text with some graphs about performance of your (currently) empty DB
Every time you want to use your cluster, you will have to follow this procedure of whitelisting your IP.
-
Once your cluster is created, click the
CONNECT
gray button underneath it's name. -
Look under the
(2) Create a MongoDB User
, write a Username (can be anything, make it simple and easy to remember, this will just be for your app / teammates), and password (should only be alphanumeric, so just click the "Autogenerate Secure Password" is a good idea). Now, write down somewhwere this username and password. For example: dbUser and aB9OKFdCJmP9flZu -
Now, click
[Create a MongoDB User]
button
- Note: For username and password, pick ones that are just alphabetical or alphanumeric, since you'll be having to include this in configuration and it's easy to mess up if you include special characters.
By default, MongoDB bans everybody, except IP addresses on the "whitelist" (like a guestlist for IPs).
-
Look under
(1) Whitelist your connection IP address
-
Click the green button
Add Your Current IP Address
-
Click the green button
Add IP Address
(Or, better yet, click on "Allow access from anywhere (0.0.0.0/0)" so it will work for every IP)
Recommended: Add 0.0.0.0/0
in addition / instead of your current IP
address. This will whitelist ALL IP addresses. (Less secure, but more
convenient for development.)
Explaination: While building your app or before launching, if it gets too
annoying to continually whitelist IP addresses, you might want to whitelist a
"wildcard" IP address that will allow all connections going forward. To do
that, add the special code 0.0.0.0/0
to your whitelist, which is means "allow
ANY IP addresses". The reason is when you work somewhere else -- e.g. your
home, coffee shop, office, school, etc -- Your IP address changes when you
change connections, so unless you add the wildcard as above, you may have to
repeat the previous steps to add your new IP to the whitelist. You will know if
an IP address is not working if you are positive everything is correctly set
up, but you are still getting a "failing to connect" error.
If you don't have MongoDB shell installed yet, install it with one of the following commands:
-
macOS:
brew install mongodb/brew/mongodb-community-shell
-
Ubuntu GNU/Linux:
sudo apt-get install mongodb
The first connection we should do will be to connect in a terminal, to test everything and make sure it works. If you want, you can skip this section, and go to the next, setting up MERN.
-
Click a green button called
Choose a connection method
-
Click Connect with the Mongo Shell
-
Click the "I have the Mongo Shell installed"
-
Look at
(2) Run your connection string in the command line
, and copy the command you see there. Depending on the command they give you, you might have to replace the<password>
with the password you created when you created a MongoDB user, or you might have to enter it in after you run the command. -
Paste it into a terminal
If all goes well, it should spit out lots of warnings and even some error messages, but in the end show a prompt with a >
.
You should be able to test some Mongo commands here, if you wish:
db.testCollection.insertOne({test: 'stuff'})
db.testCollection.find({})
- HINT: If you are having trouble logging in with the random password it gives you, try making one that's easily typeable
- HINT: If the end of the connection string looks like
<dbName>
, change that to something like "testdatabase" instead (without pointy brackets)
Now, you want to have your MERN backend connect to your MongoDB database.
-
Click
[Go Back]
if necessary, so you are at theChoose a connection method
step -
Click the
Connect Your Application
button -
Copy the
Connection String Only
-
In your MERN starter, create a
.env.local
file that has the something similar to following line:export MONGODB_URI='mongodb+srv://DBUSER:[email protected]/test'
-
Now, run
run.sh
and the test application should work!
-
NOTE: You will have to replace everything between the quotes with the string that you got from Atlas. Also, you will have to change the
PASSWORD
to be the password you created before. -
NOTE: If the connection string has a '?' close to the end (like GET parameters), you should probably delete it any everything that follows it
If you have issues connecting, make sure your username / password of your database user, and your IP address is whitelisted correctly, and database user is, consider doing the following steps:
-
Click on the name of your cluster
-
On the left is a nav bar. Click on the "Database Access" item.
-
Then, click on either "ADD NEW DATABASE USER" or click on "EDIT" for the existing user (e.g. dbUser).
-
Now, type in a new, custom password, that's easy for you to remember
-
Save
-
Now click on Network Access
-
Ensure one of the rules there says "includes your current IP address" or something similar. If not, click "+ ADD IP ADDRESS" and add either a global 0.0.0.0/0 whitelist, or add your current IP & save
-
Finally, if the end of the connection string looks like
<dbName>
, change that to something else (like "testdatabase", that is without pointy brackets)