error when connecting mysql with Node.js

Asked

Viewed 206 times

2

am connecting mysql with Node but is giving this error:

C:\E. S\dao.js:10
  if (err) throw err
           ^

Error: getaddrinfo ENOTFOUND localhost:806 localhost:806:3306
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:58:26)
    --------------------
    at Protocol._enqueue (C:\E. S\node_modules\mysql\lib\protocol\Protocol.js:144:48)
    at Protocol.handshake (C:\E. S\node_modules\mysql\lib\protocol\Protocol.js:51:23)
    at Connection.connect (C:\E. S\node_modules\mysql\lib\Connection.js:119:18)
    at Object.<anonymous> (C:\E. S\dao.js:9:5)
    at Module._compile (internal/modules/cjs/loader.js:805:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:816:10)
    at Module.load (internal/modules/cjs/loader.js:672:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:612:12)
    at Function.Module._load (internal/modules/cjs/loader.js:604:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:868:12)

I took the code from this site: https://www.w3schools.com/nodejs/nodejs_mysql.asp

the code:

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost:806",
  user: "root",
  password: "usbw"
});

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

  • You can check if the door is actually 806 ?

  • Samuel, change the host port to see if it accesses: host: "localhost:3306",

  • so I just saw here my door is 3307, obg whatever thing you update the question with possible new bugs, obg

1 answer

1


To set the port with the function createConnection you must inform the door inside the property port:

// ...
const con = mysql.createConnection({
  host: 'localhost',
  port: 806,
  user: 'root',
  password: 'usbw',
});
// ...

Connection options

...

port: The port number to connect to. (Default: 3306)

...

In free translation:

...

port: The port number to connect. (Default: 3306)

...

  • should not be the port in localhost?

  • @Samuelsilva localhost is the host. This is other information. The port should be assigned in the attribute port.

  • so I just saw here my door is 3307, obg whatever thing you update the question with possible new bugs, obg

  • @Samuelsilva the procedure is you accept the answer, since it has already met you and if you have new questions create another question and do not update this with new mistakes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.