9

I have a strange problem with my MongoDB Replica Set backup. I have 2 servers (one primary and one secondary) and I run a backup task every 2 hours.

I use this: mongodump.exe --db MyBase --out "d:\Backups"

But, when I run this backup, my client application (c#) throw errors like this :

MongoDB.Driver.MongoConnectionException: Unable to connect to a member of the replica set matching the read preference Primary

I thought that the mongodump does not have any impact like this on client applications. So, that to say.

I'd like to force my backup operation on the secondary server only. How can I proceed? What is the command to run?

Thank you for your help.

Messa
  • 24,321
  • 6
  • 68
  • 92
Fred Mériot
  • 4,157
  • 9
  • 33
  • 50

2 Answers2

9

we can also set readPreference in the mongodump command. This feature is available starting mongoDB version 3.2

--readPreference 'secondary'

https://docs.mongodb.com/manual/reference/read-preference/#replica-set-read-preference-modes

sqllearner
  • 105
  • 1
  • 7
1

If you want to backup secondary, you should write your 'mongodump' command on server, where secondary 'mongod' is running.

Or you can explicitly set secondary host and port:
mongodump.exe --host <secondary_host> --port <secondary_port> --db <db_name> --out <out_folder> --oplog

Ivan.Srb
  • 1,851
  • 1
  • 14
  • 10
  • 5
    A replicaSet connection string is a lot more stable than which server is primary/secondary. The idea of using a replicaSet connection string is a lot more useful than having to change code every time the primary/secondary swap roles. – adamb0mb May 13 '15 at 21:38
  • @adamb0mb how does one specify a replicaSet connection string? The manual for mongodump makes no mention of such a thing and passing the URI as the host parameter results in an error (no reachable servers). – Jason R. Coombs Dec 18 '15 at 20:57