简体   繁体   English

无法使用Node.js连接到mysql(express)

[英]Can't connect to mysql (express) with Node.js

Hello Stacksoverflowers, 你好,Stacksoverflowers,

I have a problem to connect my server.js file to Mysql via Express. 我通过Express将server.js文件连接到Mysql时遇到问题。 I've already insatlled mysql and express via npm. 我已经安装了mysql,并通过npm进行了表达。

when i run my server.js 当我运行server.js时

The only thing it says in my console (MacOS default terminal) is: "node server running now - using http://localhost:3000 " (I got no errors) 它在控制台(MacOS默认终端)上说的唯一一句话是:“节点服务器现在正在运行-使用http:// localhost:3000 ”(我没有看到错误)

and my webpage also showing correct - "Node Server running - startpage" in the browser. 而且我的网页在浏览器中也显示了正确的“正在运行节点服务器-起始页”。

The problem is that they say nothing like "Connected to the MySQL server" or "Timeout: error messages" ?? 问题是他们没有说“已连接到MySQL服务器”或“超时:错误消息”之类的内容? (look at #3) (看#3)

// Dan k

//#1
// My codes from server.js

const express = require('express');
const mysql = require('mysql');
const hostname = "localhost:";
const port = 3000;

//#2
// create connection to DB 
const connection = mysql.createConnection({
host: "localhost",
user: 'root',
password: 'root',
port: 3000
});

//#3
// Connect to DB
connection.connect(function(err) {
if (!err) {
console.log('Connected to the MySQL server.');
}
else if (err)
{
return console.error('Timeout: ' + err.message);
}
});


// Routing with Express
const app = express();

//#4
// Startpage (root)
app.get('/', function(request, response){
response.send('Node Server running - startpage...');
response.end();
})

connection.end();

//#5
app.listen(port, (err) => {
if(err) throw err;
console.log("node server running now - using http://"+(hostname)+ . 
(port)");
});

I got what is causing the issue. 我知道是什么引起了问题。 You are using the same port number 3000 to connect to your mysqlDB and at the same time using the same port to run your nodejs application. 您使用相同的端口号3000连接到mysqlDB,同时使用相同的端口运行nodejs应用程序。 This does throw an error but it takes a really long time to throw the error. 这确实会引发错误,但是要花费很长时间才能引发错误。 So the simple answer here is that the port that you are mentioning in your createConnection() method is incorrect and needs to be changed. 因此,这里的简单答案是,您在createConnection()方法中提到的端口不正确,需要更改。 This port should be port number on which your mysql DB is running. 该端口应该是运行mysql DB的端口号。 From the error it is clear that your mysql DB is not running on port 3306 . 从错误中可以明显看出,您的mysql DB不在port 3306上运行。 You can check this post to figure out which port is your mysql DB running on. 您可以检查这篇文章,以确定您的mysql数据库正在哪个端口上运行。 https://serverfault.com/questions/116100/how-to-check-what-port-mysql-is-running-on . https://serverfault.com/questions/116100/how-to-check-what-port-mysql-is-running-on Below is a reference for you. 以下是给您的参考。 Please see the comments. 请查看评论。

// create connection to DB 
const connection = mysql.createConnection({
host: "localhost",
user: 'root',
password: 'root',
port: 3000 // You need to change this and put in the correct port number.
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM