VPS环境下Node.js后端无法连接MySQL数据库求助
Node.js(Sequelize)连接VPS上MySQL超时问题排查
我正在开发一款基于Node.js后端+MySQL数据库的Flutter聊天应用,本地开发环境一切正常,但部署到VPS后,Node.js代码无法连接MySQL,报错ETIMEDOUT,服务器日志显示SequelizeConnectionError连接超时。以下是相关信息,恳请帮忙排查:
报错日志
Unable to connect to the database: { SequelizeConnectionError: connect ETIMEDOUT at ConnectionManager.connect (/data/Jasper/SERVER_BTN95/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:102:17) at process._tickCallback (internal/process/next_tick.js:68:7) name: 'SequelizeConnectionError', parent: { Error: connect ETIMEDOUT at Connection._handleTimeoutError (/data/Jasper/SERVER_BTN95/node_modules/mysql2/lib/connection.js:189:17) at ontimeout (timers.js:436:11) at tryOnTimeout (timers.js:300:5) at listOnTimeout (timers.js:263:5) at Timer.processTimers (timers.js:223:10) errorno: 'ETIMEDOUT', code: 'ETIMEDOUT', syscall: 'connect', fatal: true }, original: { Error: connect ETIMEDOUT at Connection._handleTimeoutError (/data/Jasper/SERVER_BTN95/node_modules/mysql2/lib/connection.js:189:17) at ontimeout (timers.js:436:11) at tryOnTimeout (timers.js:300:5) at listOnTimeout (timers.js:263:5) at Timer.processTimers (timers.js:223:10) errorno: 'ETIMEDOUT', code: 'ETIMEDOUT', syscall: 'connect', fatal: true } } (node:3270005) UnhandledPromiseRejectionWarning: Error: SequelizeConnectionError: connect ETIMEDOUT at Server.<anonymous> (/data/Jasper/SERVER_BTN95/models/server.js:58:23) at Generator.throw (<anonymous>) at rejected (/data/Jasper/SERVER_BTN95/models/server.js:6:65) at process._tickCallback (internal/process/next_tick.js:68:7) (node:3270005) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:3270005) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
数据库连接代码
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const sequelize_1 = require("sequelize"); const db = new sequelize_1.Sequelize('cw', 'root', 'mypass', { dialect: 'mysql', host: '/*I put here my VPS IP but I don’t know if I have to put 10.0.0.2 or localhost*/', port: my_port, multipleStatements : true }); db.authenticate() .then(() => { console.log('Connection has been established successfully.'); }) .catch((err) => { console.log('Unable to connect to the database:', err); }); process.on('unhandledRejection', function(err) { console.log(err); // sendInTheCalvary(err); }); exports.default = db;
尝试过的无效Promise处理代码
const promise1 = new Promise(function(resolve, reject) { throw new Error('Promise 1 has tanked.'); }); const promise2 = new Promise(function(resolve, reject) { promise1.then(); }); (async function() { try { await promise2; } catch(err) { console.log(err); } })();
排查方向
MySQL绑定地址配置
检查MySQL配置文件(通常为my.cnf或my.ini)中的bind-address项:- 默认可能是
127.0.0.1,仅允许本地连接。若Node.js与MySQL在同一VPS,可改为0.0.0.0(允许所有IP)或内网IP10.0.0.2;若不在同一机器,必须设为0.0.0.0或VPS公网IP。 - 修改后重启MySQL服务:
sudo systemctl restart mysql
- 默认可能是
数据库用户权限
默认root用户可能仅允许localhost连接,需授权允许Node.js所在IP访问:GRANT ALL PRIVILEGES ON cw.* TO 'root'@'你的Node.js所在IP' IDENTIFIED BY 'mypass'; FLUSH PRIVILEGES;同VPS环境下也可使用
'root'@'%'允许所有IP,但需注意安全风险。VPS防火墙与安全组
- 检查VPS防火墙(如ufw、firewalld)是否开放MySQL端口(即配置中的
my_port):- ufw:
sudo ufw allow 你的端口号/tcp - firewalld:
sudo firewall-cmd --add-port=你的端口号/tcp --permanent && sudo firewall-cmd --reload
- ufw:
- 若Node.js与MySQL不在同一VPS,需同时开放VPS安全组(如阿里云、AWS安全组)的对应端口。
- 检查VPS防火墙(如ufw、firewalld)是否开放MySQL端口(即配置中的
连接地址正确性
- 若Node.js与MySQL在同一VPS,直接使用
localhost或127.0.0.1即可,无需公网IP,避免绕路或被防火墙拦截。 - 若为跨机器连接,确认MySQL服务的公网IP可正常访问,且端口开放。
- 若Node.js与MySQL在同一VPS,直接使用
Promise错误处理优化
你添加的Promise代码与数据库连接无关,无需保留。日志中的未处理Promise来自/data/Jasper/SERVER_BTN95/models/server.js:58:23处的Server类代码,需找到对应async函数,添加try/catch或.catch()处理。
内容的提问来源于stack exchange,提问作者Jerandy Landín Llópiz
相关产品推荐
相关产品推荐

