我的网站node.js索引和mysql之间的连接不起作用

0sgqnhkj  于 2021-06-15  发布在  Mysql
关注(0)|答案(4)|浏览(337)

我正在为一个学校项目建立一个网站,我正在尝试在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) 
d7v8vwbk

d7v8vwbk1#

-at protocol.\u排队(c:\users\andre\documents\mtsports\node\u modules\mysql\lib\protocol\protocol.js:144:48)at protocol.handshake(c:\users\andre\documents\mtsports\node\u modules\mysql\lib\protocol\protocol.js:51:23)at connection.connect(c:\users\andre\documents\mtsports\node\u modules\mysql\lib\connection.js:118:18)at object(c:\users\andre\documents\mtsports\index.js:32:5)at module.\u compile(module.js:652:30)at object.module.\u extensions..js(module.js:663:10)at module.load(module.js:565:32)at trymoduleload(module.js:505:12)at function.module.\u load(module.js:497:3)at function.module.runmain(module.js:693:10)code:'er\u not\u supported\u auth\u mode',errno:1251,sqlmessage:'客户端不支持服务器请求的身份验证协议;考虑升级mysql client',sqlstate:'08004',fatal:true}

bvn4nwqk

bvn4nwqk2#

-------- 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. (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\u不支持身份验证模式:客户端不支持服务器请求的身份验证协议;考虑在handshake.sequence.升级mysql客户端。\u packettoerror(c:\users\andre\documents\mtsports\node\modules\mysql\lib\protocol\sequences\sequence.js:47:14)at handshake.error packet(c:\users\andre\documents\mtsports\node\u modules\mysql\lib\protocol\sequences\handshake.js:124:18)at protocol.\u parsepacket(c:\users\andre\documents\mtsports\node\u modules\mysql\lib\protocol\protocol.js:278:23)在parser.write(c:\users\andre\documents\mtsports\node\u modules\mysql\lib\protocol\parser.js:76:12)在protocol.write(c:\users\andre\documents\mtsports\node\u modules\mysql\lib\protocol\protocol.js:38:16)(c:\users\andre\documents\mtsports\node\u modules\mysql\lib\connection.js:91:28)位于套接字(c:\users\andre\documents\mtsports\node\u modules\mysql\lib\connection.js:502:10)at emitone(events.js:116:13)at socket.emit(events.js:211:7)at addchunk(\u stream\u readable.js:263:12)
q9rjltbz

q9rjltbz3#

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

ryevplcw

ryevplcw4#

似乎mysql包有一个正在进行的配置问题。也许你可以试试这个npm-mysql库,我在工作/学校项目中使用它。单击此处
更新#1
登录到mysql工作台>转到“用户和权限”设置查找要登录的用户,并将“from host”更改为“%”
%表示通配符,mysql将允许从所有ip地址进行连接

相关问题