exception: hosts cannot switch between localhost and hostname I created a replication set. 
I added localhost in the set in the beginning, but when I try to edit the member with the actual hostname. I get error "exception: hosts cannot switch between localhost and hostname"

I need to get rid of localhost:27017 because, otherwise, it doesn't let me enter any other member as hostname (i.e. non-localhost address)

my-rs0:PRIMARY&gt; cfg=rs.conf();
{
    "_id" : "my-rs0",
    "version" : 1,
    "members" : [
        {
            "_id" : 0,
            "host" : "localhost:27017"
        }
    ]
}
my-rs0:PRIMARY&gt; cfg.members[0].host="my-server04:27017"

my-rs0:PRIMARY&gt; cfg
{
    "_id" : "my-rs0",
    "version" : 1,
    "members" : [
        {
            "_id" : 0,
            "host" : "my-server04:27017"
        }
    ]
}


using rs.reconfig(cfg);

my-rs0:PRIMARY&gt; rs.reconfig(cfg);
        {
            "errmsg" : "exception: hosts cannot switch between localhost and hostname",
            "code" : 13645,
            "ok" : 0
        }


no luck with rs.add("my-server04:27017") or rs.remove("localhost:27017") as well.

 my-rs0:PRIMARY&gt; rs.add("my-server04:27017");
    {
        "errmsg" : "exception: can't use localhost in repl set member names except when using it for all members",
        "code" : 13393,
        "ok" : 0
    }


I have tried all the reconfiguration methods mentioned here Replica Set Reconfig steps
But, none fixing above issue. Already spent hours, I am really frustrated.
You need to basically reset the config, which is stored in the local database, and then start from scratch.  Assuming you still just have the one server configured as above, restart the instance without the --replSet option, then drop the local.system.replset collection.  Once that is done you may restart again with --replSet and you can re-run rs.initiate() but this time pass in the configuration document instead of letting it auto-discover, something like this:

rs.initiate({"_id" : "my-rs0", "version" : 1, "members" : [{"_id" : 0, "host" : "my-server04:27017"}]})


You can also solve this by making sure that the name resolves correctly and so is automatically populated by the default rs.initiate() command, but the above removes any doubts.