Convert standalone docker-compose version to replica set primary

I have the following docker-compose file that starts my MongoDB as standalone (in PROD).

version: '3.1'

services:

  mongo:
    image: mongo
    restart: always
    # entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
    ports:
    - 27017:27017 # admin
    environment:
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: superlongpassword
    volumes:
    - /host/mongodb:/data/db

My goal: convert this instance to the PRIMARY in a replica set.
I have another instance running, freshly set up, currently doing nothing (if this works, I’ll add a 3rd).

My thinking was that, stopping the instance, uncommenting the ‘entrypoint’ line and bringing the instance back up, would convert the standalone instance into a replica set instance, as described in this tutorial .
However, when doing this, I get:

(in mongosh):
test> rs.initiate()
MongoServerError: already initialized (PS: I had tried this before...)
test> rs.status()
MongoServerError: Our replica set config is invalid or we are not a member of it.

Any hints in what I’m doing wrong?

What message you got when you ran rs.initiate() first time?
Once you start your mongod with replset and run rs.initiate() it should become primary
Check this link.I don’t know about docker

I wish I knew :frowning:

Is there a config file somewhere that stores info about whether a node is in a replica set or not? Can you revert a node in a replica set to again be a standalone version (such that, when started again enabling replica set and rs.initiate(), it will actually say it’s in a replica set)?

Mongod.conf is the configuration file where we define replica parameters but in your case whatever commands mentioned at entry point are run when you initialise your docker
So comment this entry and restart to get back to standalone but advice you not to do experiments on prod
As mentioned before I don’t know about docker setup but many docs available
Check this link

The error you are receiving is consistent with the scenario that the replSet was previously initialized and then the --replSet name changed.

Once you are back to standalone you need to drop the local database to remove the previous replicaSet configuration which will allow you to try again.

//run in mongosh
use local
db.dropDatabase()

I set the parameters using command command in docker-compose:

version: "3.8"
services:
  mongo-0-a:
    image: mongo:6.0
    command: --wiredTigerCacheSizeGB 0.25 --replSet s0 
    volumes:
      - mongo-0-a:/data/db
volumes:
  mongo-0-a:

if you start your instances with this parameter set, no matter how many instances you have, you use rs.initiate() only once on one of them and then use rs.add() to add other members.

you could also use a configuration on initialization if you knew what would their IP addresses be. it is a tedious thing to set up but not impossible (play on compose file):

rs.initiate({
   _id: "rs0", members: [
      { _id : 0, host : "mongo1:27017"},
      { _id : 1, host : "mongo2:27017"},
      { _id : 2, host : "mongo3:27017"}
] } )

Dropping the local db and restarting with the replica set option worked for me.

Next issue: I’m running version 6.0.3 and tried to add a Raspberry Pi in the mix. However, the last version that works is 4.4. rs.status() complains that:

remote host has incompatible wire version: Server min and max wire version (9,9) is incompatible with client min wire version (17,17).You (client) are attempting to connect to a node (server) with a binary version with which you (client) no longer accept connections. Please upgrade the server’s binary version.

Can I downgrade my 6.0.3 version to be 4.4, or is that not advisable? If not, how would you suggest I add a replica set member with 4.4?

Ok, I think I got why that has worked. the problem here is that you do not add some PRIMARY to your replica set. you wouldn’t want to do this every time you try to add another machine as primary.

  • start your new machine (container) to be part of the replica set
  • login to the current replica set.
  • add the new member and set its priority higher than others to be elected as primary.

all members can be a primary depending on their priorities (or never if set otherwise) through a voting system amongst the members.

as for downgrading, can you please check this answer about running version 6 on your Pi: How to install mongodb 6.0 on Ubuntu 22.04 - #4 by Yilmaz_Durmaz

PS: as I stated in that other post, I run version 6 on ubuntu-in-a-docker-container, and should in theory run for you too. the responsibility of breaking things is yours.