繁体   English   中英

Node.JS 不会连接到 MariaDB

[英]Node.JS won't connect to MariaDB

我似乎无法让示例程序从 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();

这是我运行此代码时获得的 output。

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 完美连接。

似乎没有 mariadb 连接选项使非 IP 主机名查找有利于 IPv4。 https://github.com/nodejs/node/issues/40537#issuecomment-1237194449中显示的一般解决顺序修复:

import dns from 'node:dns';
dns.setDefaultResultOrder('ipv4first');

可能会解决这个问题,但会影响数据库连接以外的事情。

除此之外,您的选择似乎是指定 127.0.0.1 而不是 localhost,或者将您的服务器配置为也监听 ::1。

它似乎试图通过 ipv6 ( ::1 ) 连接。 我怀疑您的 MariaDb 实例仅绑定到 ipv4 地址,因此host: '127.0.0.1'可能有效。

您可以尝试或使用ss -atnl | grep 3306 ss -atnl | grep 3306看MariaDB绑定了什么地址。

您的计算机从 IPV4 和 IPV6 分配了 IP,默认情况下 dns 解析为分配不正确的 IPV6 的本地主机。

要修复此错误,请配置您的 DHCP 以分配正确的 IPV6 或更改主机:127.0.0.1(简单快速修复)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM