Connecting to MySQL from NodeJS (Express App) throws error

2019-06-10T17:32:27

edit: Solved my problem. After removing the line

connection.connect(function(err) {
    if (err) throw err;
});

I got the raw MySQL error message saying that the login credentials for MySQL are wrong. I already checked my passwort, but din't noticed the wrong username. Very sorry fr my dumb question.


I have a NodeJS / Express app. It was already working fine but know I dont'get a connection any more to MySQL (MariaDB). Always I try to start the app (with pm2) the start failed, because a exception is thrown.

I have already checked if MySQL is running and I also restarted MySQL and NodeJS app and pdated node-mysql. This is my code for connecting to mysql:


//local mysql db connection
var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : 'xxx',
    database : 'lizenzverwaltung'
});

connection.connect(function(err) {
    if (err) throw err;
});

module.exports = connection;

The error message on startup is:

0|www      |     at Socket.<anonymous> (/var/www/html/lizenzverwaltung/node_modules/mysql/lib/Connection.js:525:10)
0|www      |     at Socket.emit (events.js:182:13)
0|www      |     at Socket.EventEmitter.emit (domain.js:442:20)
0|www      |     --------------------
0|www      |     at Protocol._enqueue (/var/www/html/lizenzverwaltung/node_modules/mysql/lib/protocol/Protocol.js:144:48)
0|www      |     at Protocol.handshake (/var/www/html/lizenzverwaltung/node_modules/mysql/lib/protocol/Protocol.js:51:23)
0|www      |     at Connection.connect (/var/www/html/lizenzverwaltung/node_modules/mysql/lib/Connection.js:119:18)
0|www      |     at Object.<anonymous> (/var/www/html/lizenzverwaltung/models/db.js:11:12)
0|www      |     at Module._compile (internal/modules/cjs/loader.js:702:30)
0|www      |     at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
0|www      |     at Module.load (internal/modules/cjs/loader.js:612:32)
0|www      |     at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
0|www      |     at Function.Module._load (internal/modules/cjs/loader.js:543:3)
0|www      |     at Module.require (internal/modules/cjs/loader.js:650:17)

If I comment out the line if (err) throw err; I can start the NodeJS app, but get the following error, as soon, as I try to run a SQL query:

0|www      |     at Protocol._validateEnqueue (/var/www/html/lizenzverwaltung/node_modules/mysql/lib/protocol/Protocol.js:212:16)
0|www      |     at Protocol._enqueue (/var/www/html/lizenzverwaltung/node_modules/mysql/lib/protocol/Protocol.js:138:13)
0|www      |     at Connection.query (/var/www/html/lizenzverwaltung/node_modules/mysql/lib/Connection.js:201:25)
0|www      |     at Function.verifyLicense [as verify] (/var/www/html/lizenzverwaltung/models/licenseModel.js:29:9)
0|www      |     at Function.LicenseController.veryLicense (/var/www/html/lizenzverwaltung/controllers/licenseController.js:51:18)
0|www      |     at /var/www/html/lizenzverwaltung/routes/license.js:33:21
0|www      |     at Layer.handle [as handle_request] (/var/www/html/lizenzverwaltung/node_modules/express/lib/router/layer.js:95:5)
0|www      |     at next (/var/www/html/lizenzverwaltung/node_modules/express/lib/router/route.js:137:13)
0|www      |     at Route.dispatch (/var/www/html/lizenzverwaltung/node_modules/express/lib/router/route.js:112:3)
0|www      |     at Layer.handle [as handle_request] (/var/www/html/lizenzverwaltung/node_modules/express/lib/router/layer.js:95:5) code: 'PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR', fatal: false }
0|www      | error { Error: Cannot enqueue Query after fatal error.
0|www      |     at Protocol._validateEnqueue (/var/www/html/lizenzverwaltung/node_modules/mysql/lib/protocol/Protocol.js:212:16)
0|www      |     at Protocol._enqueue (/var/www/html/lizenzverwaltung/node_modules/mysql/lib/protocol/Protocol.js:138:13)
0|www      |     at Connection.query (/var/www/html/lizenzverwaltung/node_modules/mysql/lib/Connection.js:201:25)
0|www      |     at Function.verifyLicense [as verify] (/var/www/html/lizenzverwaltung/models/licenseModel.js:29:9)
0|www      |     at Function.LicenseController.veryLicense (/var/www/html/lizenzverwaltung/controllers/licenseController.js:51:18)
0|www      |     at /var/www/html/lizenzverwaltung/routes/license.js:33:21
0|www      |     at Layer.handle [as handle_request] (/var/www/html/lizenzverwaltung/node_modules/express/lib/router/layer.js:95:5)
0|www      |     at next (/var/www/html/lizenzverwaltung/node_modules/express/lib/router/route.js:137:13)
0|www      |     at Route.dispatch (/var/www/html/lizenzverwaltung/node_modules/express/lib/router/route.js:112:3)
0|www      |     at Layer.handle [as handle_request] (/var/www/html/lizenzverwaltung/node_modules/express/lib/router/layer.js:95:5) code: 'PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR', fatal: false } result null
0|www      | POST /v1/license/verify/ 200 19.128 ms - 16

I already searched but didn't found a solution. Do you have any tips? My MySQL server should working fine, PHPMyAdmin and my Wordpress sites are running fine.

Thanks, Klaus

Copyright License:
Author:「Klaus Schreiber」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/56523769/connecting-to-mysql-from-nodejs-express-app-throws-error

About “Connecting to MySQL from NodeJS (Express App) throws error” questions

edit: Solved my problem. After removing the line connection.connect(function(err) { if (err) throw err; }); I got the raw MySQL error message saying that the login credentials for MySQL are w...
I have started node-js recently and i was trying to connect my nodejs server with mysql. The problem is i am getting an error, i really don't know why, i am using phpmyadmin. Phpmyadmin details ...
I am trying to connect mysql with nodejs, but there is a problem in my code, please help! var express = require("express"); var mysql = require('mysql'); var connection = mysql.
me and a friend just started to work on a little App. It uses NodeJS (with Socket.io and Express). We're kind of new into NodeJS and thus can't really comprehend why we get the error we get. Every ...
I tried to connect between React(Front End) and NodeJs(BackEnd) and NodeJS -&gt; React(GET) worked well but React -&gt; NodeJS(POST) always erred &quot;POST http://localhost:3001/board/giveresult 5...
I have problem in connecting my create-react-app with mysql using npm. Its working in nodejs when i tried seperately without npm. But, When i trying to connect using npm install mysql, It throws ...
PHPMyAdmin is running on a VPS at http://ip_address:port_number. When trying to connect from a Nodejs app, I am getting the error: 'Connection lost: The server closed the connection'. Here's the code
ANy idea how can I connect from NodeJs to the Mysql ClearDB on Heroku? I was able to connect to the ClearDB Mysql runing on Heroku from Navicat, I even created a table called t_users. But I'm having
The code is very basic. Simple nodejs app using mysql. Error: connect ETIMEDOUT is received when code tries connecting to Google Cloud MySql server (second generation) on google app engine. Howeve...
I have a NodeJS application with Express as framework. In my app.js file I'm checking my connection to mysql database as below: var express = require('express'); var path = require('path'); var fa...

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.