gpt4 book ai didi

javascript - Node.js 不连接 MySQL, PHPMyAdmin

转载 作者:行者123 更新时间:2023-11-29 06:09:49 29 4
gpt4 key购买 nike

我正在尝试登录一个使用 mysql 存储用户名和密码的系统,但登录页面位于 manager.js 文件中,如下所示:

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;

当我在浏览器中键入 localhost:80 并运行 express 服务器时,它会显示以下屏幕:

enter image description here

现在用户名和密码应该存储在由其他人创建的 mysql 数据库中,我使用 phpmyadmin 添加数据库并可以完全访问它。但当然,由于数据库没有链接,我无法通过这个登录页面,它显示“localhost 拒绝连接”错误!

以下是我的控制台输出在我尝试登录页面之前:

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

那么,我的问题是我应该使用什么用户名和密码?我在哪里可以找到数据库中正确的?

编辑: 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;
};

最佳答案

在 ../utils/database 中,将用户名设置为“root”并将密码留空或“”,因为它是 windows lamp 和 xamp 设置中的默认设置。
编辑

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

关于javascript - Node.js 不连接 MySQL, PHPMyAdmin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38984306/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com