Connection Reset while connecting to MongoDB instance from Librechat
shubhamd
PROOP

7 months ago

Getting connection reset error. Using MongoDB's MONGO_URL variable. Have public TCP domain active. This is the URL I'm trying (password redacted in img, added the template formatting for reference, only using the top line in Mongo compass)

URL entered in Mongo Compass (password hidden)
mongodb://mongo:{password}@switchback.proxy.rlwy.net:26136/

MONGO_URL format -
mongodb://$(MONGO_INITDB_ROOT_USERNAME:MONGO_INITDB_ROOT_PASSWORD@RAILWAY_TCP_PROXY_DOMAIN}:${RAILWAY_TCP_PROXY_PORT}}

$10 Bounty

6 Replies

Railway
BOT

7 months ago

Hey there! We've found the following might help you get unblocked faster:

If you find the answer from one of these, please let us know by solving the thread!


clashing
HOBBY

7 months ago

Afaik, you won't need a TCP endpoint to connect a railway-instace with the railway-deployed DB

TCP connection is not what we use in the connection string while connecting via code (libraries/packages).

You can see in my above snippets that I get the correct response from the DB when I am using the correct MONDO DB connection strings, as compared to when I use the TCP endpoint in the connection string!

The correct connection string format for MongoDB is this:

mongodb://${{MONGOUSER}}:${{MONGO_INITDB_ROOT_PASSWORD}}@${{MONGOHOST}}:${{MONGOPORT}}

Where the mongoHost needs to be either the public or private/internal endpoint of the Railway deployed instance of the DB. I am using the below code to get the correct output:

// MongoDB setup
let mongoClient = null;
let mongoDb = null;
if (process.env.MONGO_URL) {
  mongoClient = new MongoClient(process.env.MONGO_URL, {
    useUnifiedTopology: true,
  });
  (async () => {
    try {
      await mongoClient.connect();
      // Replace with your database name, or get from connection string
      mongoDb = mongoClient.db(process.env.MONGO_DBNAME || "test");
      console.log("Connected to MongoDB");
    } catch (err) {
      console.error("MongoDB connection error:", err);
      mongoDb = null;
    }
  })();
} else {
  console.warn(
    "Warning: MONGO_URL is not set. MongoDB endpoint will be disabled."
  );
}

// MongoDB endpoint
app.get("/mongo", async (req, res) => {
  if (!mongoDb) {
    return res.status(500).json({ error: "MongoDB connection not configured" });
  }
  try {
    // Example: List collections in the database
    const collections = await mongoDb.listCollections().toArray();
    res.json({ status: "MongoDB connected", collections });
  } catch (error) {
    console.error("MongoDB error:", error);
    res.status(500).json({ status: "MongoDB Error", message: error.message });
  }
});

In there you can see, I have just used the variable MONGO_URL, which you can set via the variables section of the deployment. Make it point to either of these reference variables:

But as you must already know that using the private/internal URL is best to let your other railway deployments connect with the DB instance, as it saves egress costs.

Furthermore, remove the TCP endpoint for the DB instance, as you won't need it in your case, and just use the ${{MongoDB.MONGO_URL}} to connect to the database.

I hope that helps you


shubhamd
PROOP

7 months ago

Hi clashing, it only worked when I hosted mongo express server alongside other services. Thanks for the suggestions. I wanted to connect my local mongo compass with it, but your suggestions of using public URL didn't work, and the private URLs would only be accessible from railway network.


shubhamd

Hi clashing, it only worked when I hosted mongo express server alongside other services. Thanks for the suggestions. I wanted to connect my local mongo compass with it, but your suggestions of using public URL didn't work, and the private URLs would only be accessible from railway network.

clashing
HOBBY

7 months ago

Hi shubhamd, the public & private endpoints are only there for them to be accessible via code. Private URL/endpoint only lets you connect the MongoDB instance deployed on Railway to another service deployed in the same Railway environment. Whereas the PUBLIC one is to be used via libraries/code packages via the server files (for instance, via the mongo package in Node.js).

Assume you are coding a node server for your application, then you can use the public connection string to debug on your local machine, & then once you are done with the coding part, and ready to publish the server to Railway, switch to the private endpoint, so that the connection is fast, & saves you egress costs v emoji

But as you mentioned now, that you are trying to connect Mongo Compass ( running locally) to your Railway-deployed MongoDB instance, I installed the same on my machine, and was successful in connecting to the Railway instance (using the TCP thing) tada emoji

This is the connection string that I used to do the same:

mongodb://${{MONGOUSER}}:${{MONGO_INITDB_ROOT_PASSWORD}}@${{TCP_RPOXY:PORT}}

For my demo case, it was this one:

mongodb://mongo:JDQIxTrMfmUmWZFxYVlsvWStMRTtm@hopper.proxy.rlwy.net:39049/

This is what my Networking settings look like:

Note: You do not have to generate the PUBLIC NETWORKING domain for this; just use the pre-defined TCP connection string that is provided. It won't work for the public URL.

You are saying that you are unable to connect even after using the right connection string! Can you provide the snippet of mongoDB instance deployed on Railway, along with its networking settings

I am sure that would help you. I have removed the DB instance now (so no harm to provide a wrong pwd in this public domain) wink emoji


clashing
HOBBY

7 months ago

Any update, shubhamd


clashing

Hi shubhamd, the public & private endpoints are only there for them to be accessible via code. Private URL/endpoint only lets you connect the MongoDB instance deployed on Railway to another service deployed in the same Railway environment. Whereas the PUBLIC one is to be used via libraries/code packages via the server files (for instance, via the mongo package in Node.js).Assume you are coding a node server for your application, then you can use the public connection string to debug on your local machine, & then once you are done with the coding part, and ready to publish the server to Railway, switch to the private endpoint, so that the connection is fast, & saves you egress costsBut as you mentioned now, that you are trying to connect Mongo Compass ( running locally) to your Railway-deployed MongoDB instance, I installed the same on my machine, and was successful in connecting to the Railway instance (using the TCP thing)This is the connection string that I used to do the same:mongodb://${{MONGOUSER}}:${{MONGO_INITDB_ROOT_PASSWORD}}@${{TCP_RPOXY:PORT}}For my demo case, it was this one:mongodb://mongo:JDQIxTrMfmUmWZFxYVlsvWStMRTtm@hopper.proxy.rlwy.net:39049/This is what my Networking settings look like:Note: You do not have to generate the PUBLIC NETWORKING domain for this; just use the pre-defined TCP connection string that is provided. It won't work for the public URL.You are saying that you are unable to connect even after using the right connection string! Can you provide the snippet of mongoDB instance deployed on Railway, along with its networking settingsI am sure that would help you. I have removed the DB instance now (so no harm to provide a wrong pwd in this public domain)

clashing
HOBBY

7 months ago

shubhamd, any update! Is the issue solved. If yes, then how you did that


Loading...