0

我想我已经用尽了所有尝试让 MySQL 连接到我的快速服务器的选项。搜索了谷歌机器等。我对 MySQL 还很陌生,尽管我最近不得不清理我的电脑,并且试图让 MySQL 工作无济于事。

我能够连接到 MySQL 并在本地创建一个数据库。但是,当尝试连接到服务器时,它看起来可以工作,然后大约 10 秒后我收到一个ETIMEDOUT错误。我猜这是某种网络错误,我似乎无法深入了解它。

值得我使用的是最新的 MacOS,Node v17.6.0,服务器版本:8.0.28 Homebrew

我验证了正确的端口:

mysql> SHOW GLOBAL VARIABLES LIKE 'PORT';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+
1 row in set (0.01 sec)

mysql> 

错误信息:

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> quit
Bye
chriss-air:demo-db pesarcomputer$ npm start

> demo-db@1.0.0 start
> node server.js

Connected to the election database
Server running on port 3306
node:events:505
      throw er; // Unhandled 'error' event
      ^

Error: connect ETIMEDOUT
    at Connection._handleTimeoutError (/Users/pesarcomputer/Dropbox/Mac/Documents/my-sql/demo-db/node_modules/mysql2/lib/connection.js:189:17)
    at listOnTimeout (node:internal/timers:559:17)
    at processTimers (node:internal/timers:502:7)
Emitted 'error' event on Connection instance at:
    at Connection._notifyError (/Users/pesarcomputer/Dropbox/Mac/Documents/my-sql/demo-db/node_modules/mysql2/lib/connection.js:236:12)
    at Connection._handleFatalError (/Users/pesarcomputer/Dropbox/Mac/Documents/my-sql/demo-db/node_modules/mysql2/lib/connection.js:167:10)
    at Connection._handleNetworkError (/Users/pesarcomputer/Dropbox/Mac/Documents/my-sql/demo-db/node_modules/mysql2/lib/connection.js:180:10)
    at Connection._handleTimeoutError (/Users/pesarcomputer/Dropbox/Mac/Documents/my-sql/demo-db/node_modules/mysql2/lib/connection.js:193:10)
    at listOnTimeout (node:internal/timers:559:17)
    at processTimers (node:internal/timers:502:7) {
  errorno: 'ETIMEDOUT',
  code: 'ETIMEDOUT',
  syscall: 'connect',
  fatal: true
}

Node.js v17.6.0
chriss-air:demo-db pesarcomputer$ lsof -i :3306
COMMAND    PID          USER   FD   TYPE            DEVICE SIZE/OFF NODE NAME
mysqld    3104 pesarcomputer   21u  IPv4 0xd408ceb9108e063      0t0  TCP localhost:mysql (LISTEN)
mysqld    3104 pesarcomputer   36u  IPv4 0xd408ceb96cb4b43      0t0  TCP localhost:mysql->localhost:49631 (ESTABLISHED)
TablePlus 3143 pesarcomputer   20u  IPv4 0xd408ceb9109c013      0t0  TCP localhost:49631->localhost:mysql (ESTABLISHED)
chriss-air:demo-db pesarcomputer$ 

这是我的代码:

const mysql = require("mysql2");
const express = require("express");

const PORT = process.env.PORT || 3306;
const app = express();

app.use(express.urlencoded({ extended: false }));
app.use(express.json());

const db = mysql.createConnection(
  {
    host: "localhost",
    port: "3306",
    user: "root",
    password: "password",
    database: "election",
    // debug: true,
  },
  console.log("Connected to the election database")
);

app.get("/", (req, res) => {
  res.json({
    message: "Hello World",
  });
});

// app.use((req, res) => {
//   res.status(404).end();
// });

app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});
4

2 回答 2

1

3306 是您的数据库端口。将另一个端口 (const PORT = process.env.PORT || 3306;) 更改为其他端口,通常为 3000 或 3001。

于 2022-03-06T01:24:18.613 回答
0

这修复了错误:

const mysql = require("mysql2");
const express = require("express");

const PORT = process.env.PORT || 3000;
const app = express();

app.use(express.urlencoded({ extended: false }));
app.use(express.json());

const db = mysql.createConnection(
  {
    host: "127.0.0.1",
    port: "3306",
    user: "root",
    password: "password",
    database: "election",
    // debug: true,
  },
  console.log("Connected to the election database")
);

但是,我想将 localhost 配置为 127.0.0.1 我该怎么做?

于 2022-03-06T01:37:16.137 回答