Installing MongoDB

Academind by Maximilian Schwarzmüller
A free video tutorial from Academind by Maximilian Schwarzmüller
Online Education
4.6 instructor rating • 25 courses • 1,286,463 students

Lecture description

Let's get started and let's do so by installing MongoDB on our system and spinning up our on database server. In this lecture, you'll get a detailed step-by-step guide on how that works.

Learn more from the full course

MongoDB - The Complete Developer's Guide 2020

Master MongoDB Development for Web & Mobile Apps. CRUD Operations, Indexes, Aggregation Framework - All about MongoDB!

17:11:03 of on-demand video • Updated November 2020

  • Use MongoDB to its full potential in future projects
  • Write efficient and well-performing queries to fetch data in the format you need it
  • Use all features MongoDB offers you to work with data efficiently
English So let's take a break from the theory now and let's simply get started. Let's install mongodb locally for now and let's connect to it and see how we can work with it. Now mongodb runs on all operating systems, Mac, Linux, Windows, on all three operating systems, you can go to mongodb.com and on that page, click on get mongodb in the top right corner, that will allow you to download the mongodb installer and install it locally on your machine. Now there by default, it selected this atlas thing, that is something we'll dive into later in the course, for now let's use the free community server. Now the enterprise server would be your paid solution, we don't want to use that, we'll use the free one which gives us everything we need, the enterprise server just adds some enterprise level features like different kinds of authentication and encryption which we don't need here, so let's go with the community server and then there, choose your operating system in case it's not pre-selected correctly. Now there, you can go with the default version that is installed and then you can click the download button, by the way also always check the installation instructions where you find a link to here if you're facing any issues. So now I'll download that and you don't need to sign up here, you can just wait for the download to start and once the download finished, you can on Mac and Linux simply extract that file that was downloaded, on Windows you can start the installer. Now if you have that installer, simply walk through the different steps there and choose custom when you have the chance for it. Now there you can leave the defaults as they are, I just want to inform you that on Windows, mongodb will be installed as a service as you can see here. Now you could uncheck this but this is fine, this is a little different to how it will then run on Mac and Linux but you can follow along, fine in both cases and I will of course show you how to connect to it in both scenarios. So simply click next and install and install it on windows in the destination you chose. On Mac and Linux, you simply have that extracted folder here and now you can grab all these files in that folder and copy them into any place on your operating system where you want to install mongodb so to say, so there, I will go to a folder I prepared and simply paste that into here. Now the bin folder is the important folder, this has all the mongodb related executables, most of them you'll not really need for this course but we will work with a couple of them to start our own mongodb server which then hosts our mongodb databases, we'll also use the one for importing data at some point in the course and we will use the shell client which is a client that allows us to connect to the mongodb database and run commands on it and on Windows, you get the same executables in your installation path by the way. Now with these files being placed in the path or in the place of your operating system where you want them, you should do something else on Mac and Linux, you should go to your root folder on your system and there, you should create a new folder named data and in that folder, create another new folder named db and you can do that on Windows too if you want. You don't need to but you should create that folder at some place in your operating system, it can be anywhere, doesn't have to be the root folder but it will be easier to follow along for the rest of the course if you use that but I will also show you how to start mongodb when you use a different folder or a different path. So create that data db folder if possible in your root folder of your Macbook, of your computer, of your laptop or create it in any other path, I will show you how to make it work with both. Now with that, start your terminal or on Windows, your command prompt and this is where we'll work in for the majority of this course because there, we can run all these important queries or commands to insert or to retrieve data but of course, I will also show you how to then apply the knowledge you gain in here in a real app, I'll actually do this in this first module already. Now in there, try running mongo and you should get a command, not found error. Now on Mac and Linux, you fix this by going into your user folder and then look for a bash profile file, if you don't have that file, you can simply create it, .bash_profile, you can then edit this with a text editor and in there, you need to change your so-called path variable or you need to enhance it or you need to append something to it. You do this by adding export, starting with a lowercase e, path equal and then use the path to the folder where you placed your mongodb binaries in, so in my case this is in here and you can simply right click on that item, on that executable and copy that path here if you want and move that path into here, starting with one slash and in my case it's users, my user name, development, mongodb binary because that is the path I use to place mongodb in. Now important on Mac and Linux, to make sure that all your other commands work, you need to add a colon and then $path at the end and then save your bash profile file. Now on Windows, you also need to set this and you do this with a so-called environment variable, so simply press the Windows key and type environment and you should be suggested this option of setting environment variables at some point. Open that and there under your user variables, you should also choose the path variable, edit this variable and then add a new entry here which is the path to the directory where you installed mongodb and there to the bin directory as well, so always to the directory where all these executables can be found in. Simply copy that path and add it to the path variable and then simply save that. The same is true here for Mac and Linux, save that edited bash profile and thereafter, close all terminal or command prompt instances you need to restart both. Once you restarted your terminal or command prompt, you should be able to run mongo and it should give you an error that it can't connect, on Windows it will connect because mongod is running as a service there already, so the mongodb database server started as a background service because we checked this during the installation, so on Windows, you now probably already are connected. Now therefore on Windows, to also see how you could start it differently and I will have a whole module in this course where I talk a bit more about this shell and connection thing but on Windows, you can close that command prompt and start a new one as an administrator by right clicking on the executable and choosing run as administrator and there, you can now type net stop mongodb written like this and this will only work on Windows, don't do it, don't try it on Mac or Linux. This will stop that background service and now we can also start the mongodb server manually just as we do it here on Mac and Linux. You can now start your server by typing mongod, so mongo basically is a client which allows you to connect to the server and then run commands on your databases, mongod starts the mongo database server. So mongod is now a command you can use and when running it, it might fail. Now it might fail if you chose a different data db folder, I mentioned it would be easiest if you use your root directory so the C-Drive on Windows or if you use that route directory on Mac and Linux for the data db folder. If you use a different folder, you need to start your mongod instance here by typing mongodb --db path and then the path to the place where you created your data db folder. If that path includes some whitespace in there, you need to wrap it with curly braces, so for example here, I could type data db, that is redundant though that is the default where it looks for anyways and that is the path I'm using it here in my operating system, the default as suggested and on Windows, you can of course also add it in the same way to make sure you point at the folder where that data is stored. On Windows actually, I need to change that because there, the default path is inside of the installation folder of mongo database and then you start the mongodb server like this. Now if you type this, it should now start up, if you still get an error on Mac and Linux, simply try running sudo in front of this entire command, this will give you the right permissions to store data in that path. You are prompted to enter your password then and then it should work, on Windows this is not required and also not available. Now with that, the mongodb database server is up and running. On Windows, you don't need to do that, after restarting your system, the mongodb service should be running in the background again and you can absolutely use that, it's same server, just automatically started in the background. On Mac and Linux, you have to start it manually for now, I have a whole module where I dive deeper into that and explain how to start it as a service on Mac and Linux too. So now with that up and running, open a new window, a new terminal or command prompt window and leave the old one running, you need to leave that process running as long as you work on it because that is your running database server, if you shut that down, you can't work with it anymore. You can always quit it by pressing control c, this will quit and simply exit but then you need to restart if you want to work on it again. So in that new window, you can now run mongo and this will automatically connect to the running server, so now you are in the mongo shell which is the environment where you can run commands against your database server, where you can create new databases, new collections and most importantly, new documents. And this is now what we'll work on in the next lecture.