MongoDB for Developers with Python Transcripts
Chapter: Deploying MongoDB in production (and playing it safe)
Lecture: Connecting to MongoDB via Python
0:01 Let's go back to our little play-around service app, I'll go and run this for you, it probably looks familiar,
0:08 remember our service central version 2.0, this is demo edition let me actually change that little header to prod edition, not that it much matters,
0:17 but we're going to set this up to run in our production environment. If I try to do things like list the car, it will show me my local cars
0:25 because I'm running on my Mac, however if I push this up to the server and I put it onto that fake web server server,
0:33 it's going to try to talk to local host and have a big fall, right that's not going to work well. So instead, what we need to do is we need to do is
0:42 we need to go and adjust our little connection bit here. Now, let me go and actually add some parameters to this
0:51 we're going to add a password, say user, a password, a port, a server, use ssl, and I think that'll do, okay.
1:08 So now I want to use those things to actually connect, so we're going to have like this dual mode thing going on,
1:16 and I'll do it like this, we'll say if user or password, so if either of those are set we're going to do something else,
1:25 we're going to just fall back and just do this little simple bit or right here, here I'll do a print, registering dev connection,
1:36 so go like this, and it is not going to love it, so let's go over here and give this some defaults, so none for all of these;
1:47 default here's to 27017, server=local host and use_ssl = false actually let's go and default that to true.
2:07 Okay so now I should be able to run this here and list cars actually up here we'll see registering dev connection
2:17 and let's put a little kind of indicator, something to the say
2:23 hey this is like an extra thing, so go over here and we'll say registering prod connection and I want to give it some extra info, and let's hold off
2:37 and what goes there just for a second, okay so we want to gather this up, we actually have to pass more information than this
2:43 and just to make sort of recording how we connected a little bit easier, I'm going to create this dictionary where we set all these,
2:52 so username is this, password, server, port, authentication sources, admin authentication mechanism is SCRAM-SHA-1,
3:00 ssl is, use ssl and we have to say ignore the self signed certificate if we don't do this, it will say your certificate is not valid.
3:10 Now PyCharm warns that this thing is basically missing from ssl but it's not, just ignore that.
3:18 So we're going to come over here, and we're going to do this as well let's go and say, actually let me change the order real quick,
3:30 so we're going to say all of these are keyword arguments for this method so we can just say **data
3:36 and that's going to basically set username= user, password = password and so on,
3:40 why did I put it like this— because I'd like to capture all those details. So let me just do really quick data of password equals this,
3:53 and then I'll just print out this dictionary here so registering, production, connection with those details.
4:00 Okay, so if you pass a username or password in it's going to work differently, let's just make sure everything still runs
4:07 can I list the cars, see the dev connection, yeah, excellent. So things are still working good on of the dev side of the story.
4:15 The next thing we've got to do is come over here where we're calling this, and let's just go ahead and pass in all the details here.
4:26 We wanted to use ssl that defaults to true, so that's all good. Now if I run this, you're going to see not amazing stuff
4:36 so like list is probably going to time out, it takes a while for it to time actually,
4:41 let's try to renegotiate the connection and it really doesn't want to crash but eventually this is going to timeout,
4:47 we already saw we can't connect to the server here. So let me push this up to the git repository
4:56 and then we'll get it on to the server and production and make sure everything works.
5:01 I pushed those changes to github and let's go over to the web server see I am already here, I'm just in my home directory/root
5:11 so what I want to do is I want to go and get that code over here, so we're going to go and go to the github repository for our code here
5:21 notice when I do a refresh, you should see that I just added now with production capabilities,
5:27 so let's copy this, and let's say git clone this, its a public repository so I don't need any credentials or any of that business.
5:40 Okay, so things are good, we'll go to Mongo and notice there's a source and I have 09 deploys, so if we look in here,
5:51 we've got service central deploy and service starter, server central deploy is the starter obviously it's what we started with,
5:59 the service central deploys is the one that we just changed; so for example, if we look at this you can see it's using this complicated version here,
6:13 if we look at this one, you can see we're setting a MongoDB just the way we like. Okay, so now what we have to do is run it and let's go over here
6:25 connect to the MongoDb server and say show dbs, hey there's nothing here, so let's go and run this,
6:34 so we've got our service deploy, so we'll say it Python 3 we didn't use a … or change its execution states.
6:42 Now one thing we need is we need to install Mongoengine of course so let's do this, we'll just let Python do it,
6:48 so we'll save Python 3 -m venv to create a virtual environment, here we need to apt install Python 3 -venv, try again,
7:09 so now we'll source activate this and our prompt changes. Okay good, so now we should be able to run our Python 3 thing again,
7:20 oh yeah, well it's active, we still need to pip install Mongoengine and that'll take PyMongo along with it.
7:28 I believe that failed building a wheel because set up tools is out of date, anyway, it should still work. Let's give this another shot,
7:37 now we have Mongoengine registered in a virtual environment, a virtual environment is active, our code is here a lot of deployment stuff, let's go.
7:46 Oh, look at that, so now we're registering the production connection, I mean, you probably don't want to print this out all the time
7:52 but notice the hosts, authentication, everything, it seemed to take it like the register worked
7:57 we haven't tried to talk to the database yet, let's try to list the cars. There are no cars, did that make in a dent?
8:05 No, no dent yet. Let's add a car this is going to be an F40, it's going to be built in 2010, that didn't crash, let's try to list the cars,
8:18 look at that, let's add a service record, service that car. The vin number is that, the price of the service is a thousand dollars
8:31 and this is going to be a new tires, the customer is extremely happy, loved it. Now we've got our new tires, so look at this, show dbs,
8:41 use demo dealership, show collections, db.cars.find.pretty bam, look at that, we were able to make our Python code
8:59 on the right machine with all the right settings, and all the farewell rules and everything, go over and talk to the MongoDB server.
9:07 This is pretty excellent, we can go add another car obviously like at this point once you see it creating some documents
9:14 and working to some degree everything is going to work, right, there's really nothing to it, so this is excellent,
9:20 let me just go create one more car so we have two things, this is going to be Enzo and this was build very recently
9:30 let's list the cars and add a service record for it. The Enzo needs some work, so for a 100 dollars that will be oil change,
9:41 pretty happy, yeah, one more, the same car, this is going to be 250 tire rotation moderately happy, so let's go over here and do this again.
9:57 There we go, we've got our Enzo with two service histories our F40 with one service history and so on.
10:03 Okay excellent, so it looks like this is working, I added this other record here so we have a little bit of data
10:09 because the next thing that we want to look at is how do we manage this, how do we connect our database management tools
10:15 and backup things and what not to. As far as Python goes, this baby is rocking. I guess maybe connect one more time, boom, list the cars,
10:24 there they are, yeah looks good to me.