Recently used Node to write a gadget, you need to use the MySQL database, the most widely used is the mysql library. And then, now ORMso fire, simply have the ORMright, I will not just be learning about, so to find Sequelize.js the ORMlibrary.

 

Take a look at Sequelizethe document, so easy, two minutes to get ~

import Sequelize from 'sequelize';
let sequelize = new Sequelize('database', 'username', 'password', {
    host: 'localhost',
    port: 3306,
    dialect: 'mysql',
    pool: {
        max: 5,
        min: 0,
        idle: 10000
    }
});
// ...后面还有一堆懒得贴了

Run it

SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306

What the hell, why is this error? I clearly set up localhost, why did it become 127.0.0.1?

 

As usual, Google has indeed found that many people have encountered this problem. There are probably several solutions:

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

2. What is your database running? Hurry /etc/init.d/mysqld startup and running

3, the port is wrong

4, you are not turned on skip-networkingthis option? Remove it !

 

See here, I could react, because I do not relate to the remote database access, as long as the use of Unix socketcommunication is enough, so he is enabled skip-networkingto make MySQL not listening on the specified port.

 

First science about skip-networkingwhat 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.

The translation is:

Do not listen for TCP/IPconnections. All mysqldinteractions must use named pipes or shared memory (on Windows) or Unix socketfile (on Unix). It is highly recommended to use this option for systems that only allow local clients.

source

 

However, for safety, I do not want to remove this choice, we are reluctantly forced to do ORMyet?

Because read documents, mysqlthe library is connected you can use socketPaththis property to specify Unix 套接字文件, but Sequelize.jsdid not find the relevant property.

Finally had hair issue, and soon there dalao said that I can use  dialectOptionsto set mysqlproperty. Here is the code for the test to succeed:

import Sequelize from 'sequelize';
let sequelize = new Sequelize('database', 'username', 'password', {
    host: 'localhost',
    port: 3306,
    dialect: 'mysql',
    dialectOptions: {
        socketPath: '/tmp/mysql.sock' // 指定套接字文件路径
    }
    pool: {
        max: 5,
        min: 0,
        idle: 10000
    }
});

 

Mom's, it's that simple...


Original article, reproduced please indicate the source in the form of a link: https://blog.ttionya.com/article-1709.html