如何解決 Node.js 後端上的錯誤:連接 ECONNREFUSED : :1:3306?
<p>我重建了我的託管伺服器,但在設定 mysql 後,我從 Node.js 後端收到錯誤:</p>
<pre class="brush:php;toolbar:false;">Error: connect ECONNREFUSED ::1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16)
--------------------
at Protocol._enqueue (/home/smartcrop/smartcrop/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/home/smartcrop/smartcrop/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at Connection.connect (/home/smartcrop/smartcrop/node_modules/mysql/lib/Connection.js:116:18)
at Connection._implyConnect (/home/smartcrop/smartcrop/node_modules/mysql/lib/Connection.js:454:10)
at Connection.query (/home/smartcrop/smartcrop/node_modules/mysql/lib/Connection.js:196:8)
at file:///home/smartcrop/smartcrop/server/index.js:165:10 {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
加值: '::1',
port: 3306,
fatal: true
}
file:///home/smartcrop/smartcrop/server/index.js:196
if (result.length > 0) {
^
TypeError: Cannot read properties of undefined (reading 'length')
at Query.<anonymous> (file:///home/smartcrop/smartcrop/server/index.js:196:20)
at Query.<anonymous> (/home/smartcrop/smartcrop/node_modules/mysql/lib/Connection.js:526:10)
at Query._callback (/home/smartcrop/smartcrop/node_modules/mysql/lib/Connection.js:488:16)
at Sequence.end (/home/smartcrop/smartcrop/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)
at /home/smartcrop/smartcrop/node_modules/mysql/lib/protocol/Protocol.js:236:14
at process.processTicksAndRejections (node:internal/process/task_queues:77:11)
Node.js v18.15.0</pre>
<p>當嘗試在我的前端註冊和登入時,資料庫密碼設定為“”,主機名稱設定為 localhost 等,我已經在我的 ufw 上允許 3306,但仍然遇到相同的錯誤。</p>
<p>如果這有幫助,以下是我控制台的控制台錯誤:</p>
<pre class="brush:php;toolbar:false;">從來源「http://localhost:3000」存取「http://157.245.193.191/register」的 XMLHttpRequest 已被 CORS 策略阻止:請求的資源上不存在「Access-Control-Allow-Origin」標頭。
xhr.js:217
POST http://157.245.193.191/register net::ERR_FAILED 502(網關錯誤)
調度XhrRequest@xhr.js:217
xhr @ xhr.js:41
調度請求@dispatchRequest.js:43
請求@Axios.js:123
http方法@Axios.js:159
換行@bind.js:5
註冊@Signup.js:55
callCallback @react-dom.development.js:3733
invokeGuardedCallbackDev @react-dom.development.js:3777
invokeGuardedCallback @react-dom.development.js:3834
invokeGuardedCallbackAndCatchFirstError @react-dom.development.js:3848
執行調度@react-dom.development.js:7992
processDispatchQueueItemsInOrder @react-dom.development.js:8018
processDispatchQueue @react-dom.development.js:8029
dispatchEventsForPlugins @react-dom.development.js:8038
(匿名)@react-dom.development.js:8198
batchedUpdates$1 @react-dom.development.js:22595
大量更新@react-dom.development.js:3581
dispatchEventForPluginEventSystem@react-dom.development.js:8197
dispatchEventWithEnableCapturePhaseSelectiveHydrationWithoutDiscreteEventReplay @react-dom.development.js:5703
調度事件@react-dom.development.js:5697
調度離散事件@react-dom.development.js:5674
xhr.js:135 未捕獲(承諾)AxiosError {訊息:'網路錯誤',名稱:'AxiosError',代碼:'ERR_NETWORK',配置:{...},請求:XMLHttpRequest,...}
P</pre>
<p>我完全迷失了區別,與我之前的託管伺服器設定唯一的是 mysql 有一個用戶密碼,與我的前端的總體區別是伺服器的 IP 位址,但我不確定 NginX 是否有一些東西與它有關。</p>