从MariaDB的文档中我似乎无法让示例程序工作。
这是我使用的代码,这是一个最低限度的修改版本的例子在文档中。
const mariadb = require('mariadb');
const pool = mariadb.createPool({
host: 'localhost',
user: 'admin',
password: 'adminpassword',
database: 'userdb',
port: 3306,
connectionLimit: 5
});
async function f() {
console.log(pool);
let connection;
try {
console.log('Connection start');
connection = await pool.getConnection();
console.log('Connected');
const rows = await connection.query('select * from users');
console.log(rows);
const res = await connection.query('insert into users value (?, ?)', [1, 'testuser']);
console.log(res);
await connection.release();
}
catch(exception) {
console.log(exception);
}
if(connection) {
return connection.end();
}
}
f();
这是我运行这段代码时得到的输出。
ode index.js
PoolPromise {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
[Symbol(kCapture)]: false
}
Connection start
SqlError: (conn=-1, no: 45028, SQLState: HY000) retrieve connection from pool timeout after 10001ms
(pool connections: active=0 idle=0 limit=5)
at module.exports.createError (.../node_modules/mariadb/lib/misc/errors.js:57:10)
at Pool._requestTimeoutHandler (.../node_modules/mariadb/lib/pool.js:345:26)
at listOnTimeout (node:internal/timers:564:17)
at process.processTimers (node:internal/timers:507:7) {
text: 'retrieve connection from pool timeout after 10001ms\n' +
' (pool connections: active=0 idle=0 limit=5)',
sql: null,
fatal: false,
errno: 45028,
sqlState: 'HY000',
code: 'ER_GET_CONNECTION_TIMEOUT'
}
node:internal/process/promises:289
triggerUncaughtException(err, true /* fromPromise */);
^
Error: connect ECONNREFUSED ::1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1471:16)
From event:
at .../node_modules/mariadb/lib/connection.js:115:13
at new Promise (<anonymous>)
at Connection.connect (.../node_modules/mariadb/lib/connection.js:103:12)
at Pool._createConnection (.../node_modules/mariadb/lib/pool.js:402:16)
at Pool._doCreateConnection (.../node_modules/mariadb/lib/pool.js:40:10)
at listOnTimeout (node:internal/timers:564:17)
at process.processTimers (node:internal/timers:507:7)
Emitted 'error' event on PoolPromise instance at:
at Pool.emit (node:events:513:28)
at .../node_modules/mariadb/lib/pool.js:258:22
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '::1',
port: 3306,
fatal: true,
sqlState: 'HY000'
}
Node.js v19.3.0
我的问题是以下任何一个:
- 为什么会发生这种情况?
- 在哪里可以找到日志以获取更多信息?
- 是否必须启用日志?如果必须,如何启用?
- 如何调试此问题?
我可以使用CLI和Beekeeper Studio完美地连接。
3条答案
按热度按时间nxagd54h1#
似乎没有mariadb连接选项使非IP主机名查找优先使用IPv4。www.example.com中显示了常规解析顺序修复https://github.com/nodejs/node/issues/40537#issuecomment-1237194449:
可以修复此问题,但会影响数据库连接以外的其他内容。
除此之外,您的选择似乎是指定127.0.0.1而不是localhost,或者配置您的服务器也侦听::1。
brtdzjyr2#
它似乎试图通过ipv6(
::1
)连接。我怀疑您的MariaDb示例只绑定到ipv4地址,因此host: '127.0.0.1'
可能会工作。您可以尝试这样做,或者使用
ss -atnl | grep 3306
查看MariaDB绑定到哪些地址。yks3o0rb3#
为您的计算机分配了IPV4和IPV6中的IP,默认情况下,DNS解析为分配错误的IPV6的localhost。
要修复此错误,请配置DHCP以分配正确的IPV6或更改主机:127.0.0.1(简单快速修复)