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

Talk Python's Mastodon Michael Kennedy's Mastodon