Error: connect ETIMEDOUT
at Connection._handleConnectTimeout (d:\课设\服务器端\node_modules\mysql\lib\Connection.js:409:13)
at Object.onceWrapper (node:events:628:28)
at Socket.emit (node:events:514:28)
at Socket._onTimeout (node:net:589:8)
at listOnTimeout (node:internal/timers:573:17)
at process.processTimers (node:internal/timers:514:7)
--------------------
at Protocol._enqueue (d:\课设\服务器端\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (d:\课设\服务器端\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (d:\课设\服务器端\node_modules\mysql\lib\Connection.js:116:18)
at Object.<anonymous> (d:\课设\服务器端\index.js:18:12)
at Module._compile (node:internal/modules/cjs/loader:1241:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1295:10)
at Module.load (node:internal/modules/cjs/loader:1091:32)
at Module._load (node:internal/modules/cjs/loader:938:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)
at node:internal/main/run_main_module:23:47 {
errorno: 'ETIMEDOUT',
code: 'ETIMEDOUT',
syscall: 'connect',
fatal: true
}
首先检查端口号,用户名,密码,数据库,主机有没有错误。
如果没有,再看自己的mysql的版本是不适8.0以上的,因为8.0以上的采用了新的加密方法,我们需要更换mysql驱动包,导入mysql2驱动包const mysql = require('mysql2')
或者
cmd管理员进入MySQL文件夹的bin目录
输入命令:mysql -u root -p
输入密码
alter USER ‘root’@‘localhost’ IDENTIFIED BY ‘password’ PASSWORD EXPIRENEVER;
alter USER ‘root’@‘localhost’ IDENTIFIED WITH mysql_native_password BY ‘修改自己的密码’;
nodejs连接mysql
npm install mysql
const mysql = require('mysql');
//创建连接
const con = mysql.createConnection({
host: '127.0.0.1',
user: 'root',
password: '你自己的密码',
port: '3306',// 自己的端口号,默认为3306
database: "自己的数据库"
});
//连接
con.connect(); //这里没报错的话就没有问题了,如果还是报错,那重新安装个低版本的数据库吧,卸载mysql找个靠谱的教程卸载,这玩意挺麻烦的。