I've set up 2 machines running MongoDB ( O.S. Windows ) and i've set the replication. I was wondering if it's possible to kill the process that is running as primary to check if the secondary will turn as primary.
Is it possible? How? I just killed the primary process and on my mongo client ( that is connected to primary ), it didn't turned to secondary server.
Thank in advance.
You will need to set up a third server as an arbitrer.
The arbitrer is the one that determinates which of the other servers will be primary.
Usually, it is recomended that each replica set have an odd number of servers(the miniumun would be 1 primary, 1 secondary, 1 arbitrer)
This might help you.
http://docs.mongodb.org/manual/tutorial/add-replica-set-arbiter/
The arbitrer can be in a very tiny server, since it does not stores data, its only function is to decide.
You should have at least a 3 members configuration.
Replica documentation
Set a correct replica, connect your mongoshell to the primary instance and shut it down (Shutdown), connect your mongoshell with a secondary and check replica set's status (RS Status).
That's all !
The easiest way is to shutdown your primary (assuming that primarydb is your primary and secondaryDB is your secondary. The process would be as follow
Connect to your primary and secondaries.
Stop the primary.
Look at your secondaries if a new primary is elected.
Connect to your primary in the shell
conn1 = new Mongo("localhost:31000")
primaryDB = conn1.getDB("test")
Connect to your secondaries
conn2 = new Mongo("localhost:31001")
secondaryDB = conn2.getDB("test")
...
Shutdown your primary
primaryDB.adminCommand({"shutdown" : 1})
Check if one of the secondaries is elected as the primary
secondaryDB.isMaster()
In the result of the last operation you will see which server is now the new primary (look out for the primary key).
As mentioned in the comments, you should have at least three servers (one primary, two secondaries) to get the election process working.
Related
I have some questions about the mongo replica
mongo replica
If I make 1 primary and 2 secondary MongoDB for replication. So I have 3 endpoints to 3 different DB and my apps can only write on primary DB. what if suddenly my primary shutdown and secondary DB take over the primary. Then how to automatically change the endpoint in my apps? should I use mongos (mongo routes)? but it needs sharding if I remember correctly.
Thank you.
All nodes in a replica set work together to have identical data. Secondary nodes may lag behind the primary, but you don't get "3 different DB". There is only one database of which copies exist on each node.
All MongoDB drivers know to monitor replica set members and discover which is the primary one automatically. You need to configure some drivers to do so by providing the replica set name, others do it automatically by default when they connect to a replica set node. Look up "connecting to replica set" in your driver documentation.
In a proper connection string you will provide all three RS members, e.g.
mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl
The client will detect the PRIMARY and will use it. I guess, most drivers will re-connect automatically if the PRIMARY node changes.
Most drivers will detect the PRIMARY automatically if you provide the ReplicaSet name, i.e.
mongodb://mongodb0.example.com:27017/?replicaSet=myRepl
would connect to the PRIMARY even if it is not mongodb0.example.com. However, if mongodb0.example.com does not run, then you don't connect at all. So, it is beneficial to provide all ReplicaSet members in the connection string.
See Connection String URI Format
mongos is needed only to connect to a Sharded Cluster.
my mongo url config is url = 'mongodb://1.1.1.1:27017,2.2.2.2:27017/test';
where 1.1.1.1 is primary and 2.2.2.2 is secondary.
Initially it reads from the secondary but when I shut down secondary mongo does not read from primary and fails
I want to know how can I read from primary when secondary is down
I am using mongoose.
In order for a primary to exist, the majority of voting nodes in the deployment must be available and communicating. If your deployment has a total of 2 nodes, and any node is unavailable, there is no primary.
mongo does not read from primary
There is no primary in this case, therefore, it is impossible to "read from primary".
I have a three-node replica set (1 Primary, 1 Secondary, 1 Arbiter), on three different Amazon server instances. The servers where they are hosted required a memory upgrade so I needed to shut down the MongoDB instances as well.
I shut down the MongoDB instances in this order:
Secondary
Arbiter
Primary
I used the process below for shutting down each server
use admin
db.shutdownServer()
All MongoDB instances did shut down properly without any problems. So far everything is fine.
After the Amazon server upgrade, I started the MongoDB instances in the following order:
Arbiter
Secondary
Primary
The arbiter is in arbiter mode and secondary is in secondary mode, but to my surprise the primary machine went to "RECOVERING" mode.
I don't know the reason, why the primary machine went to "RECOVERING".
I have examined logs. It is showing no member to sync...something stuff like that
My basic doubt is "PRIMARY has to be in PRIMARY until there is reconfig happens in replica set".
Am I missing a step during the shutdown of servers? Or am I missing a step during the restart of servers?
Please shed some light on this so that how can I overcome this problem. I need to shut down the MongoDB servers frequently since there is a lot of upgrades happening in Amazon servers.
After you started you replica set, you "SECONDARY" became "PRIMARY" and you "PRIMARY" was probably at secondary state after short while. To keep primary status at your "PRIMARY", you must give it higher priority than what your "SECONDARY" have.
Check with rs.conf() command.
Check here how to force node to be primary
Mongodb docs state
Do not run an arbiter on systems that also host the primary or the secondary members of the replica set.
However I could not find any explanation for this. Is it for preventing the arbiter go down together with a secondary or primary when a failure occurs ?
Technical it is possible to run a setup like this, but you lost redundancy.
Let's say you have a server with AB and C where B is an arbiter running on the same server as A. If this server goes down, you've lost your majority and B can't elect a new primary. So if the wrong server goes down you have no redundancy.
Fortunately arbiters don't save any data, so a small and cheap server instance is enough to run them.
I am experimenting the mongodb replica set, in which I started localhost:27017 as secondary and localhost:27018 as primary.
then I disconnect the localhost:27018, and I expected localhost:27017 to automatically become a primary, but it doesn't work as expected. In the shell, using command like rs.add() or rs.remove() gives out a error like ' "errmsg" : "replSetReconfig command must be sent to the current replica set primary.",'
I know this error is because the command is now running in the secondary, but since the primary is closed down already. what steps I should do now?
And also why closing down the primary doesn't allow the replica set to vote for a new primary ? what is the flexible way to make it automatic vote for a new primary
To elect a new primary in MongoDB replica set the majority of the members must vote for the new primary. That's majority of the original (or configured) members.
You configured two members - one is not a majority. You need to add a third member, either a regular node or an arbiter, if you want your failover to happen automatically.