最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js doesnot connect with MySQL, PHPMyAdmin - Stack Overflow

programmeradmin5浏览0评论

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:

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;

When I type localhost:80 on my browser and run the express server it displays the following screen:

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 "localhost refused to connect" error!

Following is my console output before I try to login to the page:

C:\Mrestro\RESTaurant_backend-master\rest-server>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

and when I type random username and password following is the output:

C:\Mrestro\RESTaurant_backend-master\rest-server>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.<anonymous> (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>

So, my question is what username and password should I use? Where can I find the correct one in the database?

EDIT: database.js

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;
};

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:

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;

When I type localhost:80 on my browser and run the express server it displays the following screen:

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 "localhost refused to connect" error!

Following is my console output before I try to login to the page:

C:\Mrestro\RESTaurant_backend-master\rest-server>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

and when I type random username and password following is the output:

C:\Mrestro\RESTaurant_backend-master\rest-server>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.<anonymous> (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>

So, my question is what username and password should I use? Where can I find the correct one in the database?

EDIT: database.js

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;
};
Share Improve this question edited Aug 17, 2016 at 4:58 Bikal Nepal asked Aug 16, 2016 at 20:54 Bikal NepalBikal Nepal 4871 gold badge10 silver badges27 bronze badges 7
  • Don't confuse the login here with your database credentials. That are two separate things. So find out where you need to add your DB credentials in this code and start of with filling your credentials from phpmyadmin there to see if it is working. To login here you need a username/password from this system. That's probably in the manual or installation instructions. – Jørgen Commented Aug 16, 2016 at 21:04
  • there is err with utils/database file can you show us that. – owais Commented Aug 16, 2016 at 21:12
  • Yes, I've addedn the database.js file in the edit above. – Bikal Nepal Commented Aug 17, 2016 at 4:57
  • I tried logging into manager.js with username and password as 'root' as given in database.js file but it shows username or password incorrect message. – Bikal Nepal Commented Aug 17, 2016 at 5:00
  • @owaishanif786 I have one last problem, will you please help me out? Thanks in advance :) – Bikal Nepal Commented Aug 18, 2016 at 11:20
 |  Show 2 more ments

1 Answer 1

Reset to default 4

In the ../utils/database set username to "root" and leave password empty or "" as it's default setting in windows lamp and xamp settings.
EDIT

var mysql = require('mysql');
var c = mysql.createConnection({
  host     : 'localhost',
 // port     : '3306',
  user     : 'root',
  password : '',
  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;
};
发布评论

评论列表(0)

  1. 暂无评论