Eve: Building RESTful APIs with MongoDB and Flask Transcripts
Chapter: Appendix: Deploying MongoDB
Lecture: MongoDB admin tools

Login or purchase this course to watch this video and the rest of the course contents.
0:01 It's great that we have our MongoDB running in production we've got our web server and a MongoDB server and they're entirely locked down, we saw before
0:09 if we try to connect to that Mongo server, even though it's on a different port with ssl and authentication,
0:14 we can't talk to it because the Ubuntu firewall is blocking access from everywhere in the world except for that one fake web server thing.
0:22 So we can't talk to it, we can't for example manage it with Robomongo, which would be totally sweet, right,
0:28 but we can't even connect to it via the shell, can we? Well, we tried that and we saw it failed and if I do it again it will fail;
0:33 but I can ssh into the Mongo server like this, we've seen that, so that's cool, now what can we do with this?
0:42 It turns out we can set up an ssh tunnel using that mechanism so here if we run this command -f to run in the background
0:50 ssh over here, map the local port, 10001, on the remote machine say the local host 10001, like that. So if we run this code, it takes a second
1:04 and it creates a background task of ssh tunneling one port locally over there; now, what if we try this— we're going to run the same command
1:14 we saw working in production with authentication being this, here is the password, the admin and so on, notice there's no host up here
1:20 we have the port 10001, what is the host if we don't put one— local host, but local host 10001 really means the Mongo server 10001.
1:30 Let's do it. Check that out, it's working okay, we can ask it how are you doing, how many things you got going on here,
1:46 what is your host, this is what I was looking for, your host is the Mongo server, we're connected to the Mongo server, that's really cool.
1:53 Now we can say things like show dbs, we could come over here and say use that, we could even do our pretty find here so cars.find.pretty
2:06 and there's our data, okay so we can access this. And just like we could in the shell. Well if we can get to it this way, maybe,
2:16 just maybe something magical could happen with better tools. And yes, yes it can, we'll create, it's going to be a direct connection,
2:24 I'll call this themngoserver, connect on the local host 10001, that part is good, authentication database is good, copy these over, paste that in,
2:42 you can see this here, a mechanism is good, so this is all set, come over and say use ssl, I've not tried to do anything else,
2:55 let's try this, let's test it— all right, under ssl we say use a self signed certificate, there we go, good, alright, so we have themongoserver,
3:08 I didn't test it, but let's go ahead and give it a shot anyway. Authentication failure, okay let's go and edit that again,
3:27 oh look at that, have a little space right there, how frustrating, couldn't somebody give me a trim, connecting, authorized, yes! That is so awesome.
3:36 Okay, save the connection, now let's go over here, double click it's a little bit slow because hey, it's going over tunnels
3:44 but look at that, if we go over here we got our cars, we can view the documents we have everything that you could have done before with Robomongo,
3:52 you can do now, here's the two documents you saw me create in that Python section, oil change, tire rotation, Enzo Ferrari and so on.
4:02 And we can do things like maybe we had the service_history.price as an index well, add an index, it's going to be service history price,
4:15 and down here we'll say { 'service_history.price' :1 } like that, save and now how about that, we could even do a little thing
4:27 come down here say service_history.price is let's say 100, this should return just one record, and it does
4:38 and if we say explain, all the stuff we were doing, does it work— you bet it does. It's using that index that we just created remotely using Robomongo,
4:49 so this is super cool, last thing let's see about doing a backup. The next thing that I want to show you which I don't think we've done before,
5:02 let's go to our desktop here and we'll say make a directory called backtest cd the backup, notice it's there on the back up, nothing is in it,
5:13 so the last thing I want to do is show you how to use Mongodump so you can go to help and see all of the things that this does
5:23 but we're going to use Mongodump with basically all the same settings down to here we're going to go to demo dealership as we've named it
5:34 and the output is going to be into the working folder which is this. Because we're tunneled into the production machine
5:42 we can go and grab that data from there and back it up locally, let's try. Boom, we wrote two, we're whopping two documents
5:55 but over here, we have this, now the data comes out in this binary json but you can't really look at, we could technically look at this
6:02 but the point is this worked, we got our two documents, now you might wonder like ok that's cool for two documents that kind of works,
6:10 can you really do this for like actual data— yes, yes you can.
6:14 So I do something like this for Talk Python To Me and the training site, all these things,
6:19 and I can back them all up in one giant script that does things along these lines and it will back up to six million of records, six million documents,
6:29 I would say it probably takes less than a minute and a half over my pretty standard connection, and I'm on the West Coast of the US,
6:38 and that server is on the East Coast in Virginia, so it's not like I'm right next door, that's why it works.
6:43 So this actually works better than I expected it to work I guess, and it really is quite nice, so using this ssh tunnel means
6:52 we never have to open up that port, but we can go through ssh and still work with our server, with all of our cool tools.
7:00 Over here, come back, which one do you want to work with— local or remote, remote one of course.


Talk Python's Mastodon Michael Kennedy's Mastodon