2 years 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
DB_DATABASE: railway
DB_HOST: mysql.railway.internal
DB_PASSWORD: xxxxxxxx (same as database)
DB_PORT: 3306
DB_USERNAME: root
NODE_ENV: production
Sequelize config.js
module.exports = {
development: {
username: "root",
password: null,
database: "db_tennis",
host: "127.0.0.1",
dialect: "mysql",
},
production: {
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
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.NODE_ENV || '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_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_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
ⓘ Deployment information is only viewable by project members and Railway employees.
1 Replies
2 years 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