Mongo Replica Set

10 months ago

Description: Instant MongoDB replica set for high availability and redundancy.

Category: Storage

URL: https://railway.app/template/ha-mongo

9 Replies

brunototHOBBY

7 months ago

I'm having problems connecting locally to the replica set mongodb deployed from this template. I follow all instructions defined here but instead of using private railway domain I use public one. This gets me the error:

Debugger attached.

MongoServerSelectionError: getaddrinfo ENOTFOUND mongo1.railway.internal

at async MongoClient.connect (/home/bruno/Desktop/private/typescript/monorepo-mern-railway-starter/node_modules/.pnpm/mongodb@6.6.0/node_modules/mongodb/lib/mongo_client.js:142:13) {

reason: TopologyDescription {

type: 'ReplicaSetNoPrimary',

servers: Map(3) {

'mongo1.railway.internal:27017' => [ServerDescription],

'mongo2.railway.internal:27017' => [ServerDescription],

'mongo3.railway.internal:27017' => [ServerDescription]

},

stale: false,

compatible: true,

heartbeatFrequencyMS: 10000,

localThresholdMS: 15,

setName: 'rs0',

maxElectionId: new ObjectId('7fffffff0000000000000001'),

maxSetVersion: 1,

commonWireVersion: 0,

logicalSessionTimeoutMinutes: null

},

code: undefined,

[Symbol(errorLabels)]: Set(0) {},

[cause]: MongoNetworkError: getaddrinfo ENOTFOUND mongo1.railway.internal

[cause]: Error: getaddrinfo ENOTFOUND mongo1.railway.internal

at GetAddrInfoReqWrap.onlookupall [as oncomplete] (node:dns:118:26)

at GetAddrInfoReqWrap.callbackTrampoline (node:internal/async_hooks:130:17) {

errno: -3008,

code: 'ENOTFOUND',

syscall: 'getaddrinfo',

hostname: 'mongo1.railway.internal'

}

}

}


7 months ago

Hey @brunotot! just cross-posting my response here too

A little explanation as to why this error occurs when trying to connect to the set from an app running on your local machine -

After the nodes are deployed from the template, the init service runs a command to join them to the set. This is the command it executes (ref):

rs.initiate({
  _id: "$REPLICA_SET_NAME",
  members: [
    { _id: 0, host: "$MONGO_PRIMARY_HOST:$MONGO_PORT" },
    { _id: 1, host: "$MONGO_REPLICA_HOST:$MONGO_PORT" },
    { _id: 2, host: "$MONGO_REPLICA2_HOST:$MONGO_PORT" }
  ]
})

The MONGO_---_HOST and MONGO_PORT variables are replaced with the values of the private network.

What I'm seeing is, even though all of the nodes are assigned a public domain and accessible via the internet directly, the internal communication between the nodes is still pinned to the private network, which is of course not accessible over the internet (e.g. applications running outside of the Railway project).

To solve the issue with connecting to the cluster from your local machine -

You will need to setup a tunnel into the private network. As luck would have it, Brody just published a tutorial that covers how to implement a Tailscale subnet router in your Railway project, which provides this exact functionality. I have been able to follow the tutorial and confirm that it works like a charm to enable connectivity into the private network from my local machine. The one hiccup I personally ran into, was in my local machine's Tailscale app, I had to enable Use Tailscale subnets within Tailscale's settings:

Please let me know if you run into any issues. We can also configure the replica set to communicate over the public network, but I would strongly advise against that due to egress fees.


yaneqPRO

4 months ago

i set up this template and can connect to it form the outside via directConnect=true without problems.

However, for our production server i'd want to use the replicaset. When i try to connect to the replicaset via the public net i get:

```
Failed to connect to the database MongoServerSelectionError: getaddrinfo ENOTFOUND mongo1.railway.internal
```

so i'm not sure if this i my machine trying to access the private net, or if its the mongo instances not being able to see each other via the private net.

My first thought was to try to set the new builder to true to see if that fixes the issue by giving the instances private net access, but somehow only mongo1 had the option in settings to activate the new builder. mongo2 and mongo3 did not have the option.

Also tried to create new docker instances of the railwayapp-templates/mongo-replica-set image, but those cannot be found on the "add docker" dialog, probably also private?

I'll try to tell the mongos now to use public net for inter cluster comms, see if that helps

Any help appreciated here.


alxcPRO

4 months ago

Hi there, are there plans to make this MongoDB template multi region?


alxc

Hi there, are there plans to make this MongoDB template multi region?

4 months ago

The MongoDB Replica Set template deploys multi-region by default. Read more about Multi-region Deployments, and the High Availability MongoDB Replica Set in general, in the Docs.


yaneq

i set up this template and can connect to it form the outside via directConnect=true without problems. However, for our production server i'd want to use the replicaset. When i try to connect to the replicaset via the public net i get:```Failed to connect to the database MongoServerSelectionError: getaddrinfo ENOTFOUND mongo1.railway.internal```so i'm not sure if this i my machine trying to access the private net, or if its the mongo instances not being able to see each other via the private net. My first thought was to try to set the new builder to true to see if that fixes the issue by giving the instances private net access, but somehow only mongo1 had the option in settings to activate the new builder. mongo2 and mongo3 did not have the option.Also tried to create new docker instances of the railwayapp-templates/mongo-replica-set image, but those cannot be found on the "add docker" dialog, probably also private?I'll try to tell the mongos now to use public net for inter cluster comms, see if that helpsAny help appreciated here.

yaneqPRO

3 months ago

as this is a blocker for us we'll be abandoning our plan to move our clusters to railway. maybe you wanna still review this at some point


yaneq

i set up this template and can connect to it form the outside via directConnect=true without problems. However, for our production server i'd want to use the replicaset. When i try to connect to the replicaset via the public net i get:```Failed to connect to the database MongoServerSelectionError: getaddrinfo ENOTFOUND mongo1.railway.internal```so i'm not sure if this i my machine trying to access the private net, or if its the mongo instances not being able to see each other via the private net. My first thought was to try to set the new builder to true to see if that fixes the issue by giving the instances private net access, but somehow only mongo1 had the option in settings to activate the new builder. mongo2 and mongo3 did not have the option.Also tried to create new docker instances of the railwayapp-templates/mongo-replica-set image, but those cannot be found on the "add docker" dialog, probably also private?I'll try to tell the mongos now to use public net for inter cluster comms, see if that helpsAny help appreciated here.

forsupporterPRO

3 months ago

Hello, how did you solve this problem ?


yaneq

as this is a blocker for us we'll be abandoning our plan to move our clusters to railway. maybe you wanna still review this at some point

forsupporterPRO

3 months ago

agree


forsupporter

Hello, how did you solve this problem ?

yaneqPRO

3 months ago

using directConnection=true from mongosh/compas works


Mongo Replica Set - Railway Help Station