MongoDB for Developers with Python Transcripts
Chapter: Deploying MongoDB in production (and playing it safe)
Lecture: Connecting to MongoDB via Python

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