我正在使用Node.js和Express开发一个简单的Web应用程序。我必须使用mysql模块查询数据库。每次我试图查询数据库时,它都会显示此错误:
code: 'ER_BAD_DB_ERROR',
errno: 1049,
sqlMessage: "Unknown database 'moneydb'",
sqlState: '42000',
fatal: true
字符串
我试图创建一个新的数据库,但总是出现同样的错误。Wamp正在运行。凭证是正确的,因为如果我导航到localhost/phpmyadmin,我会以root作为用户名登录,没有密码。
我在这里发布相关代码:
app.js
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
型
index.js
var express = require('express');
var router = express.Router();
var mysql = require('mysql');
// connection setup
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'moneydb'
});
// connect to database
connection.connect((err) => {
if (err) {
console.log('Not connected to database');
throw err;
} else {
console.log('Connected to database');
}
});
// make connection global
global.db = connection;
/*
* Every page we declare here will be under the index path
* localhost:3000/ will render home page
* localhost:3000/disheslist will be under the home page path and will render the disheslist
* */
/* GET home page. */
router.get('/', function (req, res, next) {
res.render('index', {title: 'Express'});
});
/*GET dish list. */
/*render contains views(pug files)*/
router.get('/dishes', function (req, res, next) {
let query = "SELECT * FROM `dish` ORDER BY id ASC";
// execute query
db.query(query, (err, result) => {
if (err) {
// we should redirect to an error page maybe
res.redirect('/');
}
if (result) {
res.render('dishes', {tableData: result});
} else {
res.send("Error");
}
});
});
module.exports = router;
型
dishes.pug
doctype html
html
head
title = title
link(rel='stylesheet', href='/stylesheets/style.css')
body
table
thead
tr
th id
th name
tbody
型
怎么了?谢谢你,谢谢
5条答案
按热度按时间xienkqul1#
问题解决了。
我在wamp上检查了MySql使用的端口,发现是3308端口。在连接index.js之前,我将端口添加到配置中,然后我得到了它。代码在此处更新:
index.js
字符串
谢谢大家!
9cbw7uwe2#
用户似乎没有数据库服务器的权限。查看错误消息,似乎请求能够到达服务器,但由于权限不足,服务器返回错误。
jfewjypa3#
我在数据库里留言:
'<DB NAME>'
的对象。字符串
eqzww0vc4#
问题已解决-更改了端口号。mysql db的端口是3360,index.js的端口应该是3360。
字符串
jv4diomz5#
在MySQL中的Ubuntu中转到终端。创建数据库:
字符串
它会解决你的问题。