最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - MariaDB nodejs connector gets timeout for remote DB, but mysql nodejs connector does connect - Stack Overflow

programmeradmin2浏览0评论

I've got MariaDB listening on a remote server.

I am able to connect to it using the mysql npm package,
but the mariadb npm package fails, reaching timeout.

The same mariadb code did work when I was physically at work.
However, this is probably not a firewall issue, as the mysql code still works here at home.

import dotenv from "dotenv";
dotenv.config();

// ------------------
// mysql driver WORKS
// ------------------

import mysql from "mysql";

const mysqlConn = mysql.createConnection({
  host: process.env.DB_HOST,
  user: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_NAME,
});

mysqlConn.connect();

mysqlConn.query("SELECT 'OK' AS status", (error, results) => {
  if (error) throw error;
  console.log("mysql status:", results[0].status); // prints "mysql status: OK"
});

mysqlConn.end();

// ---------------------------
// mariadb driver DOESN'T work
// ---------------------------

import mariadb from "mariadb";

const mariaPool = mariadb.createPool({
  host: process.env.DB_HOST,
  user: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_NAME,
});

async function testMariaConnection() {
  let conn = null;
  try {
    conn = await mariaPool.getConnection();
    const rows = await conn.query("SELECT 1 + 1 AS solution"); // connection timeout error
    console.log("The solution is:", rows[0].solution);
  } finally {
    if (conn !== null) conn.release();
  }
}

testMariaConnection();

MariaDB nodejs connector prints this error:

SqlError: (conn:-1, no: 45028, SQLState: HY000) 
retrieve connection from pool timeout after 10001ms
    (pool connections: active=0 idle=0 limit=10)
{
  sql: null,
  fatal: false,
  errno: 45028,
  sqlState: 'HY000',
  code: 'ER_GET_CONNECTION_TIMEOUT'
}

I've got MariaDB listening on a remote server.

I am able to connect to it using the mysql npm package,
but the mariadb npm package fails, reaching timeout.

The same mariadb code did work when I was physically at work.
However, this is probably not a firewall issue, as the mysql code still works here at home.

import dotenv from "dotenv";
dotenv.config();

// ------------------
// mysql driver WORKS
// ------------------

import mysql from "mysql";

const mysqlConn = mysql.createConnection({
  host: process.env.DB_HOST,
  user: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_NAME,
});

mysqlConn.connect();

mysqlConn.query("SELECT 'OK' AS status", (error, results) => {
  if (error) throw error;
  console.log("mysql status:", results[0].status); // prints "mysql status: OK"
});

mysqlConn.end();

// ---------------------------
// mariadb driver DOESN'T work
// ---------------------------

import mariadb from "mariadb";

const mariaPool = mariadb.createPool({
  host: process.env.DB_HOST,
  user: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_NAME,
});

async function testMariaConnection() {
  let conn = null;
  try {
    conn = await mariaPool.getConnection();
    const rows = await conn.query("SELECT 1 + 1 AS solution"); // connection timeout error
    console.log("The solution is:", rows[0].solution);
  } finally {
    if (conn !== null) conn.release();
  }
}

testMariaConnection();

MariaDB nodejs connector prints this error:

SqlError: (conn:-1, no: 45028, SQLState: HY000) 
retrieve connection from pool timeout after 10001ms
    (pool connections: active=0 idle=0 limit=10)
{
  sql: null,
  fatal: false,
  errno: 45028,
  sqlState: 'HY000',
  code: 'ER_GET_CONNECTION_TIMEOUT'
}
Share Improve this question edited Feb 8 at 13:05 Nitsan BenHanoch asked Feb 7 at 16:04 Nitsan BenHanochNitsan BenHanoch 6996 silver badges7 bronze badges 1
  • The solution should be in an Answer below, not edited into the question. – Barmar Commented Feb 8 at 0:25
Add a comment  | 

1 Answer 1

Reset to default 0

Solved by adding connectTimeout: 30_000 to mariadb config (default was 10_000), though in reality it only takes ~4 seconds to start, connect, query and disconnect.

Actually, Even connectTimeout: 5_000 makes it work, but if I do connectTimeout: 10_000 it reaches timeout after 10 seconds. Wtf.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论