Lessons: 12Length: 40 minutes

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.4 Installing MongoDB and Redis

We don’t have a database yet! We will use MongoDB for our data and Redis as our session store. In this lesson we will install and configure MongoDB and compile Redis from source.

Related Links

2.4 Installing MongoDB and Redis

Hi, and welcome back to Easy Node.js Development Environment with Vagrant. In this lesson, we'll be installing the MongoDB database server and our Redis key value store. Let's SSH into our Vagrant box, so we're ready to paste in the necessary commands. To install MongoDB, go to the documentation section on their web site and jump on the install on Ubuntu part or just use the link below. They already laid out all the steps needed to install MongoDB. First, we need to add their public key to our apt package manager to verify the server's authenticity. Then, we add the repository service to apt. Since we added a new repository, we need to update our local package index with apt-get-update. Now, we're ready to install the MongoDB server. Use apt-get install mongodb-org to install server, find and tooling.. Yes, there would be a MongoDB package provided by the system repository, but those are generally a few versions behind than the repository that is provided by the project itself. After installation, the MongoDB server is up and running. All that is missing now is Redis. To download the latest version, use curl to fetch it from their servers. Wait, that's right. We don't have curl here. Let's install it with apt first. Now that is done, get the Redis source, and save the gzip file, then extract the archive. Before we can compile the source, we need the necessary tools. To get them, install the build-essential package. At the time of this recording, the provided source didn't have pre-compiled dependencies. So hop into the depths directory and run, make hiredis lua jemalloc linenoise, to compile those four dependencies. Then hop back into the previous directory and run, make, to compile Redis itself and, sudo make install, to copy the executables into the appropriate system directories. Now comes the configuration part. First, we need to create some folders. Redis has the convention that you add the port it is running on to all server-specific files and folders. This allows you to run multiple Redis servers side-by-side. Why is this important? While there are multiple databases you can use, but they are just numbered and there isn't any fine-grained authentication mechanism that prevents connections from accessing other databases than they are intended to do. For our setup, we are sticking to the default port 6379. We use sudo to create the data folder in /var and a configuration folder in /etc. Then, we need to copy the init_script that is provided by the project to our init.d directory and update the run level configuration, so Redis will restart it on boot. Because we stick to the proposed convention, we don't need to edit the init script, at all. Now, we need to copy the default configuration file to the correct directory and name it appropriately. There are a few changes that are needed to be made here. First, we have to change the demonize option, so Redis will run in the background. The port setting is fine, but we should update the pidfile name to include the port. Binding Redis to an IP address isn't really necessary in our case. But for good measure I'll confine it to local connections. Then, we should set the logfile to point somewhere. The last and probably most important setting, is really far down the configuration file, so let's just search for it. The dir property defines the location the data is stored. It defaults to the directory where Redis is started from. After saving, we can start the Redis service. Now that we have all dependencies installed, change into the /vagrant folder and start our server again. It doesn't complain, which is a good sign. When we access localhost port 4000 in the browser, we get the index page. Great! Now let's try generating some samples. That also works. Perfect. We have now successfully installed all dependencies we had for this project. In the next lesson, I will show you how you can use Vagrant to develop your project and make changes to it. See you there.

Back to the top