βš— Kevin Summersill πŸ”‹
1 min readFeb 9, 2023

--

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN:portN]]/database_name?replicaSet=replica_set_name&readPreference=preference_mode

Here's an explanation of the components of the connection string:

mongodb:// - this is the required prefix for a MongoDB connection string

[username:password@] - this is an optional component that specifies the username and password for authentication. Replace username and password with the actual values for your database.

host1[:port1][,host2[:port2],...[,hostN:portN]] - this component specifies the host names and ports of the replica set members. Replace host1, host2, ..., hostN with the actual host names and replace port1, port2, ..., portN with the actual ports. If you do not specify a port, it defaults to 27017.

/database_name - this component specifies the name of the database to connect to. Replace database_name with the actual name of your database.

?replicaSet=replica_set_name - this component is optional and specifies the name of the replica set. Replace replica_set_name with the actual name of your replica set.

&readPreference=preference_mode - this component is optional and specifies the read preference for the client driver. Replace preference_mode with the desired read preference mode. For example, &readPreference=primary means the client will only read from the primary member of the replica set.

Example

mongodb://myuser:mypassword@host1:27017,host2:27018,host3:27019/mydatabase?replicaSet=myreplicaset&readPreference=primary

--

--

βš— Kevin Summersill πŸ”‹
βš— Kevin Summersill πŸ”‹

Written by βš— Kevin Summersill πŸ”‹

Enterprise Solution Architect | Certified K8s Administrator/Developer βš“ | SAFe SPC | Cert Terraform | AWS Solutions Architect | Dev*Ops/GitOps Engineer πŸ”₯

No responses yet