Sunday, 4 June 2017

Error when trying to connect mysql to Nodejs

I have installed mysql with using npm then created a connection file :

    var express    = require("express");
    var mysql      = require('mysql');
    var connection = mysql.createConnection({
      host     : '127.0.0.1',
      user     : 'root',
      password : '',
      database : 'address_book',

    });
    var app = express();

    connection.connect(function(err){
    if(!err) {
        console.log("Database is connected ... nn");
    } else {
        console.log(err);
    };

When i connect using node connection.js I get this error :

    { Error: connect ECONNREFUSED 127.0.0.1:3306
          at Object.exports._errnoException (util.js:1033:11)
          at exports._exceptionWithHostPort (util.js:1056:20)
          at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1105:14)
          --------------------
          at Protocol._enqueue (/home/haddad/projects/plannerback/node_modules/mysql/lib/protocol/Protocol.js:141:48)
          at Protocol.handshake (/home/haddad/projects/plannerback/node_modules/mysql/lib/protocol/Protocol.js:52:41)
          at Connection.connect (/home/haddad/projects/plannerback/node_modules/mysql/lib/Connection.js:130:18)
          at Object.<anonymous> (/home/haddad/projects/plannerback/db/connect.js:12:20)
          at Module._compile (module.js:571:32)
          at Object.Module._extensions..js (module.js:580:10)
          at Module.load (module.js:488:32)
          at tryModuleLoad (module.js:447:12)
          at Function.Module._load (module.js:439:3)
          at Module.runMain (module.js:605:10)
        code: 'ECONNREFUSED',
        errno: 'ECONNREFUSED',
        syscall: 'connect',
        address: '127.0.0.1',
        port: 3306,
        fatal: true }

I try to ping to localhost 3306 , Port is not used :

    PING 3306 (0.0.12.234) 56(124) bytes of data.
    ^C
    --- 3306 ping statistics ---
    201 packets transmitted, 0 received, 100% packet loss, time 201592ms



via Hamza Haddad

No comments:

Post a Comment