Solve the Problem of connect ECONNREFUSED 127.0.0.1: 3306 in Node. js using MySQL, connecteconnrefused
Preface
Recently, I used Node to write a gadget that requires MySQL databases. Currently, the most widely used mysql database is used. Then, now the ORM is so popular, so I can't learn it, so I found the ORM library sequencee. js.
Problems Found
Let's take a look at sequencee's documentation. It's so easy. It's done in two minutes ~
Import sequencee from 'sequencee'; let sequencee = new sequencee ('database', 'username', 'Password', {host: 'localhost', port: 3306, dialect: 'mysql', pool: {max: 5, min: 0, idle: 10000 }});//... there is a bunch of questions behind it.
Run
SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306
What, why is this error? I set localhost clearly. Why is it 127.0.0.1?
Solve the problem
Google, as an example, has discovered that many people have encountered this problem. There are several solutions:
1. Do you think you can run it without installing MySQL? Install the database!
2. Is your database running? Run/etc/init. d/mysqld start.
3. Port Error
4. Have you enabled the skip-networking option? Remove it!
Here, I reflected that, because my database does not involve remote access, as long as Unix socket communication is enough, so skip-networking is enabled so that MySQL does not listen to the specified port.
What is skip-networking?
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.
Translation:
Do not listen to TCP/IP connections. All interactions with mysqld must use named pipes or shared memory (on Windows) or Unix socket files (on Unix ). We strongly recommend that you use this option for systems that only allow local clients.
Source
But for the sake of security, I don't want to remove this option. Do I have to endure the headache of Using ORM?
After reading the documentation, mysql Connection Library can use the socketPath attribute to specify Unix socket files, but sequencee. js does not find any relevant properties.
Finally, I had to issue an issue. Soon dalao replied that I could use dialectOptions to set mysql attributes.
The code for testing is as follows:
Import sequencee from 'sequencee'; let sequencee = new sequencee ('database', 'username', 'Password', {host: 'localhost', port: 3306, dialect: 'mysql', dialectOptions: {socketPath: '/tmp/mysql. sock '// specifies the socket file path} pool: {max: 5, min: 0, idle: 10000 }});
That's simple...
Summary
The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.