Simulating selective recovery in Mongo. Will it work? - mongodb

This is a follow-up to my previous question:
Suppose I want to replicate only part of the databases in Mongo. I know that Mongo does not provide selective replication. I am thinking over the following workaround:
Export (I do not know how to do it in mongo) only the databases I want to replicate in primary;
Remove the secondary instance I want to recover from the replica set;
Import data to the secondary instance;
Add the secondary instance to the replica set.
Will it work ?
I am afraid that the secondary instance will copy the rest of the databases once I added it to the replica set. Is it correct ?

This will not work, also not a good idea. A replica set uses an oplog to propagate changes made to any database running on a given mongod instance.
Have you considered running a separate mongod instance containing the collection you wish to replicate?

Related

how long will it cost when I convert a mongodb standalone node to replicaset?

There is running a single node mongodb server, and about 28,000,000 documents,
now I want to convert it to replicaSet,
but I am not sure if it will cost too much time?
and also if the db will works during the initialize process?
If you config right, I mean avoid mongodb-primary-replica-becomes-secondary-if-secondary-fails, it will works during the initialize process. The initialization time depends on network bandwidth and cost of index building.
Once your primary is up and ready, documents will be accessible as primary will hold all current documents, But secondaries will take a while to sync data from primary.
See my answer to another related post MongoDB hidden secondary stuck in startup?, I'm sure it will help you.

Changing three standalone mongodb servers with exactly the same data into a replica set

I have three standalone MongoDB database servers with exactly the same data (Created from the same snapshot) How can I convert that to a replica set? (Ideally without the need to wipe, and re-sync from a selected master node)
I believe this isn't possible. You know that the data is exactly the same on all members, MongoDB doesn't.
In that case MongoDB would have to go over all the data and make sure it's identical, which is time consuming in the order of magnitude of copying all the data.

Delete database from mongodb with replica set

I have a MongoDB running with 3 member replica set. All the members are up & running.
I have one database with 4-5 collections and I want to delete that database.
What is the best way to do it. Can I just use db.dropDatabase() on primary?
Do I need to stop secondary before I drop the database? After I drop the database will secondary members sync automatically to primary? What about the memory, will it get free after I delete the database?
Yes. Just drop the database using the command db.dropDatabase() in the primary and the changes will be propagated to secondaries as well. You don't need to shutdown the secondaries.
as Anand Jayabalan said, you just need to drop the database on primary node. The replication do the rest reading the oplog for the secondaries.
From Reference links and official documentation.
The primary is the only member in the replica set that receives write operations. MongoDB applies write operations on the primary and then records the operations on the primary’s oplog. Secondary members replicate this log and apply the operations to their data sets.
Reference:
https://docs.mongodb.org/manual/core/replication-introduction/
https://docs.mongodb.org/manual/core/replica-set-primary/

creating a different database for each collection in MongoDB 2.2

MongoDB 2.2 has a write lock per database as opposed to a global write lock on the server in previous versions. So would it be ok if i store each collection in a separate database to effectively have a write lock per collection.(This will make it look like MyISAM's table level locking). Is this approach faulty?
There's a key limitation to the locking and that is the local database. That database includes a the oplog collection which is used for replication.
If you're running in production, you should be running with Replica Sets. If you're running with Replica Sets, you need to be aware of the write lock effect on that database.
Breaking out your 10 collections into 10 DBs is useless if they all block waiting for the oplog.
Before taking a large step to re-write, please ensure that the oplog will not cause issues.
Also, be aware that MongoDB implements DB-level security. If you're using any security features, you are now creating more DBs to secure.
Yes that will work, 10gen actually offers this as an option in their talks on locking.
I probably isolate every collection, though. Most databases seem to have 2-5 high activity collections. For the sake of simplicity it's probably better to keep the low activity collections grouped in one DB and put high activity collections in their own databases.

replicating mongo db oplog to another mongo db

Hi we have a production DB on mongo which has a set of collections and all the activities are loaded into an oplog. Now i wanna write a script to watch this oplog so that when ever a new record is added to the oplog, i wanna write it to a db on another dummy server. How can i go about this. I am new to mongo, so im unsure of where to start with this. any ideas would be helpful for me. I am thinking of something on the lines of
while(true)
{
watch(oplog)
OnNewEntry
{
AddToAnotherMongo(another.server.com,port,dbname,record)
}
}
There are various oplog readers which can watch and replay to a specific server. This is what replicasets do by default and there is only one primary (writer). If you just want copies of your data then replicasets are the best option, and supported without any code.
Here are some samples of code which read the oplog:
http://github.com/wordnik/wordnik-oss/
http://github.com/RedBeard0531/mongo-oplog-watcher/
http://github.com/mongodb/mongo-java-driver/blob/master/examples/ReadOplog.java
I had a simliar problem and found a quite easy solution following your opcode-example in javascript to be executed in a mongo-shell.
source code available here
With opening a tailable cursor on the oplog of the master server each operation could be applied to another server (of course you can filter by the namespace of the collections or even the databases...)