Knex: Pool Error 2 - Error: Connect ECONNREFUSED

I work with Node.js, Express.js, Bookshelf.js and Knex.js

I get the following error with the default Knex pool definition.

Knex:Error Pool2 - Error: connect ECONNREFUSED 

That is, with the following definition of knex (the default value for the .min pool is 2)

 var knex = require('knex')({ client: 'mysql', connection: config.connection, }); 

I get an error

 Knex:Error Pool2 - Error: connect ECONNREFUSED Knex:Error Pool2 - Error: connect ECONNREFUSED 

Where it works great with the following definition.

 var knex = require('knex')({ client: 'mysql', connection: config.connection, pool: { min: 0, max: 10 } }); 

I noticed that the error prints n times, where n is in the value of pool.min.

Can someone tell me why this is happening. Although the error is resolved, but since I am a beginner, I cannot understand why this is happening.

+5
source share
2 answers

ECONNREFUSED is returned by the connect () system call when the remote system or service either does not listen on the specified port or the firewall between them does so.

Basically, knex cannot connect to the database server. The reason you are not getting an error with pool.min is 0 because the pool2 library is satisfied with zero active connections and will not try to connect to the database until the connection is needed on knex.


To keep track of what is happening, knex people do not look care to implement this . In addition, the problem is that the Pool2 object is destroyed when ECONNREFUSED is encountered at startup, and knex simply cannot restore it.

As the saying goes, the problem only arises for me at startup. If the database drops / after / the pool is installed, the kneks will continue to try to connect until it returns. Something in either knex or in the mysql client library handles the reconnect case just fine, even if ECONNREFUSED is returned. With that in mind, I did this:

 setTimeout(function () { knex.raw('SELECT 1').catch(function (error) { log.fatal('caught exception trying to issue database keep-alive'); // do something fatal, like exit or cluster.worker.disconnect() }); }, 0); 

I do this right after creating the knex object. Crash fast. This works for me, but YMMV.

+4
source

As diz says, you see this error when knex cannot connect to the database. When I received this error, it happened because I did not have a MySQL server running on my localhost (my "host" in the connection object), on which there was a database with the name specified in the database key of the connection object. I started MySQL and created an empty database, and everything was fine from there. Here are the steps I wrote for my fellow developers:

Required MySQL steps before this:

  • starting mysql.server

  • mysql -u root

  • CREATE USER 'our_user' @ 'localhost' IDENTIFIED BY 'our_password';

  • CREATE DATABASE dbname;

  • GRANT ALL ON dbname. * TO 'our_user' @ 'localhost';

+1
source

Source: https://habr.com/ru/post/1236409/


All Articles