Home Error ECONNREFUSED NodeJS MySql
Reply: 0

Error ECONNREFUSED NodeJS MySql

Leonard
1#
Leonard Published in 2017-12-07 23:29:52Z

I tried connecting to my first MySql server with the following code

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "yourusername",
  password: "yourpassword"
});

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

But I get the following error. It only works if I comment out the "throw err" part

C:\NodeJS Projects>node demo_db_connection.js
C:\NodeJS Projects\demo_db_connection.js:10
  if (err) throw err;
       ^

Error: connect ECONNREFUSED 127.0.0.1:3306
    at Object.exports._errnoException (util.js:1022:11)
    at exports._exceptionWithHostPort (util.js:1045:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1087:14)
    --------------------
    at Protocol._enqueue (C:\NodeJS 
Projects\node_modules\mysql\lib\protocol\Protocol.js:145:48)
    at Protocol.handshake (C:\NodeJS 
Projects\node_modules\mysql\lib\protocol\Protocol.js:52:23)
    at Connection.connect (C:\NodeJS 
Projects\node_modules\mysql\lib\Connection.js:130:18)
    at Object.<anonymous> (C:\NodeJS Projects\demo_db_connection.js:9:5)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.333141 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO