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