How to Make Mongodb replicaset as master - mongodb

Recently because of an unknown issue our MongoDB hosted on a GCP compute VM has stopped, we were unable to restart it because it's throwing MongoDB.service is not found. so we reinstalled the MongoDB after taking a backup of all .wt files in the DB path. once we've reinstalled the MongoDB we copied the files back and we can't see the data in the DB. we tried the --repair flag but still no use.
is there a way we can get this working?
The other thing is we've taken the VM snapshot from the day before the crash. there we can see the data in MongoDB only if we run the method rs.slaveOk(). I think we can't use that DB as the primary db. is there a way that we can use this as a primary db.
I'm relatively new to the concept of replica set, Master/slave any suggestions and questions are welcome
Thanks

If you see the data from the snapshot with rs.slaveOk() it is easy to recover , you can reconfigure the member as standalone PRIMARY with this steps:
Get the current config:
cfg = rs.conf()
printjson(cfg)
Set in the temp variable cfg only the available member(in the example it is the first in the cfg with id:0 ):
cfg.members = [cfg.members[0]]
Reconfigure the replicaSet only with the available member:
rs.reconfig(cfg, {force : true})
(Don't forget to add the {force:true} option since it is the only way to reconfigure from SECONDARY)
If all is good with this member and it successfully elect as PRIMARY , you can add other new members with rs.add() ...

Related

Need to change storage db path in mongo secondary node of replica set

We have 3 members replica set of mongo db (MongoDB server version: 4.2.9). one of the member (secondary) is running on default storage db path and this storage getting nearly full. We need to change path of that specific secondary node of 3-member replica set. I am new to mongodb. I need your expertise to accomplish this task.
MongoDB server version: 4.2.9
OS Version: CentOS Linux release 7.8.2003 (Core).
Regards,
Option 1: ( preserving the current already synced data )
Stop the mongod service:
mongo --host xxxx --port yyyy
use admin
db.shutdownServer()
Copy the data from old location to the new location:
cp -R /oldPath/ /newPath/
Change in the /etc/mongodb.conf the dbPath to point to the new location.
Start the mongodb service.
Option 2) Init sync the member from the other members.
Same like option 1) , just ommit the point 2) just you will need to wait abit until the member init sync ( copy the data ) from the other members , it depends on data size and compression.
Option 3) Better high availability:
From the PRIMARY member add another member on that will use the new path:
rs.add("newMember:XXXX")
Wait until the new member init sync.
Remove the old SECONDARY member:
rs.remove("oldMember:YYYY")
Clean the old files and configuration.
( small disadvantage with this option that you will need to change the port for the new member if on the same host )
Option 2) seems best suitable for your case ...

Restart MongoDB Replica Set

I tried to search but most of the solutions are very old, and I feel there will be a better solution available which I am unable to find.
Problem
I created the replica set, and it's working. I tried to connect my docker backed application, and it was connecting to it.
Later I changed the dbpath from dbpath=/var/lib/mongodb to the new path dbpath=/data/mongodb. I didn't restart the replica set after this change.
I ran my backend application and it couldn't connect to the replica set. I think its because I didn't restart the MogoDB ReplicaSet. I am using Mongo version 4.4.5
I want to restart the MongoDB Replica Set to check if this can solve the issue.
My connection string :
mongodb://username:pwd#prod-privateIP-1:27017,prod-privateIP-2:27017,prod-privateIP-3:27017/prodDB?replicaSet=replica1&authSource=admin
What is the best practice/recommended approach to restart the MongoDB Replica Set ?

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?

Can't create user

I was using MongoDB version 2.6.6 on Google Compute Engine and used the click to deploy method.
rs0:SECONDARY> db.createUser({user:"admin", pwd:"secret_password", roles:[{role:"root", db:"admin"}]})
2015-07-13T15:02:28.434+0000 Error: couldn't add user: not master at src/mongo/shell/db.js:1004
rs0:SECONDARY> use admin
switched to db admin
rs0:SECONDARY> db.createUser({user:"admin", pwd:"secret_password", roles:["root"]})
2015-07-13T15:13:28.591+0000 Error: couldn't add user: not master at src/mongo/shell/db.js:1004
I had a similar problem with mongo 3.2:
Error: couldn't add user: not master :
When trying to create a new user, with root role.
I was using only a local copy of mongo.
In my mongod.conf file I had the following uncommented:
replication:
replSetName: <node name>
Commenting that out and restarting fixed the problem. I guess mongo thought it was part of a replication set, and was confused as to who the Master was.
Edit:
I've also found that if you ARE trying to setup a replication set, and you get the above error, then run:
rs.initiate()
This will start a replication set, and set the current node as PRIMARY.
Exit, and then log back in and you should see:
PRIMARY>
Now create users as needed.
I ran into this error when scripting replica set creation.
The solution was to add a delay between rs.initiate() and db.createUser().
Replica set creation is seemingly done in background and it takes time for the primary node to actually become primary. In interactive use this doesn't cause a problem because there is a delay while typing the next command, but when scripting the interactions the delay may need to be forced.
MongoDB will be deployed in a cluster of Compute Engine instances (also known as a MongoDB replica set). Each instance will use a boot disk and separate disk for database files.
Primary and master nodes are the nodes that can accept writes. MongoDB’s replication is “single-master:” only one node can accept write operations at a time.
Secondary and slave nodes are read-only nodes that replicate from the primary.
Your error message looks like you are trying to add the user on the secondary. Try adding the user in the primary.
I ran into this issue when I thought I was running mongo 3.4 but it was mongo 3.6. Uninstalling 3.6 and installing 3.4 fixed my issue.

Restoring from a single instance mongodb to an empty replica set

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