Sunday, February 20, 2022

[FIXED] Sometime I can't connect to mysql db with node.js while offline until I'm online, other time it just works, why?

Issue

My node.js program use the mysql module which I use with xampp. Why does it (sometime)require an internet connection to connect to my db in localhost using the mysql node module while other times it works fine while offline? It won't connect to my db which looks fine in phpmyadmin while offline but once I tried while online then it worked for a while offline then it seems to require internet once again. Why?


Solution

Behalf on using 'localhost' as your host you should use '127.0.0.1' That Will solve your problem

const host='127.0.0.1';
const user='root';
const password='root';
const database='node_db'

var connection = mysql.createConnection({
  host:host,
  user:user,
  password: password,
  database: database
})


Answered By - Pratik Hublikar

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.