SoFunction
Updated on 2025-03-09

Solve the problem of connecting ECONNREFUSED 127.0.0.1:3306 when using MySQL

Preface

Recently, I used Node to write a gadget, which requires the MySQL database. The most widely used library is the mysql library. Then, now that ORM is so popular, I might as well go to ORM. I just can't learn it, so I found this ORM library.

Discover problems

Check out Sequelize's documentation, so easy, it will be done in two minutes~

import Sequelize from 'sequelize';
let sequelize = new Sequelize('database', 'username', 'password', {
 host: 'localhost',
 port: 3306,
 dialect: 'mysql',
 pool: {
  max: 5,
  min: 0,
  idle: 10000
 }
});
// ...There are still a bunch of people behind it that are too lazy to post

Run it

SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306

What the hell, why does this error occur? I clearly set localhost, why does it become 127.0.0.1?

Solve the problem

As usual, Google first, and I did find that many people have encountered this problem. There are probably several solutions:

1. Do you think you can run away without installing MySQL? Go and install the database!

2. Is your database running? Hurry /etc//mysqld start run

3. The port is written incorrectly

4. Have you turned on the skip-networking option? Remove it!

Seeing this, I realized that my database does not involve remote access, just using Unix socket communication is enough, so skip-networking is enabled to prevent MySQL from listening to the specified port.

Let’s first introduce what skip-networking is

Do not listen for TCP/IP connections at all. All interaction with mysqld must be made using named pipes or shared memory (on Windows) or Unix socket files (on Unix). This option is highly recommended for systems where only local clients are permitted.

Translate it to:

Do not listen for TCP/IP connections. All interactions with mysqld must use named pipes or shared memory (on Windows) or Unix socket files (on Unix). This option is strongly recommended for systems that only local clients are allowed.

source

But for the sake of security, I don’t want to remove this option, so do I have to bear the pain and stop using ORM?

Because I read the documentation, the connection library of mysql can use the socketPath attribute to specify Unix socket files, but no relevant attributes were found.

Finally, I had to post an issue, and soon dalao replied that I could use dialectOptions to set the properties of mysql.

Here is the code for the successful test:

import Sequelize from 'sequelize';
let sequelize = new Sequelize('database', 'username', 'password', {
 host: 'localhost',
 port: 3306,
 dialect: 'mysql',
 dialectOptions: {
  socketPath: '/tmp/' // Specify the socket file path }
 pool: {
  max: 5,
  min: 0,
  idle: 10000
 }
});

It's that simple...

Summarize

The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.