我的网站的node.js索引与mysql之间的连接无法正常工作

时间:2019-01-07 17:33:24

标签: mysql node.js

我正在为一个学校项目建立一个网站,并且试图在mysql和我的项目之间建立连接。 我不知道为什么,但是没有连接,也没有创建数据库

我已经有用户mtsports了,他的密码是1234,我已授予他所有访问权限。

index.js

const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
var expressSanitizer = require('express-sanitizer');
var mysql = require('mysql');
const app = express();
const routes = require('./routes/routes');
app.use(bodyParser.urlencoded({extended : true}));
app.use(expressSanitizer());
const session = require('express-session');
const PORT = 3000;

app.use(express.static(path.join(__dirname, 'public')));
app.set('view engine', 'pug');
app.engine('html', require('ejs').renderFile);
app.set('views', './views');

app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: false
}));


// Instancia da BD
var con = mysql.createConnection({
host: "localhost:3306",
user: "mtsports",
password: "1234"
});

con.connect(function(err) {
if (err){ //TODO : Conexão da BD
    console.log(err);
    return;
}else{
    console.log("Connected!");
}  
con.query("CREATE DATABASE mydb", function (err, result) {
  if (err) throw err;
  console.log("Database created");
});
});

//app.use('/', routes);

app.listen(PORT, () => {
console.log('À escuta na porta ' + PORT);
});

app.get('/', (req, res) => {
res.render(__dirname + '/views/home.pug');
});

错误:

{ Error: getaddrinfo ENOTFOUND localhost:3306 localhost:3306:3306 at errnoException (dns.js:50:10) at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:92:26) ---------------------------- at Protocol._enqueue (C: \Users\andre\DocumentsV'llSports\node_modules\mysql\lib\protocol\Protocol .15:144:48) at Protocol.handshake (C:\Users\andre\Documents\MTSports\node_modules\mysql\lib\protocol\Protocol.js:S1:23) at Connection.connect (C:\Users\andre\Documents\MTSports\node_modules\mysql\lib\Connection.js:118:18) at Object.<anonymous> (C:\Users\andre\Documents\MTSports\index.js:32:5) at Module._compile (module.js:652:30) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12) at Function.Module._load (module.js:497:3) at Function.Module.runMain (module.js:693:10) code. 'ENOTFOUND', errno: 'ENOTFOUND', syscall: 'getaddrinfo', hostname: 'localhost:3306', host: 'localhost:3306', port: 3306, fatal: true

更新

当我更改host: "127.0.0.1"

var con = mysql.createConnection({
host: "127.0.0.1",
user: "mtsports",
password: "1234"
});

这是出现的错误:

  

{错误:ER_NOT_SUPPORTED_AUTH_MODE:客户端不支持   服务器请求的认证协议;考虑升级MySQL   客户       在Handshake.Sequence._packetToError(C:\ Users \ andre \ Documents \ MTSports \ node_modules \ mysql \ lib \ protocol \ sequences \ Sequence.js:47:14)       在Handshake.ErrorPacket(C:\ Users \ andre \ Documents \ MTSports \ node_modules \ mysql \ lib \ protocol \ sequences \ Handshake.js:124:18)       在Protocol._parsePacket(C:\ Users \ andre \ Documents \ MTSports \ node_modules \ mysql \ lib \ protocol \ Protocol.js:278:23)       在Parser.write(C:\ Users \ andre \ Documents \ MTSports \ node_modules \ mysql \ lib \ protocol \ Parser.js:76:12)       在Protocol.write(C:\ Users \ andre \ Documents \ MTSports \ node_modules \ mysql \ lib \ protocol \ Protocol.js:38:16)       在套接字。 (C:\ Users \ andre \ Documents \ MTSports \ node_modules \ mysql \ lib \ Connection.js:91:28)       在套接字。 (C:\ Users \ andre \ Documents \ MTSports \ node_modules \ mysql \ lib \ Connection.js:502:10)       在emitOne上(events.js:116:13)       在Socket.emit(events.js:211:7)       在addChunk(_stream_visible.js:263:12)       --------------------       在Protocol._enqueue(C:\ Users \ andre \ Documents \ MTSports \ node_modules \ mysql \ lib \ protocol \ Protocol.js:144:48)       在Protocol.handshake(C:\ Users \ andre \ Documents \ MTSports \ node_modules \ mysql \ lib \ protocol \ Protocol.js:51:23)       在Connection.connect(C:\ Users \ andre \ Documents \ MTSports \ node_modules \ mysql \ lib \ Connection.js:118:18)       在对象。 (C:\ Users \ andre \ Documents \ MTSports \ index.js:32:5)       在Module._compile(module.js:652:30)       在Object.Module._extensions..js(module.js:663:10)       在Module.load(module.js:565:32)       在tryModuleLoad(module.js:505:12)       在Function.Module._load(module.js:497:3)       在Function.Module.runMain(module.js:693:10)代码:'ER_NOT_SUPPORTED_AUTH_MODE',errno:1251,sqlMessage:'客户端不支持身份验证协议   服务器请求;考虑升级MySQL客户端',sqlState:   '08004',致命:true}

2 个答案:

答案 0 :(得分:0)

似乎mysql软件包存在持续的配置问题。也许您可以尝试一下我用于工作/学校项目的npm mysql库。 click here

更新#1

登录到mysql工作台>转到“用户和特权”设置,找到您要登录的用户,将“从主机”更改为“%”

%表示通配符,mysql将允许来自所有IP地址的连接

enter image description here

答案 1 :(得分:0)

请尝试更正您的MySQL连接配置。在主机字段中删除端口,并在单独的字段中提供端口。还提供您的数据库名称(可选)。
var con = mysql.createConnection({ host: "localhost", user: "mtsports", port: "3306", database: "dbname", password: "1234" });