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