我正在尝试将NodeJS与MySQL数据库连接。我正在使用WAMP。它仅连接默认数据库,例如“ mysql”和“ information_schema”,但我创建的那些数据库未与NodeJ连接,并给了我以下错误。 请让我知道我要去哪里错了。
代码
**Server.js**
----------
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const mysql = require("mysql");
app.use(bodyParser.json());
var connection = mysql.createConnection({
host : "localhost",
user : "root", //User Name
password : "", // No password
database : "techbiz", // Database Name
multipleStatements:true
});
connection.connect((err)=>{
if(!err)
{
console.log("Database connected ");
}
else{
console.log(err) }
})
// Initialize port
const PORT = process.env.PORT || 5000;
// send rquest to web page
app.get('/',(req,res) => res.send(`Automize Business Solution running on :${PORT}`));
app.listen(PORT,()=>console.log("Server is running"));
[Database][1]
**Error**
----------
Server is running
{ Error: ER_BAD_DB_ERROR: Unknown database 'techbiz'
at Handshake.Sequence._packetToError (K:\WebProjects\MySQL\abs-server\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)
at Handshake.ErrorPacket (K:\WebProjects\MySQL\abs-server\node_modules\mysql\lib\protocol\sequences\Handshake.js:123:18)
at Protocol._parsePacket (K:\WebProjects\MySQL\abs-
at Socket.<anonymous> (K:\WebProjects\MySQL\abs-
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
code: 'ER_BAD_DB_ERROR',
errno: 1049,
sqlMessage: 'Unknown database \'techbiz\'',
sqlState: '42000',
fatal: true }
[1]: https://i.stack.imgur.com/E9MSc.png
谢谢
答案 0 :(得分:0)
我只是更改了我的wamp版本并使用MySQL 5.5.8,现在相同的代码可以正常工作。 谢谢