I'm using Sequelize as an ORM to my node js app and Mysql database, after following some tutorials im adding this code to connect mysql to the node but after taping npm start im 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);
});
问题解决了我在 phpMyadmin 中使用端口:3307 而不是 3306
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
.
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.
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.
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'
}
});
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,
},
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.