MongoDB with Async Python Transcripts
Chapter: Deployment
Lecture: Connecting to a Remote MongoDB

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now the final thing we would like to work on here is we want to be able to back up our
0:05 Data somewhere off of the machine right like I guess it's great to back it up Privately on that single MongoDB server, but it's not super helpful
0:17 So because if something goes wrong with the server probably the local backup will be messed up as well or at least inaccessible, right?
0:23 the other thing is we would like to be able to use tools like studio 3t or
0:29 robo3t or you name it to be able to actually talk to and manage our database. So that's a bit of a
0:37 challenge. Let's pull this up again. What did I tell you, you do not want to make possible a new
0:43 connection, I want to go put in the server name, like Mongo server, course, whatever we call it,
0:52 it right. And we said the port was 5621 or whatever that was. I said you never ever want
1:02 to be able to connect to this outside of that virtual private network. Well, how are you
1:07 going to fix that? Do you want to put your computer inside that network? No. What we're
1:11 instead instead we're going to do we're going to create an SSH tunnel. The one thing we
1:15 can do to our MongoDB server is create an SSH connection. An SSH tunnel says we can
1:22 create a way to you over once we set up an SSH connection, tell a local port that it actually flows through SSH over to the server.
1:33 It's slow, but for admin stuff, it's totally fine. So that's how we're going to connect to it. We're going to create one of these SSH connections.
1:41 There's a little bit of a shortcut we can use here, we can give it a connection string.
1:45 And down here, we happen to have a great long connection string that we can use. Now it doesn't import perfectly as we will see, but it's a good start.
1:54 So we're gonna connect over here to this and the IP address is going to be localhost and the port is fine. 'Cause we're gonna use SSH as we'll see.
2:08 So hit that, it says it's been auto-configured mostly. Let's give it a name, MongoDB production and I'll put course in here
2:15 to not confuse it with other things. So localhost actually is where we're going to go to, but then the SSH is going to redirect us. So let's do that.
2:25 Use a SSH tunnel, MongoDB core server. And this is the SSH port, which is right. Username is root. And then you need to set your private key.
2:36 I'm going to browse over to mine and set that up. This test connection is for the whole setup, not just for the SSH tunnel.
2:44 So hold off for a minute there. Over to SSL, use SSL, yes. But this is one of the things that did not get set up correctly.
2:52 Actually, so actually that did get set up, didn't it? Okay, very nice. Authentication.
2:59 This is our database auth, so username and password, admin, that was what we had. And finally, the server is localhost. This might work, let's test it.
3:11 Yes, okay, it works. Save. Now in order for that to work, there's one minor change I actually had to make in the in the setup
3:21 Because we're only listening on the server on this IP address Turns out that the way this tunnel was working when we tunneled the localhost
3:33 It was it was not using the virtual IP It was using just localhost
3:45 So over here I added instead of just this IP address in quotes with no spaces between the commas
3:52 Localhost and this it doesn't change the security really because it just adds a local loop back
3:58 But it was what was required to make this actually work. So now if we connect This we got our data and we go to the packages
4:07 We do a find on them the data comes back a little slower, but because it's tunneling through SSH it still comes back Great, look at this.
4:16 So now we can manage it this way. We can also use things like the Mongo dump. Over here we can export the entire collection or view.
4:28 You can add more things, but Mongo Restore has an opposite operation called Mongo dump. Let's see if we can use that.
4:39 And we're going to set up our own SSH tunnel like this. I'm going to say the -f to tunnel it and -fnl from here from the local one to that one over
4:53 there. So it's just going to kick off in the background. All right, well, let's try that. CD desktop. And now we're going to use a Mongo dump command.
5:07 I put that over here in MongoDB, --host, localhost, all of the commands we have here. And what we're gonna do is we're gonna take the database,
5:18 PyPI, and put it into the output into the working directory. And also here's the command. Again, you're gonna have to change the server name
5:26 to whatever you named yours. We're gonna dump this out here. You can see it's downloading over the SSH tunnel.
5:35 And you can look right here and there's all the data backed up. Excellent.
5:40 So we have the ability to use the Mongo tools using the SSL tunnel locally, as well as within studio three T we can use the SSL tunnel as well.
5:51 All right. Pretty excellent. Pretty excellent. I'm not sure how that landed with you.
5:58 If you've done a lot of things with Linux and the command line or the terminal, you should feel pretty comfortable.
6:04 But I understand that sometimes that might be overwhelming. So again, decide, do you want to use some hosted service or do you want to create your own?
6:11 Honestly, once you get it set up and running, as long as you make sure it stays patched and backed up, there's not a big deal,
6:17 not a lot of work to running your own MongoDB server. Here's how you do it. It's up to you on how you want to deploy your code.


Talk Python's Mastodon Michael Kennedy's Mastodon