Start single node Replica Set in GitLab CI

When i use these configurations

- name: mongo:latest
      command:
        [
          "/bin/sh",
          "-c",
          "mongod --logpath /dev/null --bind_ip localhost --bind_ip_all --replSet 'rs0' && mongo --eval 'rs.initiate(); rs.status()'",
        ]

I get following

INTERNALERROR> pymongo.errors.NotMasterError: node is not in primary or recovering state, full error: {'topologyVersion': {'processId': ObjectId('615098600402729c91e7cd5f'), 'counter': 0}, 'ok': 0.0, 'errmsg': 'node is not in primary or recovering state', 'code': 13436, 'codeName': 'NotPrimaryOrSecondary'}

How can I get this work?

Hi @Erkin_Qarayev and welcome in the MongoDB Community :muscle: !

From what I’m guessing here, I think you are trying to use Docker with Compose or maybe K8S. I’m not too sure but anyway, I think you are getting this error because when you execute the command

mongo --eval 'rs.initiate(); rs.status()'

Your mongod is - indeed - not in Primary or Secondary as it’s was just started 0.1 ms before this command, so it’s most probably in STARTUP or STARTUP2 state.

All that to say: your mongod needs a few seconds to start.

Personally, I’m using this alias to start a temporary single node RS with Docker:

alias mdb='docker run --rm -d -p 27017:27017 -h $(hostname) --name mongo mongo:5.0.2 --replSet=test && sleep 4 && docker exec mongo mongo --eval "rs.initiate();"'

And as you can see, there is a tactical sleep 4 in the middle of my command line to make sure that my mongod had enough time to start and get ready to access the first command to initialize the RS.

I hope this help.
Cheers,
Maxime.

Hi @MaBeuLux88, Thanks
Actually, I’m trying to configure MongoDB with the replica set in GitLab Ci. I’ve set the command

[
          "/bin/sh",
          "-c",
          "mongod --logpath /dev/null --bind_ip localhost --bind_ip_all --replSet 'rs0' && sleep 4 && mongo --eval 'rs.initiate(); rs.status()'",
        ]

but no chance I’m getting same error

I have a powerful PC which is able to start my mongod fast. Maybe try 15 sec and see if that works?
If it does work, then you can lower the sleep time until it doesn’t and add an extra “security” second to avoid the “coin flip”.

If that still doesn’t work… Then I guess there is something else…
Does that work if you start a mongod without the --replSet option?

I guess you need some features of the RS but this would be a first step.

Cheers,
Max.

Does that work if you start a mongod without the --replSet option?
Yes it’s working but since I am using transaction I need RS

And with sleep 15 ?

Unfortunately, It doesn’t work. What do you suggest?

You do not specify dbpath on your mongod command. This means /data/db is used.

Does this directory exists?

1 Like

I’ve used dbpath=/srv/mongodb/db0 with this command but this one is not working either

But the question remains.

When using /srv/mongodb/db0 you have to make sure it exists before your launch mongod.

Can you please confirm that the mongod node is started correctly after the first command, before you try to initiate the RS?
This was the goal of the first test (start without --replSet): making sure the node is starting correctly.

Can you connect to it and run the rs.initiate() command manually?
If yes, then this means that the node didn’t have enough time to set up and start properly and can’t accept write operations just yet. Maybe Github CI needs 2 minutes to start a node?
If you have access to the mongod logs, please share them.

@MaBeuLux88 I guess you missed this bit :wink:

@steevej
The default data path is present on the docker images so a docker run --rm -d -p 127.0.0.1:27017:27017 mongo:5 results in a working mongodb.

1 Like

Hehe :sweat_smile:. Well I guess they can be re-activated for a little test. :muscle:

That is the case for @MaBeuLux88 with the command he shared. But I am still not sure that it is the case for @Erkin_Qarayev since he is using GitLab CI and I do not know how that works.

Another thing to check is that the error is pymongo.errors.NotMasterError and that is not the error message I get. So I suspect that the error comes from what ever GitLab CI use to execute the command shared by Erkin_Qarayev.

I suppose sleep command doesnt work for Gitlab Ci because it doesn’t even wait ,
But yes without any command mongodb works as excepted , but it returns that you need replicaset for transaction

@Erkin_Qarayev is effectively bypassing the docker-entrypoint pieces that do a lot of the sanity checks that help with a successfully running mongod.

@Erkin_Qarayev you may want to create your own container and invoke or extent the docker-entrypoint.sh

1 Like

I’ve found a workaround installing MongoDB within the image

stages:
  - test
test:
  stage: test
  image: python:3.8-bullseye 
  services:
    - name: mongo:latest
      command:    [
          "/bin/sh",
          "-c",
          "mongod --logpath /dev/null --bind_ip_all --replSet 'rs0'",
        ]
  before_script:
    - wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc |  apt-key add -
    - echo "deb http://repo.mongodb.org/apt/debian buster/mongodb-org/5.0 main" |  tee /etc/apt/sources.list.d/mongodb-org-5.0.list
    - apt-get update
    - apt-get install -y mongodb-org
    - mongo --host mongo --eval "rs.initiate()"   
  variables:
    MONGODB_HOST: mongo:27017
    PYTHONWARNINGS: ignore::DeprecationWarning
  script:
    - mongo --host mongo --eval 'rs.status()'
1 Like