我正在尝试使用以下视频连接到 MySQL 数据库:https://www.youtube.com/watch?v=f5kye3ESXE8。我的app.js文件如下:
const express = require('express');
const bodyParser = require("body-parser");
const mysql = require('mysql');
const app=express()
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
//MySQL
const pool=mysql.createPool({
connectionLimit : 10,host : 'localhost',user: 'root',password: '',database: 'nodejs_beers'
})
//want to be able to get all beers in the database
app.get('',(req,res) => {
pool.getconnection((err,connection) => {
if(err) {
console.log(err)
}
//query(sqlString),callback
connection.query('SELECT * from beers',(err,rows) => {
connection.release() //release the connection to pool
if(!err){
res.send(rows)
}
else{
console.log(err)
}
})
})
})
//make sure app is listening on 5000
app.listen(5000,() => console.log(`listen on port 5000`));
我已经完全按照视频进行操作,但是当我尝试在浏览器中输入“localhost:5000”时出现以下错误:
[nodemon] starting `node app.js`
listen on port 5000
Error: connect ECONNREFUSED 127.0.0.1:3306
我查看了 xampp mysql doesn't run on port 3306 但我不知道如何让 XAMPP 在另一个端口上运行 MySQL,而且我找不到 my.cnf 文件来尝试更改它(如果是它应该如何更改)。 我在 MacOS Catalina 上,XAMPP 版本是 7.4.2
我还应该注意,每当我在 Web 浏览器中输入“localhost:8080”时,我都会被带到“欢迎使用 XAMPP”页面,我相信这意味着 Apache 服务器正在正确运行。我已经仔细检查过,我已经在 XAMPP(Apache、MySQL 和 ProFTPD)上运行了所有 3 个服务。