Welcome to another edition of five minute react. Today we’re talking data storage. If we’re going to store data—and we are—then we need a database. Now, I know that this is a controversial subject, and someone out there is thinking I'm an idiot for not using Redis, or PostgreSQL, or CouchDB, or, I dunno, Microsoft Access or something. I apologize to you, good sir or madam, for the offense I have caused … but I’m going with MongoDB, because I find it easy to install, easy to work with, and I like its BSON data structure. Those of you reading or watching this tutorial: I recommend using what your DevOps Engineers and Server Admins think is best for the project. It’s really just a matter of swapping out database drivers, since no matter what, we’re building an API that gets data from the database and returns it as JSON to our front-end.
So with that out of the way, let’s rock and roll. We’ve got another hybrid Mac / PC tutorial here because the installation differs pretty heavily depending on the OS. I’ll try to keep each side short. If you’re working on a PC, you can skip to “Installing MongoDB on Windows” below. If you’re on a Mac, stay with us.
We’re going to be using Homebrew again, because Homebrew is great! That means we just need a couple simple commands to get MongoDB on our systems. Fire up a terminal window and type the following code:
brew update && brew install mongodb
This will, just like when we installed Yarn a couple tutorials ago, make sure that Homebrew has the most up to date list of packages, and then install the MongoDB package. Let it run, and … you’ve got MongoDB. Now we need to make a directory in which to store our data. MongoDB’s default is
/data/db … and that’s fine by me if you want to use it. I’m going to put it somewhere else though, mostly so I can show you how to make the necessary edits to make Mongo work with the new directory. So let’s put our data in our home directory, instead of at the top level. First, in our terminal, we need to type:
mkdir -p ~/data/db
and hit enter. Now you need to edit Mongo’s config file. It’s in
/usr/local/etc/mongod.conf – the reason there's a D in "mongod" is because it stands for daemon (the server kind).
(pictured: another type of Damon)
So, open the config file in whichever text editor floats your boat. I suggest Sublime Text, obviously, which we’ll do like this:
You’ll see something like this:
systemLog: destination: file path: /usr/local/var/log/mongodb/mongo.log logAppend: true storage: dbPath: /usr/local/var/mongodb net: bindIp: 127.0.0.1
There are two things we need to change here. The first is commenting out the entire systemLog section, because for right now we want to see things logging to our terminal (on a live server, logging to a file is the superior option). So comment those out by adding hashtags, like this:
#systemLog: # destination: file # path: /usr/local/var/log/mongodb/mongo.log # logAppend: true
Now we need to work with that
dbPath setting. Delete what’s there and replace it with
Note that, obviously,
YOURUSERNAME needs to be replaced with your user name, unless your username is, in fact, “yourusername” … which would be really weird. My username on my MacBook is “chris” so my config looks like:
Yours will differ. If you don’t know your username, then … consider sticking with the default settings and not bothering with this stuff.
You might also be wondering why it is that, if the config file was originally set to
/usr/local/var/mongodb, it doesn’t by default use that folder for storing data. Well, that’s because Mongo doesn’t read that config file unless you tell it to (Homebrew generates the file when you install MongoDB). So let’s deal with that by launching the Mongo daemon. There are two ways to go about this: as a one-off launch, and as a service. The former means that every time you restart your Mac (or even just kill the terminal in which Mongo is running), you’ll have to restart MongoDB. The latter means that MongoDB runs in the background, and will spin up automatically every time you restart. Obviously, you want the latter for your server, but for our local machines let’s stick with the running it manually every time, for now.
So, if you just want to use the default directory, open a terminal window and type:
and hit enter. If you edited your config file, you have a longer command to use, so type:
mongod --config /usr/local/etc/mongod.conf
Yeah, that’s super annoying, but the good news is: you rarely ever have to restart MacBooks, so it’s not going to come up that often. If it bugs you, well, you can always set it up as a service by typing:
brew services start mongodb --config /usr/local/etc/mongod.conf
Anyway, for now just launch the thing. You’ll see it spin up, and then just sit there, waiting for something to connect to it. We’ll get to that in the next tutorial, so that’s it. You’ve installed and run MongoDB. Your database is waiting, and you’re done with this tutorial. On to the Windows folks!
Installing MongoDB on Windows
Welcome back Windows users. This is going to be quicker and easier than the Mac version. First, point your browser to http://mongodb.com and click the download link in the upper right. You’ll be presented with a bunch of tabs, so click “Community Server,” which is their code for “the one we don't charge for.” It’ll give you an MSI to download. Download it! Then click to open the installer. It’ll do the standard Windows thing of computing space requirements, then have you click a few buttons to get through the installation. As usual, I’m sticking with the defaults, here. You can go custom if you’re a power user and want to eliminate anything you feel is unnecessary and save a few bytes of disk space, but I’m just going complete. Let the installer do its thing, and that’s it: you’ve got MongoDB installed.
However, MongoDB doesn’t automatically add itself to your system path, which is annoying. To do that manually, open your start menu and just search for “path” – you’ll see an option for “Edit the system environment variables” which brings up the System Properties. Click the button that says “Environment Variables,” choose “Path,” and click “Edit.” You’ll get a big list, or possibly a small list if you just recently installed Windows. Ignore the list, click “New” and paste the following:
Then click OK, and you should be good to go! Note: if you're running through this tutorial with a version of MongoDB higher than 3.6, check your file location, as it's probably going to be slightly different.
Now, we need to decide where we’re going to store our data. By default, MongoDB will look in
\data\db on the same drive on which the program is installed (in this instance, C). That’s fine, but for the purposes of showing how to use custom folders, I’m going to go with
C:\mongo\data\db\ instead. So open A command prompt, head for C:\ and then type:
And hit enter. Now we can start Mongo by typing:
mongod --dbpath c:\mongo\data\db
or, if you’re using the default folder, just:
and hitting enter. There it goes, spinning up and waiting for connections, which we’ll talk about more in the next tutorial. For now, you’re done, unless you’d like to register Mongo as a Windows service that runs in the background whenever you start your machine. To do that, you’ll first need to create a config file, which for the purposes of this tutorial we’ll store in
C:\mongo, but you can put it wherever you like. Your config file should be named
mongod.cfg and should look like this:
systemLog: destination: file path: c:\mongo\mongod.log storage: dbPath: c:\mongo\data\db
Again … you can store that stuff wherever you want to store it. Just edit the config file appropriately, then save it. You can use MongoDB itself to register the service, which is nice. Open a command prompt and type:
mongod --config "C:\mongo\mongod.cfg" -install
If you hit enter, it’ll install and start the service. I’m not going to hit enter, because I don’t want to install Mongo as a service right now. We’re just going to run it manually for the time being.
All right, that’s all there is! You now have Mongo installed. Let’s connect to it and do a little bit of testing. See you in the next tutorial.
Matt Damon image copyright: buzzfuss / 123RF Stock Photo