MongoDB with Async Python Transcripts
Chapter: Deployment
Lecture: Connecting to Production MongoDB with Python

Login or purchase this course to watch this video and the rest of the course contents.
0:00 The next thing we need to do is just make sure that we can actually have Python talk to our production database. So that's going to take two steps.
0:10 First of all, we need to change how we connect to MongoDB. Remember over here we have our Mongo setup, which is fantastic.
0:18 Never mind this. It's just if I set that as the source root, it'll be okay again. Over here, we're just passing in the database name.
0:26 And it's a local host. And it's this. And there's no more details.
0:29 details. Well, we saw that that is not going to fly any longer, is it? No. We need to make
0:35 sure that we're using the other port and the other server name as well as passing over
0:41 the actual username and password for the database server. All of those things need to be updated
0:47 in order for this to work. So we're going to do that, but we also need to get the code
0:51 moved over so that we can do something on the server. So instead of changing what I've
0:58 done previously. Let's go ahead and just make a copy of that and unmark it. And I'll make a
1:04 a new folder here called PyPI app or something like that. And we'll paste all those things from
1:14 chapter 10 right into there. And then we're going to go and update our infrastructure to allow us to talk to the server. So in this case, this
1:28 is what this app is supposed to consider as a cohesive whole. We're going to take that and mark it as a source's root. Excellent.
1:37 Now there's some warning from PyCharm here, but let's just run this. I don't think this is any sort of problem.
1:43 And sure enough, it's not. We can ask for a summary locally. It looks like it's working here.
1:51 Now for this to work, we have to pass in a lot more information. So let's go and work on this here.
1:59 Now first I'm going to make a function that allows us to create a slightly more complicated and interesting connection string.
2:07 So I'm going to actually go over here and say extract method, create connection string like that.
2:14 And then I'm going to go ahead and replace this with something more involved and we'll talk through it.
2:20 Okay, so instead of having just the database name, which we still have, we now have the server
2:25 that defaults to localhost. So we can use it in a simple form. But if you need to pass it,
2:30 say in production, that'll override it. Same thing for the port, we need to pass the username
2:37 and the password. Again, they have defaults, so you don't have to pass them. But in production,
2:41 you'll need to have a use SSL. Because on development, you don't production you do. And I guess we don't really need this.
2:51 The next thing we're doing is for some reason if they pass in empty string or none for the server, go ahead and just throw that back to localhost.
2:58 Again, same thing here, right? And then I have this function I called motor_init. And let's put that down here. It's a little bit more involved again.
3:07 So it's, what it's going to do, it's going to be createConnectionString. What it's going to do is take the database,
3:15 all that information, create the connection string, which we're going to have to upgrade as well.
3:21 It'll print out a little bit about how it's initialized. It's going to use motor, which is actually motor async. Like that.
3:34 And just like before, it's going to pass all that stuff off. It's going to pass all that stuff off to Beanie using the motor client that we got from
3:44 no longer a simple connection string, but a quite complicated one. And it looks like this models was conflicting
3:52 with that right there, so we'll change that. Okay, the last thing to do is we need to create a much more interesting connection string.
4:03 Let's go over here and paste all that in, like this. And so the idea is if you're passing in the username and password, you're doing all the things.
4:14 You're also passing in a special port, a server. We're going to have this nice complicated connection string, MongoDB slash slash for the scheme,
4:26 username colon password at server colon port. Our source is this TLS, whether or not we're specifying it. And this insecure,
4:37 this means use our self-created certificate. So that's what we do in production, But in development, we're just going localhost and port
4:46 and so on, very simple stuff. So we don't have to worry about configuring the dev system to be really complicated, but we do for production.
4:54 All right, so all of these things here are working great. What is this about? Okay, no need to make that async, so that error goes away.
5:07 All right, looks like we now have way more interesting setup, isn't it? So hopefully, I didn't want to hit you all with this in
5:16 the beginning, right? Because this, it can seem like overwhelming what's going on here,
5:20 right? It doesn't have to be but now you know all the stuff we got to do to connect the
5:24 server find the server, use certificates and passwords. You can see where this all comes
5:29 from. So hopefully you appreciated waiting until the end to see this. But here we are,
5:35 We should be able to run this code on a server inside that virtual network and do database things. That'll be cool, right?


Talk Python's Mastodon Michael Kennedy's Mastodon