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

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.

Related

mongorestore - AtlasError - getting non-bucket system collections is unsupported

Failed: (AtlasError) getting non-bucket system collections is unsupported
I'm trying to use mongorestore to migrate data from one database to another. Both on Atlas. The dump is working fine, but mongorestore is outputting the below messages. I have no clue what this means and a google search renders nothing remotely close. I've added hyphens to the beginning of each line of the output to make it more readable.
-- using write concern: &{majority false 0}
-- will listen for SIGTERM, SIGINT, and SIGKILL
-- connected to node type: replset
-- The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION}
-- got error from options parsing: (AtlasError) getting non-bucket system collections is unsupported
-- Failed: (AtlasError) getting non-bucket system collections is unsupported
-- 0 document(s) restored successfully. 0 document(s) failed to restore.
The command I'm running
mongorestore --uri="mongodb+srv://username:password#hostname.mongodb.net/$DEV_DATABASE" \
--preserveUUID \
--drop \
--nsFrom="$PROD_DATABASE.*" \
--nsTo="$DEV_DATABASE.*" \
--verbose \
"dump/$PROD_DATABASE"
I've also tried creating an archive file with mongodump and using that with --archive="filename", as well as piping stdout to mongorestore. I've also checked that the user I'm using has the correct privileges. They have the role of Atlas admin, which I'm assuming is correct. The dev cluster I'm trying to restore to is an M0 if that makes any difference.
I should also point out that I have minimal Mongo management experience, so I'm sure there's something I've overlooked. Thanks for your help.
MongoDB records the collection UUIDs in a separate system collection.
The --preserveUUID option instructs mongorestore to create the collection, and force it to use the UUID from the source system.
The error message indicates that Atlas is refusing to allow you to access or modify that system collection.
Run without the --preserveUUID option when restoring to Atlas.

What's the equivalent of `rs.slaveOk()` for config file in MongoDB?

I have set up my MongoDB on local machine with replicaSet, and I have this error:
An error occurred while loading navigation: 'not master and
slaveOk=false': It is recommended to change your read preference in
the connection dialog to Primary Preferred or Secondary Preferred or
provide a replica set name for a full topology connection.
If I don't exec this command on secondary servers,
rs.slaveOk()
After using the command, the problem is solved, but only temporarily. After restarting the servers, the above error pops up again, I again have to stop it using the command.
How can I somehow define the slaveOk in a config file, so that I don't have to allow slaveOk each time I start the server?
Your connection string should look something like this:
mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl
If you are connecting from shell
mongo "mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl"
OR
mongo --host myRepl/mongodb0.example.com.local:27017,mongodb1.example.com.local:27017,mongodb2.example.com.local:27017
Where myRepl is the replica set name, mongodbX.example.com.local:27017 are your nodes.

Mongodump with Replica Set : how to force on secondary?

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

connecting to remote mongo db server and running clone

I am migrating from server OLD at the old hosting company to server NEW at the new hosting company.
I want to run the clone command so I clone the mongoDB from OLD to NEW.
For OLD:
The public ip address is: 44.55.66.77.
The machine login user name is: admin, and the password is password
What is the right way to do this?
So far I can't even log into the server OLD
So far I have tried the following command prompts on NEW:
mongo -u admin -p password 44.55.66.77
mongo remote-ip:44.55.77.66 -u admin -p password
That don't work
I also tried this from mongo shell:
db.CopyDatabase('OldDb', 'NewDb', '44.55.66.77', 'admin', 'password')
and I get: the "could not connect to server" error message
Aside from firewall considerations in order to copy data between MongoDB servers, db.copyDatabase() (aka the copydb command) has a number of important usage caveats including:
copydb does not produce point-in-time snapshots of the source database; writing data to the source or destination database during the copy process will result in divergent data sets
copydb does not lock the destination server during its operation, so the copy will occasionally yield to allow other operations to complete.
There is also a known issue that copydb may not work with the role-based privileges in MongoDB 2.4 if you have authentication enabled (see SERVER-8213, which was recently fixed in the 2.5.x development releases).
A much better approach to migrating your data would be to restore from a normal backup using mongodump/mongorestore or file system snapshots. The Backup & Recovery section of the MongoDB manual has tutorials covering procedures for different deployment types.

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).