6

I'm using Sequelize as an ORM to my node js app and Mysql database , after following some tutorials i m adding this code to connect mysql to the node but after taping npm start i m getting this error : Unable to connect to the database: { SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306

 const Sequelize = require('sequelize');

 // Option 1: Passing parameters separately
 const sequelize = new Sequelize('Education', 'root','', {
 host: '127.0.0.1',
 dialect: 'mysql'
 } );

  //test db 
 sequelize
.authenticate()
.then(() => {
  console.log('Connection has been established successfully.');
})
 .catch(err => {
 console.error('Unable to connect to the database:', err);
});
Tien Duong
  • 2,517
  • 1
  • 10
  • 27
Anas Zayene
  • 314
  • 1
  • 4
  • 13

7 Answers7

5

Problem solved i was using port : 3307 in phpMyadmin instead of 3306

Anas Zayene
  • 314
  • 1
  • 4
  • 13
4

Add socketPath in the dialect options

Example code snippet:

// Option 1: Passing parameters separately
const sequelize = new Sequelize('Education', 'root', '', {
  host: '127.0.0.1',
  dialect: 'mysql',
  dialectOptions: {
    socketPath: '/Applications/MAMP/tmp/mysql/mysql.sock'
  }
});
theusaf
  • 1,781
  • 1
  • 9
  • 19
4

FIX for Node.js use:

It looks as though the current version of sequelize (7) only works on Node.js versions ^12.22.0, ^14.17.0 & ^16.0.0. www.sequelize.org says that some other versions may work as well.

Use NVM to install and switch to version 16.0.0:

If you don't already have node version manager you can install it with

$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

Then install version 16 with nvm i 16.0.0 and then tell your application to use it with nvm use 16.0.0.

1

Just restart the mysql server.

Linux and Mac:

service mysql restart

Windows:

Open the Run window by using the Windows+R keyboard. Second, type services.msc and press Enter: Find mysql and restart the service.

Amit Chhatbar
  • 93
  • 1
  • 9
0

If you're running XAMMP server on windows, first start apache server with MySQL

You get this error probably because your server is not running.

check your server and restart if necessary.

Jamal S
  • 1,649
  • 1
  • 19
  • 24
Aman Kumar
  • 51
  • 1
0

the solution is just only Start Your Xampp Apache and MySQL Service.enter image description here

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/33452505) – Insane Skull Dec 24 '22 at 09:05
-1

I have removed port option and issue is resolved.

const sequelize = new Sequelize('dbname', 'dbuser', 'pass', {
    dialect: 'mysql',
    host: "127.0.0.1",
    operatorAlias:false,
    logging:false,
    pool: {
        max: 5,
        idle: 30000,
        acquire: 60000,
    },
    
})
  • Removing the port option could work only in your set-up. As the author mentioned afterwards, on their set-up their port was different than the default, and thus was the connection error. – Evan P Aug 08 '21 at 15:43