MongoDB for Developers with Python Transcripts
Chapter: Working with MongoDB directly from Python: PyMongo
Lecture: Concept: Connection strings

Login or purchase this course to watch this video and the rest of the course contents.
0:01 So in our example we saw that we pass a connection string to the Mongo client and it was super simple,
0:06 it was just the MongoDB scheme and local host and the default port, like I said, we could even omit the the connection string,
0:13 I believe it would still be totally picking all the defaults. So let's look at some non default options.
0:19 So here, if I want to connect to a remote server and I've either put some kind of dns records somewhere or I've just hacked my local hosts file to say
0:28 there's a thing called mongo_server which is maybe within a virtual private network or at least in the same data center zone,
0:36 if I'm doing cloud hosting like a Digital Ocean or something like this, and if I want to connect it on the default port, which is still 27017,
0:45 I could just say mongodb://mongo_server, and then we could connect that way. Well, maybe you want to connect on an alternate port,
0:53 so port 2000, instead of 27017, this is probably a good idea, there's a lot of people scanning the internet for open MongoDB ports,
1:02 27017, 27018, up to 20020 I believe, it's probably the range that they're looking at, because different services run on different ports,
1:12 like replication versus sharding versus whatever. So you probably don't want to run on that port, and when we get the deployment section,
1:19 we'll look at all the steps we need to take in order to make our server safe, so be sure you do not put MongoDB in production
1:26 until you watch that chapter at the end of the course, but let's just assume that one of the things we might want to do is
1:31 run on a non default port, we just obviously like any web address type thing, we just say mongodb://mongo_server:2000
1:39 okay great, so now we have a separate server on a non default port we probably want to have authentication so if we had a user name and password
1:48 again we'll talk about this in the deployment section at the end we would have jeff:supersecure, so user name jeff
1:54 ultra secure password is supersecure, and then we can have everything else. And if we wanted to talk to a replica set, so this is a set of cooperating
2:03 duplicated fail over MongoDB servers that can be working together so in case one of them goes down, or you have to take one offline for some reason,
2:13 it will just switch over and a different server will become the primary and start to store the data.
2:19 This doesn't lead to eventual consistency and things like that, there still is one primary place things go to,
2:23 but depending on how the state of the cluster is, it could be any one of these replicas, and the replica sets.
2:29 So here we would say server one port one, server two port two, server three port three— well, the first two are actually
2:35 both running on the same machine, so in case the process dies but we also have a separate server, Mongo server two
2:40 that is running on a different port as well, in fact, this might not be all of the replica sets,
2:45 all the servers in the replica set, this might just be sufficiently many, so that once it connects it finds all the others,
2:51 and then it will start participating in all of them. And we also need to say replicaSet=prod or whatever we're calling a replica set.
2:58 So we have all these options in terms of connection strings and then once you have this, well you pretty much use it the same way,
3:03 you create a client by passing the connection string off to it and it figures out all the details for you.

Talk Python's Mastodon Michael Kennedy's Mastodon