Home > Blockchain >  Node.JS won't connect to MariaDB
Node.JS won't connect to MariaDB

Time:01-04

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.

CodePudding user response:

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.

CodePudding user response:

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)

  • Related