Restoring from a single instance mongodb to an empty replica set - mongodb

I have a mongodb installed on windows server. I take regular backups of the data/db folder using Rackspace backup.
I created a deployment of a mongodb replica set with 3 ubuntu servers using Rackspace deployments. Now I want to move the data on windows to the empty replica set. How can I do it?
I tried copying the contents of data/db on windows to var/lib/mongodb on the primary replica set. It didn't work.
For some reason the var/lib/mongodb on the ubuntu machines does not contain data/db directory. When I create a new db the db files are created on var/lib/mongodb directory.

The difference in data directories is fine .. on Windows the default dbpath will be c:\data\db; the Ubuntu package sets the dbpath to /var/lib/mongodb instead.
Since you are starting with an empty replica set (and using a backup from a standalone server), the most straightforward approach would be to:
Stop all the mongod servers for the replica set (you definitely don't want to copy data files directly into a running instance!).
Remove any files that are already in the /var/lib/mongodb data directory.
Copy the data files from your standalone MongoDB backup into /var/lib/mongodb on one of your replica set servers. This server will become your primary to set up the rest of the replica set.
Start up this primary making sure to include a replSet name in your configuration file. You may already have this set from your "empty" replica set that you already created.
Run rs.initiate() in the mongo shell to create the initial configuration on the primary.
Start up your additional servers as members of this replica set: they need the same replSet name configured.
Use rs.add(..) to add your additional servers from the mongo shell on your primary. Assuming the add is successful (i.e. the mongods can connect to each other), this will begin the process of initial sync (copying data from the primary) and the new hosts will become secondaries after they have finishing initial sync.
This is essentially the same steps as the deploy a replica set tutorial, except you are copying over your data first.

The problem could be related to the configuration file of mongodb
locate the file mongodb.conf and edit the dbpath parameter, check if the path really exist, and if it doesn't create the missing directories. Also check permissions in that path
anyway i don't know if its the right way to just copy the datafiles in a new location, i guess you should use mongo import/export

Related

How to specificy path of new collection in Mongo DB?

I am running Mongo on Windows 10. Data access is made trugh Pymongo (Python). All my data is stored in an old large HDD, but I would like to have some smaller collection stored on a much faster SSD? When I create a new collection either trough Compass or Pymongo there is no option to specify the path on disk.
Is it possible to learn such power?
If you want to have databases in different disks under the same dbPath , this is the option:
Add the option --directoryperdb in the mongod config file or at startup.
db.createDatabase("newDatabase")
You will see inside the dbPath folder for the new database like:
\dbPath\newDatabase
Stop the mongodb process.
Copy the content from \dbPath\neWDatabase to your SSD let say:
ssd:\newData
make link to the folder with:
mklink /d \newData \dbPath\newDatabase
or follow this tutotial
Start the mongodb process again.
Note:
As suggested by #Wermfried in the comment it is safe to have the option --directoryperdb set initially in the member before to get populated ...

Lost data upon migration from standalone server to one node replica set

I was attempting to migrate my standalone mongo server to a single node replica set using the following instructions:
https://docs.mongodb.com/manual/tutorial/convert-standalone-to-replica-set/
Upon restarting the docker container with the -replSet argument and initiating within the mongo shell, I noticed that the replica set was created, but virtually all of my database collections disappeared. The /data/db folder argument hasn't changed and I haven't assigned a different config file. Is there any hope in recovering my data?

Where are mongo replication settings stored?

After a complete reinstall of mongo, it has still remembered the replica set configuration from before; where has it stored this?
I installed mongo on a linux server, using our project's user account, into a directory owned by that user. I set up replication and had it working fine. Then, to test out some finer install points, I removed the whole mongo directory and did a reinstall. Entering into mongo, I found that the replication was already set up as before; so it would appear that mongo is storing the information somewhere.
I have checked several areas which might have been holding the replica set config:
First, in the mongo directory, but that was deleted.
In some traditional linux structure probably owned by root, but the project user does not have root access and mongo, run by the same, should not either.
The project user's home directory. Now this does have a .dbshell file containing the command line history, but only that. I did not see any other files there that related.
Some location specified in the mongo configuration. But I only have two paths mentioned in there, one for the system log (systemLog.path) and the other for storage (storage.dbPath), and the both point to the mongo directory, which was deleted.
Does anyone know where mongo is storing this configuration information?
The replication data is stored in the local database in each node (as of MongoDB 3.2.9). This database contains information about the replica set, and also contains the oplog (the oplog.rs collection). The replica set information is stored in the system.replset collection in this local database.
The physical files for this database (and also for other databases) are stored in the dbPath directory, which can be configured using:
The --dbpath parameter when starting mongod
The storage.dbPath setting in the configuration file
The default dbpath value is /data/db
Your replica set setting will not be retained if you emptied the dbpath directory. If you find that the replication settings are being retained, it is possible that the dbpath setting is incorrect.

Data storage in mongodb

first of all please forgive me for asking a silly question but I am new to mongodb and just installed it on my windows platform by following this installation guide :http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/
It says "MongoDB requires a data folder to store its files. The default location for the MongoDB data directory is C:\data\db.You can specify an alternate path for data files using the --dbpath option to mongod.exe."
So I created a folder d://data/db in my computer and issued a command
C:\mongodb\bin\mongod.exe --dbpath d:\mongodb\data
Then it says
"At the mongo.exe prompt, issue the following two commands to insert a record in the test collection of the default test database and then retrieve that record:
db.test.save( { a: 1 } )
db.test.find()"
I issued this to commands to save and retrieve the objects and its working fine but what is this default test database? where is it? Moreover where this object is stored? Where I can find this file?
what is this default test database?
When you connect to a mongod server without specifying a database, a default database "test" is selected. Since databases are created lazily, it may not even exist until you write to it.
db.test.save( { a: 1 } )
After this line is executed, database with current name ("test" by default) is created (if didn't exist already) and in it, collection "test" is created (if didn't exist already).
where is it? Moreover where this object is stored? Where I can find this file?
All databases are ultimately stored as files in your data dir. Look for "test.*" files there.
mongod.lock, is the file which provides the PID of your running mongod instance. When you start a mongod instance, MongoDB check if the lock is empty to start cleanly mongod. Then MongoDB registered the PID number of the running mongod instance in this lock file.
MongoDB delete the contains of this lock file when you shutdown cleanly your server,
mongod --shutdown -- dbpath <path name> --port <port number>

MongoDB replica set to stand alone backup and restore

For development reasons, I need to backup a production replica set mongodb and restore it on a stand alone, different machine test instance.
Some docs are talking about the opposite ( standalone 2 replica-set ), but I cannot find his downgrade/rollback way.
What's the way to go, in this case ?
No matter how many nodes you have in a replica set, each of them holds the same data.
So getting the data is easy - just use mongodump (preferably against the secondary, for performance reasons) and then mongorestore into a new mongod for your development stand-alone system.
mongodump does not pick up any replication related collections (they live in database called local). If you end up taking a file system snapshot of a replica node rather than using mongodump, be sure to drop the local database when you restore the snapshot into your production stand-alone server and then restart mongod so that it will properly detect that it is not part of a replica set.