Error deploying node, express, mysql, sequelize to railway

snowy5314HOBBY

8 months ago

Trying to deploy node express sequelize to railway
When i run npm run start all ok & can see index page in browser
When run npx sequelize-cli db:migrate && npx sequelize-cli db:seed:all && npm run start i get error on deployment
ERROR: getaddrinfo ENOTFOUND
mysql.railway.internal

database variables
MYSQLDATABASE: railway
MYSQLHOST: mysql.railway.internal
MYSQLPASSWORD: xxxxxxxxxxx
MYSQLPORT: 3306
MYSQLUSER: root

SERVER variables
DBDATABASE: railway DBHOST: mysql.railway.internal
DBPASSWORD: xxxxxxxx (same as database) DBPORT: 3306
DBUSERNAME: root NODEENV: production

Sequelize config.js

module.exports = {
development: {
username: "root",
password: null,
database: "dbtennis", host: "127.0.0.1", dialect: "mysql", }, production: { username: process.env.DBUSERNAME,
password: process.env.DBPASSWORD, database: process.env.DBDATABASE,
host: process.env.DB_HOST,
dialect: "mysql",
},
};

index.js

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const process = require('process');
const basename = path.basename(filename); const env = process.env.NODEENV || 'development'; // const config = require(dirname + '/../config/config.json')[env];
const config = require("../config/config")[env];
const db = {};
console.log(config);
let sequelize;
if (config.use
envvariable) { sequelize = new Sequelize(process.env[config.useenv_variable], config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}
fs
.readdirSync(dirname) .filter(file => { return ( file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js' && file.indexOf('.test.js') === -1 ); }) .forEach(file => { const model = require(path.join(dirname, file))(sequelize, Sequelize.DataTypes);
db[model.name] = model;
});
Object.keys(db).forEach(modelName => {

if (db[modelName].associate) {
db[modelName].associate(db);
}
});
require('../utils/associations')(db)
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;

Server.js

const PORT = process.env.DB_PORT || 3500;

app.listen(PORT,() => {
console.log(server is running on port ${PORT});
});

All migrations & seeds work fine on localhost.
Totally inexperienced in deploying but i guess its not finding database to carry out migrations etc.
Thank you for any help

View Deploy details

ⓘ Deployment information is only viewable by project members and Railway employees.

1 Replies

8 months ago

Your DB_HOST variable has a leading newline, to avoid such mistakes please use reference variables -

https://docs.railway.app/guides/variables#referencing-another-services-variable


Error deploying node, express, mysql, sequelize to railway - Railway Help Station