0

I can't seem to get the example program to work from MariaDB's documentation.

Here's the code I am using which is a minimally modified version from the example in the docs.

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();

This is the output that I obtain if I run this code.

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

My questions are any one of the following:

  • Why does this happen?
  • Where can I find the logs to obtain more information?
  • Do I have to enable the logs, if so, how?
  • How can I debug this issue?

I can connect perfectly fine using the CLI and Beekeeper Studio.

3
  • dumb question, is the mariadb running? got firewall? connection refused can be of many things, firewall on the way is one. Commented Jan 3, 2023 at 21:58
  • sudo systemctl status mariadb suggests yes, and I am running on the same system, so I would have thought firewall configuration was unlikely to affect things. How can I check that? Commented Jan 3, 2023 at 22:00
  • This is a Debian 11 system btw. Commented Jan 3, 2023 at 22:00

3 Answers 3

2

There doesn't seem to be a mariadb connection option to make non IP hostname lookup favor IPv4. The general resolve order fix shown in https://github.com/nodejs/node/issues/40537#issuecomment-1237194449:

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

may fix this, but would affect things other than the database connection.

Other than that, it seems your choices are to specify 127.0.0.1 instead of localhost, or to configure your server to also listen on ::1.

Sign up to request clarification or add additional context in comments.

Comments

1

It seems to be trying to connect over ipv6 (::1). I suspect that your MariaDb instance only bound to an ipv4 address, and hence host: '127.0.0.1' might work.

You can try that or use ss -atnl | grep 3306 to see what addresses MariaDB is bound to.

1 Comment

That seems to have solved it. Is there a way to use localhost in the configuration while also telling mariadb to connect via ipv4 only? ie: Is there an ipv4 option which I can use as part of the connection configuration?
0

You computer is assigned IPs from IPV4 and IPV6 and by default dns resolve to localhost for IPV6 which is incorrectly assigned.

To fix this error either configure your DHCP to assign proper IPV6 or change host:127.0.0.1 (simple and quick fix)

2 Comments

localhost shouldn't be going through DHCP
@ysth correct silly me then disabling the IPV6 in TCP/IP shall fix it so that no IPV6 is bound to localhost.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.