How to format the connection string for a MongoDB replica set in Sitecore?

4.1k views Asked by At

I have configured MongoDB for Sitecore using a replica set. I set up keyfile access control and added a user. But I'm facing problems while creating connection strings.

Lets assume my replica set name is rsHelloWorld with several mongod instances: localhost:21017,localhost:21018,localhost:21019,localhost:21020,localhost:21021(arbitor)

username: mongo_admin
password: test@123

The default connection string in Sitecore is:

  <add name="analytics" connectionString="mongodb://localhost/analytics" />

How to specify the connection string for my MongoDB database with a replica set and authentication?

1

There are 1 answers

0
Dmytro Shevchenko On BEST ANSWER

Sitecore xDB uses the standard MongoDB connection string format.

In the provided example, the connection string will be this:

mongodb://mongo_admin:test%40123@localhost:21017,localhost:21018,localhost:21019,localhost:21020,localhost:21021/?replicaSet=rsHelloWorld

Note that I have replaced the @ symbol in your password with %40. This is because @ in a connection string is the separator between credentials and host names. See more here.

Also, keep in mind that you don't have to specify all of your servers in the connection string. You need to specify at least one, and upon connecting to it, xDB will receive full information about the replica set, including the addresses of all data-bearing nodes. Nevertheless, it is a best practice to include several servers in order to ensure that if one of them goes down, the application will still find a server to connect to. In your case, including the arbiter doesn't make much sense since xDB (or any other MongoDB client) will never have to communicate with it.