Nodejs Failing to Connect To WampServer MySQL Database

432 Views Asked by At

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 the localhost server. To ensure that I am using the right ports and hostnames, I have tried to set up 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
0

There are 0 best solutions below