title: Error: connect ECONNREFUSED 127.0.0.1:3306 ( DB not connecting in node js with mysql)
body: <p>I was building a simple rest API with node and mysql. But while creating the connection to the database I am getting this error.</p>
  <blockquote>
  <p>Error: connect ECONNREFUSED 127.0.0.1:3306
  at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
  --------------------
  at Protocol._enqueue (/Users/shehriyarnadeem/Documents/others/learning_projects/linkedinclone/node_modules/mysql/lib/protocol/Protocol.js:144:48)
  at Protocol.handshake (/Users/shehriyarnadeem/Documents/others/learning_projects/linkedinclone/node_modules/mysql/lib/protocol/Protocol.js:51:23)
  at Connection.connect (/Users/shehriyarnadeem/Documents/others/learning_projects/linkedinclone/node_modules/mysql/lib/Connection.js:116:18)
  at Object. (/Users/shehriyarnadeem/Documents/others/learning_projects/linkedinclone/config/database.js:12:6)
  at Module._compile (internal/modules/cjs/loader.js:1138:30)
  at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
  at Module.load (internal/modules/cjs/loader.js:986:32)
  at Function.Module._load (internal/modules/cjs/loader.js:879:14)
  at Module.require (internal/modules/cjs/loader.js:1026:19)
  at require (internal/modules/cjs/helpers.js:72:18) {   errno: 'ECONNREFUSED',   code: 'ECONNREFUSED',   syscall: 'connect',<br />
  address: '127.0.0.1',   port: 3306,   fatal: true }</p>
  </blockquote>
  <p>database.js</p>
  <blockquote>
  <p>var mysql = require(&quot;mysql&quot;);</p>
  <p>let pool = mysql.createConnection({   host: &quot;localhost&quot;,   user:
  &quot;root&quot;,   port: &quot;3306&quot; /* port on which phpmyadmin run */,   password:
  &quot;&quot;,</p>
  <p>database: &quot;dbname&quot;, });</p>
  <p>pool.connect((err) =&gt; {   console.log(err); });</p>
  <p>module.exports = pool;</p>
  </blockquote>