Home > Software engineering >  nodejs express reconnect mysql ER_ACCESS_DENIED_ERROR: Access denied for user ''@'loc
nodejs express reconnect mysql ER_ACCESS_DENIED_ERROR: Access denied for user ''@'loc

Time:12-31

at first my sql code was :

var express = require('express');

const mysql = require("mysql");

var db = mysql.createConnection({
    host: 'ipaddress',
    user: 'username',
    password: 'password',
    database: 'my77admin'
});

db.connect(function (err) {
    if (err) throw err;
    console.log("Connected!");
});

and it's keep disconnect so after some research I add a handle disconnect to reconnect mysql below of code above

function handleDisconnect(myconnection) {
    myconnection.on('error', function (err) {
        console.log('\nRe-connecting lost connection: '   err.stack);
        db.destroy();
        db = mysql.createConnection(db.db);
        handleDisconnect(db);
        db.connect();
    });
}

handleDisconnect(db);


module.exports = db

and now the problem become error log in my pm2

│││ www > error when connecting to db: Error: ER_ACCESS_DENIED_ERROR: Access denied for user ''@'localhost' (using password: NO) │
│││ www >     at Handshake.Sequence._packetToError (/root/my77admin/node_modules/mysql/lib/protocol/sequences/Sequence.js:47:14) │
│││ www >     at Handshake.ErrorPacket (/root/my77admin/node_modules/mysql/lib/protocol/sequences/Handshake.js:123:18)│
│││ www >     at Protocol._parsePacket (/root/my77admin/node_modules/mysql/lib/protocol/Protocol.js:291:23)│
│││ www >     at Parser._parsePacket (/root/my77admin/node_modules/mysql/lib/protocol/Parser.js:433:10)    │
│││ www >     at Parser.write (/root/my77admin/node_modules/mysql/lib/protocol/Parser.js:43:10) │
│││ www >     at Protocol.write (/root/my77admin/node_modules/mysql/lib/protocol/Protocol.js:38:16)        │
│││ www >     at Socket.<anonymous> (/root/my77admin/node_modules/mysql/lib/Connection.js:88:28)│
│││ www >     at Socket.<anonymous> (/root/my77admin/node_modules/mysql/lib/Connection.js:526:10)          │
│││ www >     at Socket.emit (node:events:390:28)         │
│││ www >     at addChunk (node:internal/streams/readable:324:12)         │
│││ www >     --------------------  │
│││ www >     at Protocol._enqueue (/root/my77admin/node_modules/mysql/lib/protocol/Protocol.js:144:48)    │
│││ www >     at Protocol.handshake (/root/my77admin/node_modules/mysql/lib/protocol/Protocol.js:51:23)    │
│││ www >     at Connection.connect (/root/my77admin/node_modules/mysql/lib/Connection.js:116:18)          │
│││ www >     at Timeout.handleDisconnect [as _onTimeout] (/root/my77admin/routes/products_listing.js:19:14)          │
│││ www >     at listOnTimeout (node:internal/timers:568:17)   │
│││ www >     at processTimers (node:internal/timers:510:7) {  │
│││ www >   code: 'ER_ACCESS_DENIED_ERROR',    │
│││ www >   errno: 1045, │
│││ www >   sqlMessage: "Access denied for user ''@'localhost' (using password: NO)",│
│││ www >   sqlState: '28000',      │
│││ www >   fatal: true

What should I do? and I can confirm that the first connection is working. but after few second it start to loop the error

CodePudding user response:

var mysql = require('mysql')
var connection = mysql.createPool({
  host: "localhost",
  user: "root",
  password: "",
  database: "dbname",
  port : "3306"
})
connection.getConnection((err, connection) => {
    if (err) {
        if (err.code === 'PROTOCOL_CONNECTION_LOST') {
            console.error('Database connection was closed.')
        }
        if (err.code === 'ER_CON_COUNT_ERROR') {
            console.error('Database has too many connections.')
        }
        if (err.code === 'ECONNREFUSED') {
            console.error('Database connection was refused.')
        }
    }
    if (connection) connection.release()
    return
})
module.exports = connection

example : https://github.com/vishalims095/nodeJS_Mysql/blob/developer/src/Modules/connection.js

  • Related