Home > Blockchain >  Can't connect to remote db (Hostinger)
Can't connect to remote db (Hostinger)

Time:12-06

I've been having this error for a long time and I can't solve it. I have made my project locally, connecting to my database locally. Once finished I wanted to migrate my database manually since it is small to my hosting provider.

I have created the database, I have given it permissions to connect remotely and I have also checked that the connection data is correct.

A curiosity is that through mysql workbench it has allowed me to connect, the problem is clearly with my code, I am working with NodeJS (express). Another curiosity is that it appears to me as if I have successfully connected and 30 seconds later it gives me the following error:

    Node.js v18.12.0
[nodemon] app crashed - waiting for file changes before starting...
[nodemon] restarting due to changes...
[nodemon] starting `node app.js`
Listen on port 3000
Connection success!
node:events:491
  throw er; // Unhandled 'error' event
  ^

Error: read ECONNRESET
at TCP.onStreamRead (node:internal/stream_base_commons:217:20)
Emitted 'error' event on Connection instance at:
at Connection._handleProtocolError 
(C:\Users\M\Desktop\Proyect\node_modules\mysql\lib\Connection.js:423:8)
at Protocol.emit (node:events:513:28)
at Protocol._delegateError 
(C:\Users\M\Desktop\Proyect\node_modules\mysql\lib\protocol\Protocol.js:398:10)
at Protocol.handleNetworkError 
(C:\Users\M\Desktop\Proyect\node_modules\mysql\lib\protocol\Protocol.js:371:10)
at Connection._handleNetworkError 
(C:\Users\M\Desktop\Proyect\node_modules\mysql\lib\Connection.js:418:18)
at Socket.emit (node:events:513:28)
at emitErrorNT (node:internal/streams/destroy:151:8)
at emitErrorCloseNT (node:internal/streams/destroy:116:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
errno: -4077,
code: 'ECONNRESET',
syscall: 'read',
fatal: true
}

Node.js v18.12.0
[nodemon] app crashed - waiting for file changes before starting...

This is my connection script:

const mysql = require('mysql')
const connection = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE
});

connection.connect((error) => {
if(error){
    console.log('The connection error is: '   error)
    return;
}
console.log('Connection success!')
})


module.exports = connection;

CodePudding user response:

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
  // Now use the connection to do stuff
  con.query(sql, function (err, result) {
    if (err) throw err;
    console.log("Result: "   result);
  });
});

You don't show the rest of your code, but you're not doing any query in that callback function, just outputting success. You need to do stuff inside that callback, or change the way you're exporting the connection. Have a look at createPool, and getConnection as I'm guessing you're actually wanting a persistent connection to your database?

  • Related