WELCOME!

What is Pinoy? It's a slang for Filipino, a person who comes from the Republic of the Philippines, or commonly known as the Philippine Islands (PI). I am a Filipino and works as an Oracle DBA in the United States. Whenever an issue arises or just for experiments, I usually forget what I did to solve/conduct them. There must be a central location where I can put everything. Thus, this blog was born. It's a collection of ideas, tips and tricks, scripts, or anything interesting that happened to me on an Oracle database or any other stuff.

The simpler, the better! has always been my motto. You don't have to complicate things. Simple things, for me, is always easier, just like my site.

FYI, anything that is written here is based on my personal experiences and is not endorsed by any other party. Also, I will not be held liable for issues that can arise by following whatever I did. Just like any other good DBA would say... ALWAYS TEST!

Hope you can find this site helpful in whatever you need and remember, I am not a guru.

Jun 24, 2013

Forcing a slave to be primary

I was playing with MongoDB and had a replica set with two members. The servers were running as guests using VirtualBox. Because of network configuration, I cannot have a static IP. Thus, everytime I start a virtual machine, it gets a different IP address. And everytime I log into that machine, it is always secondary. So, I always force it to become primary. How did I do that? Here are the steps:


1. Log into MongoDB.
2. Use the admin database. Authenticate if needed:

> use admin
switched to db admin
> db.auth("dbadmin","dbadmin")
1
rs01:SECONDARY>

As you can see, it's a secondary machine and I want it to become the primary.

3. Get the configuration and save it to a variable:

rs01:SECONDARY> x = rs.conf()
{
        "_id" : "rs01",
        "version" : 4,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "10.68.6.85:27017"
                },
                {
                        "_id" : 1,
                        "host" : "10.68.6.90:27017"
                },
                {
                        "_id" : 2,
                        "host" : "10.68.6.80:27017",
                        "arbiterOnly" : true
                }
        ]
}

4. In the above configuration, there are three (3) members of the replica set. However, only one (1) of them (_id:0) is surviving. So, remove any non-surviving machines:

rs01:SECONDARY> x.members = [x.members[0]]
[ { "_id" : 0, "host" : "10.68.6.85:27017" } ]

5. Now force reconfiguration:

rs01:SECONDARY> rs.reconfig(x,{force:"true"})
{ "ok" : 1 }
rs01:SECONDARY> rs.status()
{
        "set" : "rs01",
        "date" : ISODate("2013-06-24T20:13:57Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "10.68.6.85:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 91,
                        "optime" : {
                                "t" : 1371582629,
                                "i" : 1
                        },
                        "optimeDate" : ISODate("2013-06-18T19:10:29Z"),
                        "self" : true
                }
        ],
        "ok" : 1
}
rs01:PRIMARY>

As you see, the machine is now the primary.

No comments:

Post a Comment