502 Application Failed to respond - days spent trying to fix
selucus
FREEOP

4 months ago

My mysql db was working just fine but all of a sudden I get 502 errors constantly. When I build it on the railway site and click on the public link I get the classic:

I have followed all troubleshooting steps linked

Logs show live on port 3306:

HTTP logs showing 502 failures

Public configured to port 3306

$10 Bounty

11 Replies

Railway
BOT

4 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
FREE

4 months ago

Have you checked whether the volume mounted to the database is full or not?


selucus
FREEOP

4 months ago

Hi, the volume is nowhere near full:


clashing
FREE

4 months ago

Okay.
Can you provide the server file's snippet?


selucus
FREEOP

4 months ago

I am getting errors when just clicking this link in railway:However, to connect I am using prisma:

declare global {
  // eslint-disable-next-line no-var, no-unused-vars
  var cachedPrisma: PrismaClient
}

let prisma: PrismaClient
if (process.env.NODE_ENV === 'production') {
  prisma = new PrismaClient()
} else {
  if (!global.cachedPrisma) {
    global.cachedPrisma = new PrismaClient()
  }
  prisma = global.cachedPrisma
}

export const db = prisma

Attachments


clashing
FREE

4 months ago

selucus, I got it. The thing is that Railway provides users with the UI itself in the service's dashboard to access the data (I am sure you can access that).

But the public-networking domain that you have generated is not to be viewed in a browser! If you go to the Pubic Networking settings, you can see this:

It means that Railway allows you to make your service (such as a MySQL database) accessible from outside their internal network through a secure, managed TCP connection. One use case for it could be: Using MySQL Workbench to directly manage your Railway-hosted database from your laptop.

It would give a 502 error because it is not meant to be accessed that way. So, in short, as this is a DB instance, you cannot see any UI when requesting the TCP domain via browser; it's for TCP purposes, to let you connect to the DB from external applications/platforms! https://docs.railway.com/reference/databases#tcp-proxy

I hope that clarifies your issue.

Attachments


selucus
FREEOP

4 months ago

Okay, but I still cannot connect from my application any more: I also cannot see the data in the online UI anymore, it just hangs on connecting:


clashing
FREE

4 months ago

I guess you have somehow affected your MYSQL instance, and that is why it can't get the PUBLIC endpoint via which the DATA tab populates. As you are on the trial plan, I request you to delete that instance and its related volume and then redeploy a fresh instance of MySQL.

At my end, I have used a simple Express server to connect to the MySQL DB, and it worked perfectly. I am not sure why specifically you are using Prisma to do this. I used the mysql2 npm package to do the same. Here is my server file showing the ways to connect to a PostgreSQL, MySQL, or a REDIS DB instance.

require("dotenv").config();
const { createClient } = require("redis");
const mysql = require("mysql2/promise");
const express = require("express");
const { Pool } = require("pg");

const app = express();
const port = process.env.PORT || 5600;

app.get("/", async (req, res) => {
  res.status(200).json({
    env: process.env,
  });
});

// Check for PostgreSQL env var
let pool = null;
if (process.env.PG_URL) {
  pool = new Pool({
    connectionString: process.env.PG_URL,
  });
} else {
  console.warn(
    "Warning: DATABASE_URL is not set. PostgreSQL endpoint will be disabled."
  );
}

// PostgreSQL endpoint
app.get("/pg", async (req, res) => {
  if (!pool) {
    return res
      .status(500)
      .json({ error: "PostgreSQL connection not configured" });
  }
  try {
    const result = await pool.query("SELECT NOW()");
    res.json({ status: "Postgres connected", time: result.rows[0].now });
  } catch (error) {
    console.error("PG error:", error);
    res.status(500).json({ status: "PG Error", message: error.message });
  }
});

// Check for Redis env var and initialize client
let redisClient = null;
if (process.env.REDIS_URL) {
  redisClient = createClient({
    url: process.env.REDIS_URL,
  });
  redisClient.on("error", (err) => console.error("Redis Client Error", err));
  (async () => {
    await redisClient.connect();
  })();
} else {
  console.warn(
    "Warning: REDIS_URL is not set. Redis endpoint will be disabled."
  );
}

// Redis endpoint
app.get("/redis", async (req, res) => {
  if (!redisClient) {
    return res.status(500).json({ error: "Redis connection not configured" });
  }
  try {
    await redisClient.set("ping", "PONG");
    const value = await redisClient.get("ping");
    res.json({ status: "Redis connected", message: value });
  } catch (error) {
    console.error("Redis error:", error);
    res.status(500).json({ status: "Redis Error", message: error.message });
  }
});

// MySQL setup
let mysqlPool = null;
if (process.env.MYSQL_URL) {
  mysqlPool = mysql.createPool({ uri: process.env.MYSQL_URL });
} else {
  console.warn(
    "Warning: MYSQL_URL is not set. MySQL endpoint will be disabled."
  );
}

// MySQL endpoint
app.get("/mysql", async (req, res) => {
  if (!mysqlPool) {
    return res.status(500).json({ error: "MySQL connection not configured" });
  }
  try {
    const [rows] = await mysqlPool.query("SELECT NOW() AS now");
    res.json({ status: "MySQL connected", time: rows[0].now });
  } catch (error) {
    console.error("MySQL error:", error);
    res.status(500).json({ status: "MySQL Error", message: error.message });
  }
});

app.listen(port, () => {
  console.log(`Server running on port ${port}`);
});

It worked perfectly for me, because the connection process for the same is straightforward.

I hope that it would help you


selucus
FREEOP

4 months ago

this also does not work for me


fra
HOBBY

4 months ago

did you try to restart the db?


selucus

this also does not work for me

clashing
FREE

4 months ago

Just delete the previously existing DB (because anyhow you are saying that you are unable to access it), and check whether the DATA tab is being formulated or not, for the new one?


Loading...