Eve: Building RESTful APIs with MongoDB and Flask Transcripts
Chapter: Appendix: Installing MongoDB
Lecture: Setting up MongoDB as a Windows service

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Here we are in windows 10, so I think this might be the anniversary update
0:05 or not sure exactly which version, but it's not the fresh one,
0:08 it's been out for quite a while now.
0:10 So what we're going to do is we're going to install MongoDB.
0:13 Let's go over here, check out mongodb.com, click on download,
0:17 so we're going to go and get the msi, I want to get the one with ssl x64 bit,
0:23 you put your name in here to get info from MongoDB if you want,
0:26 you don't have to; okay downloaded, let's run this, all right,
0:32 so current version of MongoDB at the time of this recording is 3.4.4
0:36 so we're going to install this, and I'll just open up the custom
0:39 complete would be totally fine, just so you can see what's there,
0:43 there's the server which is mongod itself, there's the client tools,
0:47 there is the monitoring like analysis tools,
0:50 import, export, for data backup and restore
0:53 the sharding server Mongo s and then like a few other utilities,
0:58 unless you have a good reason to not have these on your machine,
1:01 I would just go for the complete install.
1:06 All right, it looks like MongoDB is installed let's try this—
1:09 no Mongo, all right, the reason there's no Mongo is
1:14 we've got to set up this thing in our path,
1:17 so let's go over here to program files, mongodb, server, number, bin,
1:21 so basically to do anything interesting from the command line
1:25 and that's really where you want to be working with MongoDB
1:28 you're going to have to come over here
1:30 and put this into your path, so let's do that now.
1:33 You just go to properties, advanced, properties, environment variables,
1:37 this is way deep down, go to the path and hit edit, and then hit new,
1:41 this is very much nicer than the way it has been in the past,
1:44 and it will just take that path and put it here;
1:47 close everything off, ok, so now we should be able to run Mongo,
1:54 hey look it's not going to work, but we can see
1:58 it's this one that we just found and set up,
2:01 so in order for Mongo to work, we can actually try to run Mongod
2:05 and we're going to get a sad, sad message here,
2:09 so if you look somewhere it's going to say this directory
2:13 basically the database directory is not set up.
2:16 Well, there aren't default places where you can put the data
2:18 and it will actually create that, you see here is the startup settings that it's using.
2:23 So we don't want to do this, we want to actually make another one
2:28 logs and one called configs, so you get to configure all of these,
2:34 so you can configure that however you like,
2:37 but we should set up something like this
2:40 and so let's go in here, now I'm going to copy a config file over
2:44 so we have two, and notice I've named one command line and one is service,
2:49 let's just look at the command line one.
2:52 So notice, there's not a lot going on here,
2:54 I think this directoryPerDB we could actually drop this,
2:57 this is not used in the new version,
3:00 so we're basically saying drive c:\mongodb\data,
3:02 let's just double check that that does exist, it looks good up here,
3:06 c:\mongodb\data, okay, journaling enabled, you definitely want that on
3:11 and this is super important, listen on a local host only, only,
3:15 this is a dev machine there's no reason they should listen on the open internet
3:21 in case your firewall is down or you're somewhere
3:23 where people are scanning the local ports on their local network,
3:26 think hotel, something like that, so we don't want any of that to happen,
3:30 so we're going to listen on a local host only.
3:32 All right, so what we need to do now is
3:34 we want to try to run MongoDB again, now with this,
3:37 so let me go up here and copy the path,
3:43 so we should be able to run MongoDB now, let's clear that off,
3:46 so mongod and when we tried to run it before and it gave us this error,
3:50 now we can say --config is that, and if we've got everything set up correctly
3:55 this should work, there might be permissions on that mongo folder I created
3:58 we're going to find out in a second.
4:01 It looks like things are going well, let's go over here and try to connect
4:06 so we can type mongo and hey, we're in,
4:09 I can do things like show dbs what's here, perfect,
4:13 ok so it looks like this is working, it says now warning,
4:16 you don't have access control like this is wide open to the internet
4:20 and it's unrestricted read/ write, this is not the best,
4:24 it's pretty much okay because we're listening it on the local host,
4:27 still could be a problem, you might want to set up an account
4:30 when we get to the deployment and production settings,
4:33 this is, we're going to solve these problems,
4:35 but for development this is probably good.
4:37 I had that one cofig, this one that worked,
4:41 let's check this one out and make sure everything is ok as well.
4:43 So this service one is going to run
4:45 when we install MongoDB as a Windows service
4:48 so if we were running in like Windows virtual machine
4:52 in aws, ec2 or in Azure something like that,
4:56 this would be what we'd probably run, of course
5:00 with credentials and things like that, we'll talk about it at the end;
5:03 but if we're going to set this as a Windows service,
5:06 this will only succeed if we set the logs,
5:09 so that's why we created this logs folder
5:12 and that's why this service one has a system log section.
5:16 So the next thing to do, now that we're over here
5:19 is we actually want to first let's just test that,
5:23 so let's test this service version and we won't see anything because the log file
5:31 but if it just sets there, I guess we could go ahead
5:35 and test that we can connect to it— yeah, looks like that worked.
5:38 Okay so it looks like the service is working
5:43 we'll just control c out of there.
5:46 Now the next thing that we need to do, this is optional,
5:49 you don't have to do this, you could literally come and type this out every time,
5:53 but let's go ahead and set this up as a Windows service,
5:56 so you can tell it to auto start, delay start
5:58 or just flip open to the services and click go
6:01 whenever you want to use MongoDB, that's how I
6:03 whenever I'm working on windows, how I use it.
6:05 So we can go to the services,
6:11 and let's hit m to see if there is anything for Mongo,
6:13 and now there's nothing for MongoDB here, ok, so no MongoDB;
6:16 and what we want to do is we want to register MongoDB as a Windows service,
6:21 now there's something that's really, really important here,
6:24 I can run MongoDB like this,
6:28 -port equals whatever port, --ssl and whatever,
6:35 all of the options go here, so --db path equals, we get filled this out here,
6:43 it turns out the way that MongoDB registers itself
6:47 if I try to install it as a Windows service using the explicitly passing the parameters
6:52 the only way to change those values, to change the way MongoDB works,
6:55 is to actually go and edit the registry entry in Windows, not amazing.
7:01 So what we're going to do instead, is we are going to do what we already did
7:04 we want to go to basically say run that config file.
7:07 Now, the other thing that I've seen can be really tricky
7:10 is the Windows service path might not be the same as your path
7:13 so you need to use like full path names everywhere,
7:16 so we'll say where mongod, so we want to run this explicitly
7:21 because that's what gets recorded in the Windows service,
7:24 so we're going to say that instead of just mongod,
7:27 we'll say --config and that was in c:\mongo\config\ this one,
7:36 now we've got to use the service one that has the log
7:38 and then finally, here's the trick, this is the thing,
7:42 actually this is not going to work, so I'm going to copy it,
7:45 I'll show you this not going to work.
7:47 So the trick is to say I would like to install this as a service
7:50 because it's not going to work, i'm going to copy it, so I don't have to type it again,
7:55 ready— enter, now, no errors, but if I refresh, also no MongoDB.
8:02 What happened? Well if you actually open up that log file
8:06 in there it will say permission denied, could not install MongoDB,
8:10 why— because this is not an administrator command prompt,
8:14 not root or anything like that, this is just straight up, just whatever my account is,
8:20 so I got to right click, you see these options, if you shift right click
8:24 you say run as administrator, and then you run the exact same command
8:28 and it does the exact same thing,
8:32 except in the log file, there's now a different message
8:34 if I refresh— ta-da, we have MongoDB.
8:38 So let's test this, if type mongo, trying to connect, trying to connect,
8:42 it's going to time out, right, nothing.
8:45 Now if I go over here and I press start, do the same thing again,
8:51 ta-da, now we have MongoDB set up as an auto start windows service.
8:55 That's pretty awesome right.
8:58 So if we reboot, MongoDB will run.
9:01 It might be the case that just sometimes you want to run Mongo
9:03 and the other times you don't want to it say sucking down the battery on your laptop,
9:06 you can set it to automatic delayed start, so your Windows boots faster,
9:10 and you'll still have it, or you can just set up purely to manual
9:13 in which case it's only going to run
9:15 after reboot if you come over here and click go.
9:19 So that's depending on how you use Mongo
9:23 whether you want it certainly in production
9:25 if you're on a Windows server set that to start
9:27 but maybe manual for development, I don't know,
9:30 it depends how often you use MongoDB, if you use it all time
9:32 that's all you work on is apps to talk to it,
9:34 just set it to auto start or delayed or something.
9:36 Okay, so now this Windows machine is all configured to run MongoDB,
9:41 how about PyCharm, and RobMongo and so on?
9:44 Those all have straightforward installers
9:47 so just install Python 3, Robomongo and PyCharm whichever edition you want
9:51 and just next, next, next your way through,
9:54 with the Python one make sure that you check the box
9:56 that says add Python to my path, that one is important.
9:59 Other than that, there's really nothing else to it, you'll have a machine
10:02 that is set up and ready to do this MongoDB course on Windows.