0

I am new to web development and I have started working on Nodejs. I am trying to connect to the MySQL Server on port 3308, WampServer but instead I always get connected to localhost server. To ensure that I am using right ports and hostnames, I have tried to setup the connection using MYSQL Workbench and it worked! But still, I am not able to figure out how to connect with mysql driver.

const sql = require('mysql');
var con = sql.createConnection({
   host: '127.0.0.1:3308',
   user: 'root',
   password: '',
   port: 3308
});
con.connect(function (err) {
    if (err) throw err;
    console.log("Connected!");
});

createDatabaseQuery= theQ =>{
   return "CREATE DATABASE ".concat(theQ);
}

con.query(createDatabaseQuery('nodedb'), function(err) {
   if (err) {
     throw err;

   }
});

Here is my output:

[Running] node "c:\Users\Asus\WebstormProjects\Impulse\MySQLTest.js"
c:\Users\Asus\WebstormProjects\Impulse\MySQLTest.js:8
    if (err) throw err;
             ^

Error: connect ECONNREFUSED 127.0.0.1:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1137:16)
    --------------------
    at Protocol._enqueue (c:\Users\Asus\WebstormProjects\Impulse\node_modules\mysql\lib\protocol\Protocol.js:144:48)
    at Protocol.handshake (c:\Users\Asus\WebstormProjects\Impulse\node_modules\mysql\lib\protocol\Protocol.js:51:23)
    at Connection.connect (c:\Users\Asus\WebstormProjects\Impulse\node_modules\mysql\lib\Connection.js:116:18)
    at Object.<anonymous> (c:\Users\Asus\WebstormProjects\Impulse\MySQLTest.js:7:5)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47 {
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 3306,
  fatal: true
}

[Done] exited with code=1 in 1.198 seconds
James Z
  • 12,209
  • 10
  • 24
  • 44
jsgrewal12
  • 128
  • 9
  • Port `3308` or, more likely, `3306`? And, `127.0.0.1` *means* `localhost`. – O. Jones Apr 08 '20 at 19:10
  • Hi, I have resolved this issue by uninstalling WampServer on my System. Apparently it was clouding over the MySQL server I wanted to connect with. Issue was arisen due to different versions of MySQL. Thanks! – jsgrewal12 Apr 30 '20 at 18:59

0 Answers0