title: Node.js doesnot connect with MySQL, PHPMyAdmin
body: <p>I'm trying to login to a system which uses mysql to store username and password but the login page is in manager.js file as below:</p>
  
  <pre><code>var express = require('express');
  var router = express.Router();
  var async = require('async');
  
  var util = require('../utils/util');
  var db = require('../utils/database');
  var connection = db.connection();
  
  router.get('/login', function (req, res) {
    if (req.session.manager) {
      return res.redirect('/');
    }
  
    if (req.query.tip == 'error') {
      var tip = 'username or password incorrect!';
    } else {
      var tip = null;
    }
    res.render('login', { tip: tip });
  });
  
  router.post('/login', function (req, res) {
    var username = req.body.username;
    var password = req.body.password;
    var sql = 'SELECT * FROM restaurant_accounts WHERE ra_name=?';
    connection.query(sql, [username], function (err, result) {
      if (err) throw err;
      if (result.length == 0) {
        return res.redirect('/manager/login?tip=error');
      } 
      var account = result[0];
      if (!util.checkHash(password, account.ra_password)) {
        return res.redirect('/manager/login?tip=error');
      }
  
      connection.query('SELECT * FROM restaurants WHERE rest_owner_id=?', [account.ra_id], function (err, result) {
        if (err) throw err;
        var restaurant = result[0];
        req.session.manager = {
          id: account.ra_id,
          name: account.ra_name,
          rest_id: restaurant.rest_id,
          rest_name: restaurant.rest_name
        };
        res.redirect('/');
      });
    });
  });
  
  router.get('/logout', function (req, res) {
    req.session.destroy();
    res.redirect('/manager/login');
  });
  
  module.exports = router;
  </code></pre>
  
  <p>When I type localhost:80 on my browser and run the express server it displays the following screen:</p>
  
  <p><a href="https://i.stack.imgur.com/vKrWG.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vKrWG.jpg" alt="enter image description here"></a></p>
  
  <p>Now the username and password are supposed to be stored in mysql database created by somebody else and I use phpmyadmin to add database and have full access to it. But ofcourse since the database is not linked, I can't get past this login page and it shows the "<strong>localhost</strong> refused to connect" error!</p>
  
  <p>Following is my console output <strong>before I try to login to the page</strong>:</p>
  
  <pre><code>C:\Mrestro\RESTaurant_backend-master\rest-server&gt;node bin\www
  Express server listening on port 80
  GET /manager/login 304 23ms
  GET /css/bootstrap.css 304 7ms
  GET /css/main.css 304 6ms
  GET /js/jquery.min.js 304 11ms
  GET /images/bg.jpg 304 2ms
  </code></pre>
  
  <p>and when I type random username and password following is the output:</p>
  
  <pre><code>C:\Mrestro\RESTaurant_backend-master\rest-server&gt;node bin\www
  Express server listening on port 80
  GET /manager/login 304 23ms
  GET /css/bootstrap.css 304 7ms
  GET /css/main.css 304 6ms
  GET /js/jquery.min.js 304 11ms
  GET /images/bg.jpg 304 2ms
  C:\Mrestro\RESTaurant_backend-master\rest-server\web\manager.js:27
      if (err) throw err;
               ^
  
  Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'bjtu'@'localhost' (using password: YES)
      at Handshake.Sequence._packetToError (C:\Mrestro\RESTaurant_backend-master\rest-server\node_modules\mysql\lib\protocol\sequences\Sequence.js:51:14)
      at Handshake.ErrorPacket (C:\Mrestro\RESTaurant_backend-master\rest-server\node_modules\mysql\lib\protocol\sequences\Handshake.js:103:18)
      at Protocol._parsePacket (C:\Mrestro\RESTaurant_backend-master\rest-server\node_modules\mysql\lib\protocol\Protocol.js:280:23)
      at Parser.write (C:\Mrestro\RESTaurant_backend-master\rest-server\node_modules\mysql\lib\protocol\Parser.js:74:12)
      at Protocol.write (C:\Mrestro\RESTaurant_backend-master\rest-server\node_modules\mysql\lib\protocol\Protocol.js:39:16)
      at Socket.&lt;anonymous&gt; (C:\Mrestro\RESTaurant_backend-master\rest-server\node_modules\mysql\lib\Connection.js:109:28)
      at emitOne (events.js:77:13)
      at Socket.emit (events.js:169:7)
      at readableAddChunk (_stream_readable.js:153:18)
      at Socket.Readable.push (_stream_readable.js:111:10)
      --------------------
      at Protocol._enqueue (C:\Mrestro\RESTaurant_backend-master\rest-server\node_modules\mysql\lib\protocol\Protocol.js:141:48)
      at Protocol.handshake (C:\Mrestro\RESTaurant_backend-master\rest-server\node_modules\mysql\lib\protocol\Protocol.js:52:41)
      at Connection.connect (C:\Mrestro\RESTaurant_backend-master\rest-server\node_modules\mysql\lib\Connection.js:136:18)
      at Connection._implyConnect (C:\Mrestro\RESTaurant_backend-master\rest-server\node_modules\mysql\lib\Connection.js:467:10)
      at Connection.query (C:\Mrestro\RESTaurant_backend-master\rest-server\node_modules\mysql\lib\Connection.js:212:8)
      at Object.handle (C:\Mrestro\RESTaurant_backend-master\rest-server\web\manager.js:26:14)
      at next_layer (C:\Mrestro\RESTaurant_backend-master\rest-server\node_modules\express\lib\router\route.js:103:13)
      at Route.dispatch (C:\Mrestro\RESTaurant_backend-master\rest-server\node_modules\express\lib\router\route.js:107:5)
      at C:\Mrestro\RESTaurant_backend-master\rest-server\node_modules\express\lib\router\index.js:195:24
      at Function.proto.process_params (C:\Mrestro\RESTaurant_backend-master\rest-server\node_modules\express\lib\router\index.js:251:12)
  
  C:\Mrestro\RESTaurant_backend-master\rest-server&gt;
  </code></pre>
  
  <p>So, my question is what username and password should I use? Where can I find the correct one in the database?</p>
  
  <p><strong>EDIT:</strong> database.js</p>
  
  <pre><code>var mysql = require('mysql');
  var c = mysql.createConnection({
    host     : 'localhost',
   // port     : '3306',
    user     : 'root',
    password : 'root',
    database : 'restaurant'
  
  });
  
  // enable error logging for each connection query
  c.on('error', function(err) {
    console.log(err.code); // example : 'ER_BAD_DB_ERROR'
  });
  
  exports.connection = function() {
      return c;
  };
  </code></pre>