Mongodump with Replica Set : how to force on secondary? - mongodb

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.

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

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

Related

Archive replica set in mongodb

Friends,
I'm a MongoDB DBA and I'm totally new to Mongo and also to DBA role.
I want to archive data that is one month old in a 3 node replica set. mongodump is one option I can achieve this but my client asks me if there are any options. So please could you suggest the available options for archiving the data in replica set.
Many thanks!!!
Yes, we have multiple options.
We can able to take from tools like,
OPS Manager
Atlas
Scripting via
Other than that, if you are trying in manually use MongoDUMP
mongodump --archive=test.20150715.gz --gzip --db test
or
mongodump --gzip --db test
EXTRA
if you want to restore same archive file,
mongorestore --archive --port 27017 --host abc.mongo.com
Refer:
https://docs.mongodb.com/manual/reference/program/mongodump/
https://docs.mongodb.com/manual/reference/program/mongorestore/

How to restore Mongo(WT engine) only with collection-0-****.wt file?

My mongodb can't lanuch now, when I want start mongo got error ***aborting after invariant() failure
Now I want to restore collection-0-****.wt file to a new db, is this possible?
As at MongoDB 3.2, only full backups of WiredTiger data directories can be copied into a new instance. WiredTiger collection or index files aren't self-contained; they rely on other metadata in the WiredTiger.* catalog files. The invariant/assertion you are getting on startup is expected if data files are incomplete or inconsistent.
If you want to backup and restore a single collection, you should use mongodump and mongorestore, eg:
mongodump --db test --collection northwind --host host1
mongorestore --db test dump/test/northwind.bson --host host2
For supported full backup procedures, see: MongoDB Backup Methods.
I had the same issue and after spending 5 hours doing everything, found this.
https://medium.com/#imunscarred/repairing-mongodb-when-wiredtiger-wt-file-is-corrupted-9405978751b5
You will need to restore 1 collection at a time(a few at once when you get the hang of it), but it works!

How to dump (backup) several tables (collection) and recover from MongoDB?

I'm new in MongoDB and my english is too bad - so, i can't understand mongodb documentation.
I have to dump (backup) several tables (collection) from MongoDB base - how I can do it?
Which utility I have to use for recover collections from backup?
Thank you for your attention for my question!
Start your mongod server. Assuming that your mongod server is running on localhost and port 27017. Now open a command prompt and go to bin directory of your mongodb instance and type the command:
mongodump
This command will back all data of the server to directory /bin/dump/
To backup only specific collection, use:
mongodump --collection YOUR_COLLECTION_NAME --db YOUR_DB_NAME
On a good-bye note, to restore/recover data use command:
mongorestore

Force mongodump from secondary server "not master or slaveOk=false"

Trying to export small data set from a mongo server (2.4.3)
The environment has a primary and one secondary instance, but the secondary shows slaveOk=false, and I can't fix/connect to the primary.
Connecting by shell works; auto routes me to secondary and shows warning:
assertion:{ $err: "not master and slaveOk=false", code 13435}
Can't do any queries unless I do:
rs.slaveOk()
...that's fine, but works only for the current connection. Once I exit and try do do mongodump, I get the same error as above.
> mongodump -h myhost:33478 -d database -o /mydir
connected to: myhost:33478
assertion:{ $err: "not master and slaveOk=false", code 13435}
Did not find any option in mongodump to force exporting from a server in such a state.
I'm assuming this can be achieved through scripting, but after searching around, could not find anything to switch secondary to "ok" state and executing mongodump with the same connection.
Pardon the ignorance, still very new to Mongo.
That means you driver is configured to use Master only and host you gave to mongo connector is a slave.

Restore a mongodb in meteor production server

I'm trying to copy all the data from my localhost default meteor mongo database to the production server to use it in "app.meteor.com".
I tried to use mongorestore usinng the information provided by "meteor mongo --url app.meteor.com", but it does not modify any document.
Moreover, when I connect to mongo database of the server, I can only read (find) documents. When i use update or insert functions it says "not master"
Run
~/meteor/meteor mongo -U yoapp
You will get something like this
mongodb://client:387shff-fe52-07d4-69a4-ba321f3665fe7#c0.meteor.m0.mongolayer.com:27017/yoapp_meteor_com
Take the values and put into mongorestore like this
mongorestore -u client -p 387shff-fe52-07d4-69a4-ba321f3665fe7 -h c0.meteor.m0.mongolayer.com:27017 -db yoapp_meteor_com /home/user/dump/yoapp
I just dumped a prod app to my local dev machine. Testing some new changes. Pushed code to a staging install on meteor.com and then used mongorestore to populate the staging database.
Moreover, when I connect to mongo database of the server, I can only read (find) documents. When i use update or insert functions it says "not master"
It's probably because the server you are connecting to is not a MASTER, but a SLAVE in a replica-set. Slaves are readonly and all writes need to be directed to the master. You can get the master hostname:port by querying rs.conf() and looking at the entries in members. See http://docs.mongodb.org/manual/reference/replica-configuration/
Get the master and then try mongoimport/mongorestore on it.
You should also tail the mongod logs on your production server, to see if there are any errors on import (provided you have access).