如何解决 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',
address: '::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>