我想我已經用盡了所有嘗試讓 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}`);
});
uj5u.com熱心網友回復:
3306 是您的資料庫埠。將另一個埠 (const PORT = process.env.PORT || 3306;) 更改為其他埠,通常為 3000 或 3001。
uj5u.com熱心網友回復:
這修復了錯誤:
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 我該怎么做?
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/438533.html
