1

node.js project using express, knex, dotenv

I created a copy of my project to be treated as a staging environment. It runs on the same host as my live project, but on a different port. There are no errors when I start the server.

However, when I navigate to it and it tries to get /, which pulls data from the db via knex, I get this error in the console:

Note: although my knexfile's host value is 'localhost', the error is showing my webserver's external IP address as the one it's trying to connect to (repalced with "mywebserverIP" in the snippet below")

{ Error: connect ECONNREFUSED mywebserverIP:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1082:14)
    --------------------
    at Protocol._enqueue (/usr/home/jessie/gs-staging/node_modules/mysql/lib/protocol/Protocol.js:144:48)
    at Protocol.handshake (/usr/home/jessie/gs-staging/node_modules/mysql/lib/protocol/Protocol.js:51:23)
    at Connection.connect (/usr/home/jessie/gs-staging/node_modules/mysql/lib/Connection.js:118:18)
    at /usr/home/jessie/gs-staging/node_modules/knex/lib/dialects/mysql/index.js:95:18
    at Promise._execute (/usr/home/jessie/gs-staging/node_modules/bluebird/js/release/debuggability.js:313:9)
    at Promise._resolveFromExecutor (/usr/home/jessie/gs-staging/node_modules/bluebird/js/release/promise.js:483:18)
    at new Promise (/usr/home/jessie/gs-staging/node_modules/bluebird/js/release/promise.js:79:10)
    at Client_MySQL.acquireRawConnection (/usr/home/jessie/gs-staging/node_modules/knex/lib/dialects/mysql/index.js:90:12)
    at create (/usr/home/jessie/gs-staging/node_modules/knex/lib/client.js:280:23)
    at tryPromise (/usr/home/jessie/gs-staging/node_modules/tarn/lib/Pool.js:366:22)
    at tryPromise (/usr/home/jessie/gs-staging/node_modules/tarn/lib/utils.js:57:20)
    at Promise (/usr/home/jessie/gs-staging/node_modules/tarn/lib/Pool.js:366:5)
    at new Promise (<anonymous>)
    at callbackOrPromise (/usr/home/jessie/gs-staging/node_modules/tarn/lib/Pool.js:357:10)
    at Pool._create (/usr/home/jessie/gs-staging/node_modules/tarn/lib/Pool.js:307:5)
    at Pool._doCreate (/usr/home/jessie/gs-staging/node_modules/tarn/lib/Pool.js:275:32)
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: 'mywebserverIP',
  port: 3306,
  fatal: true }

I have read multiple posts about what it means for the connection to be refused, and the only reason I am still baffled is that it's the only one of 3 projects on this server using this config that has the problem.

I have another node/express/knex project (an admin utility) which uses the same knex configuration to connect to the same database on the same mysql server with the same credentials... and it works without issues. The live site is up and running throughout, with this same configuration.

The live version, the staging copy, and the admin utility all have identical knexfiles (and matching .env files for the variables):

module.exports = {
    client: 'mysql',
    connection: {
        user: process.env.GS_DB_USER,
        password: process.env.GS_DB_PASS,
        host: process.env.GS_DB_HOST,
        database: 'gs'
    },
    pool: {
        min: 1,
        max: 1
        }
}

I can only imagine that the copy is conflicting with the live site in a way I don't know to look for. What am I missing?

JLP
  • 21
  • 6
  • is mysql listening on port 3306? Which interfaces (on server run `netstat -plnt | grep mysqld`)? Does using 'localhost' in the connection string work? – danblack Feb 19 '19 at 23:59
  • @danblack -Yes, it is listening on 3306 on localhost; localhost is the value of the GS_DB_HOST variable in all 3 .env files. – JLP Feb 20 '19 at 00:13
  • So why is your error `connect ECONNREFUSED mywebserverIP:3306` rather than ` connect ECONNREFUSED localhost:3306`? – danblack Feb 20 '19 at 00:53
  • I put in the word "mywebserverip" because it's actually showing the external IP of my webserver for some reason (didn't want to reveal it here). I have no idea why it's showing the external IP instead of localhost - perhaps it's a clue? – JLP Feb 20 '19 at 18:01

1 Answers1

1

Discovered that in the failing instance, the values in the .env file had been modified so that GS_DB_HOST was defined as the public hostname. Changed value back to localhost and it worked.

An exercise in double-checking all the basics first!

JLP
  • 21
  • 6