Eve: Building RESTful APIs with MongoDB and Flask Transcripts
Chapter: Appendix: Deploying MongoDB
Lecture: Installing MongoDB for production use
Login or
purchase this course
to watch this video and the rest of the course contents.
0:01
It's time to install MongoDB on our cloud server. One thing I'd like to point out is you don't have to necessarily go down this path
0:08
to run your own MongoDB server, you maybe don't want to deal with it, maybe you don't have enough experience with it things like that,
0:14
so a couple of options just that I want to point out, but I definitely want to show you how to run your own MongoDB server,
0:20
how to do it safely so that you can because in a lot of cases it really is the best. So MongoDB has this thing called MongoDB Atlas
0:27
which is database as a service, and basically what you do, I haven't used this really, but I have looked at it, is you create a AWS account
0:34
it was on web service, EC2 account, you give them access to make machines and manage those machines on your behalf
0:41
and they can create a replica sets and things like that that are cure for you, this is like a service that will manage more or less
0:49
your EC2 machines upon your behalf, so this is decent. Another one is you can go to something like mlab over here
0:56
and you can check out the plans on pricing, they have a free, sandbox free half a gig, just cool when you do a shared one with light production work
1:07
for up to eight gigs of data, but it isn't a replica set with fail over and things like that,
1:13
so this is a pretty nice service, it's really expensive in my opinion but it is pretty much turnkey, push the button
1:20
you get what you want, you get what you need. I found it to be decent, but also it seems like it's added a lot of latency
1:29
to some of the apps that we moved off of our own servers on to mlab, so I guess it probably depends, one on how much you pay,
1:37
and two, how close your machine is to their machine, but they do claim to do a lot for half a million MongoDB deployments,
1:44
on the major cloud providers. I just want to put it that like you can go and just get MongoDb as a service.
1:50
Now, if you're still with me, I'm assuming that you want to install understand how to create your own MongoDB servers,
1:57
so let's go over here to download, and we're going to go through a few interesting steps,
2:02
so I would like to do the Linux download, I know I'm on MacOS, but I'm configuring this over here, now notice, I could hit the tarball
2:11
and this would do a thing, I could install this, I could run it, but it wouldn't give me the ability to say automatically upgrade my server.
2:19
Right now it says instructions for installing with yum, but I want to do this on this x64 version of Linux of Ubuntu,
2:31
16.04 that's the one I got, I think you can just take the same instructions and apply them for 17.04 as well.
2:40
Now here's what we really want to go, we could click this, we could get the binary, but this is better,
2:45
so we're going to come down here, and there's just going to be some copy paste action, now look, it says what we can do is
2:53
you can just use aptitude to install this, so let's try that. Before we actually go over here, tell me, which one of these is a MongoDB server,
3:04
I don't know, I don't remember either, so let's take just a moment and step back and give these names, and I want to give them
3:10
the exact same name as they believe their machine names are, so this one, the web server, it refers to itself as a web server,
3:18
this one its local machine name is this, themongoserver so let me open this one up, now we should probably enable floating ips for real production,
3:29
but this is not real production this is me playing around so I'm not going to mess with that. We could also enable what are called cloud firewalls,
3:39
but again, this is the Digital Ocean specific thing, you do this in EC2 differently, you do this in Azure differently and so on,
3:44
so I didn't want to show you how to just use the Linux tools to do that but it may be better actually to do this,
3:50
here you can see some of the monitoring kicking in so I'd like to be able to say ssh root@ themongoserver, right
3:56
and sadly, it doesn't work, so let's tell this machine, let's do a sudo, and I'll run Visual Studio Code again, it's /etc/host
4:09
here you can see I have hacked a few things together already, and we're going to go and put this, the Mongo server in here
4:17
and what is its ip address, of course like I said, give it a floating ip and use that one possibly
4:23
but we're going to go like this, if you want to give it a real domain name feel free to go ahead and do so, but this will work for,
4:31
there's probably no reason to give your MongoDB like a public dns name so I'm going to suggest that maybe you don't do that.
4:39
Let's go here and get the web server, okay, so I save that, now let's try that again,
4:55
we've never connected to a machine called themongoserver with this key so it's fine, and now we're back,
5:03
so now we can say connect to root@themongoserver, and at thewebserver, that's what I called it right, thewebserver.
5:11
This will make things easier and you can see even on that machine, it believes it's called this, for some of the tricks we do later with tunneling ssl,
5:19
it turns out that makes our life a little bit easier. Okay, so we're on the mongo server, that was our goal
5:26
maybe a little bit long to get here but that was our goal. The next thing to do is we're going to go down this list that they gave us here,
5:34
so we are going to do is ssh in here and play this, so it says what you can do is to install MongoDB is you can install this aptitude package
5:46
and then you can actually install smaller pieces, like we could install say just the server, right or maybe just the sharding deamon things like that,
5:54
but if you install this you kind of get all of it, and it's going to be amazing except for that it's not, it's not amazing it all, it's not there,
6:01
because this comes from one of mongoDB's own app update servers, so we got to go down here and go through the steps,
6:08
so the first thing we have to trust, trust MongoDB we're going to stall the software as root, I guess we're going to have to trust it anyway aren't we.
6:17
Then all seem to come out alright, be careful here, I always screw this up,
6:23
even though I clicked on install for 16.04, it gives me all the options here so don't do that, that's 12, 14, 16, that's done.
6:36
Now the next thing to do is run apt update, I'm already in root, so I don't need sudo
6:44
so we needed to do that basically to pull from that list we just added there, so now let's see what it's asking about, it's all good.
6:59
Alright, so now we can go do that apt install mongodb.org, and what happens— magic is going to happen, that's what.
7:10
Notice when I said this one, it's like a metapackage, it's really installing those four, it just said
7:16
hey here's an empty package with these four dependencies. Alright, that was quite quick, and notice we have 3.4.5 so that is quite a recent one here,
7:29
and it's even created a mongodbuser for us that is the one I believe that runs the deamon process
7:34
so it is not running his route, that's pretty awesome, it's another thing you'd have to do if you just downloaded the tarball and tried to set it up.
7:41
Very cool, and now next time I come over here and I run apt update and then upgrade that could potentially install 3.4.6 or whatever is next.
7:52
Do we have MongoDB— I could type Mongo and something happens except fail, no you cannot connect; why— because it's not running,
8:00
it will run though if we just say service mongod start then we can ask status, and notice, it's running this process,
8:14
it's running the server in quiet mode, especially important using this configuration file
8:20
so we're going to be able to use that basically to configure or to adjust to the server all the changes we need to make to MongoDB
8:27
we're just going to edit that file and it'll be golden, but for now, let's just try to see if we can connect it. Wow, we can, we get a few warnings
8:35
like you really should not put this on the open internet, you really shouldn't do that
8:39
and some other stuff that we might consider about changing our file system around so we can say things like show db's and it just has the local stuff
8:50
but we're connected and it's all good. So, that's installing MongoDB on our cloud server. However, you want to be extremely careful about changing this
9:05
we're not ready to open this up, not even close, we can see here's the log file if we want to go and get the log here's where the data is stored,
9:16
you generally don't need to go in there and mess with the data you don't need to mess with the files directly,
9:21
we'll use the tools or replication or something to back up and configure the data
9:25
but that's where it lives and you can change where it is if you need to, right so those two things are interesting, we're going to go change this stuff
9:32
but we need to make a few configuration changes before we go and do that.