gpt4 book ai didi

javascript - MySQL连接错误node.js events.js 182

转载 作者:行者123 更新时间:2023-11-29 18:31:36 24 4
gpt4 key购买 nike

我是 Node.js 新手,我原本计划使用 PHP,但想获得使用 Node 的经验。我尝试使用 Node.js 连接到 MySQL 数据库,但在尝试连接时出现以下错误。

感谢您的帮助!!

这是JS代码

    var mysql = require('mysql')
var con = mysql.createConnection({
host: 'localhost',
user: 'lcoleman0422',
password:'Micheal4',
database:'Top Level',
port:3306
});


con.connect();
/* function(err){
if (err) throw err
con.query('select * from Customer',function(error,result){
if(error)throw error;
console.log(result[0].First_Name);
}
)
});*/

con.end();

这是我的 MYSQL_Connection.js 中的代码

Connected
events.js:182
throw er; // Unhandled 'error' event
^

Error: Connection lost: The server closed the connection.
at Protocol.end (/home/lcoleman0422/node_modules/mysql/lib/protocol/Protocol.js:113:13)
at Socket.<anonymous> (/home/lcoleman0422/node_modules/mysql/lib/Connection.js:109:28)
at emitNone (events.js:110:20)
at Socket.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1045:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
lcoleman0422@p3plcpnl0867 [~/public_html/js]$

最佳答案

尝试此代码,如果您对此有任何疑问,请告诉我们。

const mysql = require('mysql');
const express = require('express');
const app = express();
const connection = mysql.createConnection({
host: 'localhost',
user: 'lcoleman0422',
password: 'Micheal4',
database: 'Top Level'
});

connection.connect();

app.get('/', (req, res) => {
connection.query(SELECT * FROM Customer, (error, data) => {
if(error) => {
console.log(error)
}else{
res.json(data[0].First_Name)
}
});
});

关于javascript - MySQL连接错误node.js events.js 182,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45643736/

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