MongoDB Service Connected but App Cannot Establish Database Connection - Mongoose State 0 (Disconnected)

arvora-group
HOBBY

a month ago

**Issue Summary:**
My Node.js app deploys successfully on Railway and passes health checks, but cannot establish a connection to the MongoDB service. The app shows `mongooseState: 0` (disconnected) despite having correct environment variables and connection strings.

**Current Status:**
- ✅ App deployment: SUCCESS (health endpoint returns 200)
- ✅ MongoDB service: RUNNING (shows as active in dashboard)
- ✅ Environment variables: CONFIGURED (DATABASE_URL, MONGODB_URI set)
- ❌ Database connection: FAILED (mongoose connection state = 0)

**Environment Details:**
- **Platform:** Railway
- **App Type:** Node.js 18 (Express + Mongoose)
- **Database:** Railway MongoDB Plugin

**Connection Configuration:**
```javascript
// Connection string format:
mongodb://***:***@mongodb.railway.internal:27017/[database_name]

// Environment variables available:
DATABASE_URL: ✅ Set
MONGODB_URI: ✅ Set  
RAILWAY_ENVIRONMENT: ✅ Set
// Health check response:
{
  "database": "disconnected",
  "mongooseState": 0,
  "mongooseStateText": "disconnected", 
  "currentDatabase": "none"
}
const mongoURI = process.env.DATABASE_URL || process.env.MONGO_URL || process.env.MONGODB_URI;
await mongoose.connect(mongoURI, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
  maxPoolSize: 10,
  serverSelectionTimeoutMS: 5000,
  socketTimeoutMS: 45000,
  bufferCommands: false
});

What I've Tried:

  1. Verified MongoDB service is running in Railway dashboard

  2. Confirmed environment variables are properly set

  3. Tested connection string format (appears correct)

  4. Implemented connection retry logic

  5. Added comprehensive error handling and logging

Questions:

  1. Could this be a Railway internal networking issue between services?

  2. Are there specific MongoDB service configuration requirements I'm missing?

  3. Should I be using a different connection approach for Railway's MongoDB plugin?

  4. Any known issues with mongodb.railway.internal hostname resolution?

Repository: Can provide minimal reproduction code if needed for debugging.

Any guidance on troubleshooting Railway MongoDB connectivity would be greatly appreciated!

Solved

8 Replies

Railway
BOT

a month 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!


Can you give me a timestamp of when you see this occuring?


Status changed to Awaiting User Response Railway 30 days ago


angelo-railway

Can you give me a timestamp of when you see this occuring?

arvora-group
HOBBY

a month ago

Are you able to be specific? timestamp? this is from the deploy-log. If I am being honestly, I am so overwhelmed.

client metadata

Connection accepted

client metadata

Supported SASL mechanisms requested for unknown user

Failed to authenticate

Auth metrics report

Failed to authenticate

Connection ended

Connection ended

Connection accepted

client metadata

Connection accepted

client metadata

Supported SASL mechanisms requested for unknown user

Auth metrics report

Failed to authenticate

Auth metrics report

Failed to authenticate

Connection ended

Connection ended

WiredTiger message

Connection accepted

client metadata

Connection accepted

client metadata

Supported SASL mechanisms requested for unknown user

Auth metrics report

Failed to authenticate

Auth metrics report

Failed to authenticate

Connection ended

Connection ended

Connection accepted

client metadata

Connection accepted

client metadata

Supported SASL mechanisms requested for unknown user

Auth metrics report

Failed to authenticate

Auth metrics report

Failed to authenticate

Connection ended

Connection ended

WiredTiger message

Connection accepted

client metadata

Connection accepted

client metadata

Supported SASL mechanisms requested for unknown user

Auth metrics report

Failed to authenticate

Auth metrics report

Failed to authenticate

Connection ended

Connection ended

Connection accepted

client metadata

Connection accepted

client metadata

Supported SASL mechanisms requested for unknown user

Auth metrics report

Failed to authenticate

Auth metrics report

Failed to authenticate


Status changed to Awaiting Railway Response Railway 30 days ago


I can understand how stressful it, however, these are logs, not a time. I need a time that you observed the issue.


Status changed to Awaiting User Response Railway 29 days ago


angelo-railway

I can understand how stressful it, however, these are logs, not a time. I need a time that you observed the issue.

arvora-group
HOBBY

a month ago

Hi okay would it be this?

https://networksync-app-production-4aec.up.railway.app/force-database-connection
https://networksync-app-production-4aec.up.railway.app/debug-database-connection

**Current Issue Timestamps:**

1. **Latest Health Check:** 2025-08-15T18:19:43.953Z (UTC)
   - Status: Database disconnected
   - App uptime: 39.5 hours (running since ~2025-08-14T02:46:00Z)

2. **Latest Debug Check:** 2025-08-15T18:19:55.949Z (UTC)
   - Mongoose State: 0 (disconnected)
   - Environment variables: MONGODB_URI=true, DATABASE_URL=true
   - Railway environment detected: true

3. **Issue Duration:** 
   - App has been running for 39+ hours with persistent database disconnection
   - Issue appears to be ongoing since deployment

**Additional Context:**
- App service is healthy and responding to HTTP requests
- MongoDB service shows as "running" in Railway dashboard
- Connection attempts are failing at the mongoose.connect() level
- No successful database connections logged since deployment

Status changed to Awaiting Railway Response Railway 28 days ago


No, and this is just an AI summary. I need a specific time when you observed this, a 39 hour window is not helpful.


Status changed to Awaiting User Response Railway 27 days ago


angelo-railway

No, and this is just an AI summary. I need a specific time when you observed this, a 39 hour window is not helpful.

arvora-group
HOBBY

a month ago

It was 3 days ago, I can't specify the time.(3AM - 4AM) LISBON - I am going to try again.


Status changed to Awaiting Railway Response Railway 27 days ago


All good, here if you run into it again.


Status changed to Awaiting User Response Railway 27 days ago


Status changed to Solved angelo-railway 27 days ago


MongoDB Service Connected but App Cannot Establish Database Connection - Mongoose State 0 (Disconnected) - Railway Help Station