Connection to Mysql DB (with sequelize) fail
imulyssus
TRIALOP

2 years ago

original: Error: connect ECONNREFUSED ::1:3306

at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1555:16) {

errno: -111,

code: 'ECONNREFUSED',

syscall: 'connect',

address: '::1',

port: 3306,

fatal: true

}

}

Node.js v18.20.2

The above error kept showing. I used mysql_url var from the db creation on Railway platform itself and try to connect as below. Is there anything wrong? I am new here.

// config.js file

require('dotenv').config(); // Assuming config.js is in a subdirectory

module.exports = {

production: {

url: process.env.MYSQL_URL,

dialect: 'mysql',

},

};

// models/index.js file

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');

})

.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);

}

});

db.sequelize = sequelize;

db.Sequelize = Sequelize;

module.exports = db;

//index.js file (main file)

require('dotenv').config({ path: "./config.env" });

app.use(cors({

origin: 'http://localhost:3000',

methods: ["POST", "GET", "DELETE"],

credentials: true,

}));

app.use(bodyParser.json({ limit: "10mb" }));

app.use(bodyParser.urlencoded({ limit: "10mb", extended: true }));

app.use(cookieParser());

sequel.sequelize.sync().then(() => {

app.listen(process.env.PORT, () => {

console.log('Running at 3001');

});

});

View Deploy details

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

0 Replies

Welcome!

Sign in to your Railway account to join the conversation.

Loading...