You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js连接MariaDB超时及连接被拒问题求助(树莓派环境)

Node.js连接MariaDB超时/连接拒绝问题求助

我是一名学生,正在进行课程项目开发,这是我首次在StackOverflow发帖求助。我编写了一个Node.js脚本,用于读取并处理JSON文件,将数据插入MariaDB数据库,但执行脚本10秒后出现超时错误。我推测问题出在数据库连接阶段,而非数据处理环节。数据库与脚本均部署在运行Rasbian系统的树莓派4上。


使用连接池时的错误信息

Error:  SqlError: (conn=-1, no: 45028, SQLState: HY000) retrieve connection from pool timeout after 10002ms
    (pool connections: active=0 idle=0 limit=5)
    at module.exports.createError (/home/alexpi/node_modules/mariadb/lib/misc/errors.js:64:10)
    at Pool._requestTimeoutHandler (/home/alexpi/node_modules/mariadb/lib/pool.js:349:26)
    at listOnTimeout (node:internal/timers:564:17)
    at process.processTimers (node:internal/timers:507:7) {
  sqlMessage: 'retrieve connection from pool timeout after 10002ms\n' +
    '    (pool connections: active=0 idle=0 limit=5)',
  sql: null,
  fatal: false,
  errno: 45028,
  sqlState: 'HY000',
  code: 'ER_GET_CONNECTION_TIMEOUT'
}

使用无连接池脚本时的错误信息

Error fetching data: Error: connect ECONNREFUSED ::1:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1481:16)
 From event:
    at /home/alexpi/node_modules/mariadb/lib/connection.js:138:13
    at new Promise (<anonymous>)
    at Connection.connect (/home/alexpi/node_modules/mariadb/lib/connection.js:127:12)
    at Object.createConnection (/home/alexpi/node_modules/mariadb/promise.js:38:17)
    at fetchDataFromDatabase (/home/alexpi/weather/testdirect.js:14:26)
    at Object.<anonymous> (/home/alexpi/weather/testdirect.js:25:1)
    at Module._compile (node:internal/modules/cjs/loader:1218:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1272:10)
    at Module.load (node:internal/modules/cjs/loader:1081:32)
    at Module._load (node:internal/modules/cjs/loader:922:12) {
  errno: -111,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '::1',
  port: 3306,
  fatal: true,
  sqlState: 'HY000'
}

我的Node.js脚本代码

const mariadb = require('mariadb');
const moment = require('moment-timezone');
const fs = require('fs');

// Read the JSON file
const rawData = fs.readFileSync('weather.json');
const weatherData = JSON.parse(rawData);

// Database connection configuration
const pool = mariadb.createPool({
  host: 'localhost',
  user: 'query',
  password: 'query_pw',
  database: 'weather',
  connectionLimit: 5,
});

async function processData() {
  // Create a connection
  let conn;
  try {
    conn = await pool.getConnection();

    // Delete existing entries in the database
    await conn.query('DELETE FROM allData');

    // Iterate over each weather entry
    for (const entry of weatherData.weather) {
      // Parse and convert timestamp to CET
      const timestampCET = moment(entry.timestamp).tz('Europe/Berlin');

      // Round values
      const temperature = Math.round(entry.temperature);
      const windSpeed = Math.round(entry.wind_speed);
      const precipitation = Math.round(entry.precipitation);
      const precipitationProbability = Math.round(entry.precipitation_probability);

      // Insert data into the database
      await conn.query(
        'INSERT INTO allData (date, time, temperature, wind_speed, precipitation, precipitation_probability, icon) VALUES (?, ?, ?, ?, ?, ?, ?)',
        [
          timestampCET.format('YYYY-MM-DD'),
          timestampCET.format('HH:mm:ss'),
          temperature,
          windSpeed,
          precipitation,
          precipitationProbability,
          entry.icon,
        ]
      );
    }

    console.log('Data inserted successfully.');
  } catch (err) {
    console.error('Error: ', err);
  } finally {
    if (conn) conn.release(); // release connection
  }
}

// Process the data
processData();

执行netstat -tunlp的输出

(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State                                                                                                                                      PID/Program name
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN                                                                                                                                     -
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN                                                                                                                                     -
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN                                                                                                                                     -
tcp6       0      0 :::1883                 :::*                    LISTEN                                                                                                                                     -
tcp6       0      0 ::1:631                 :::*                    LISTEN                                                                                                                                     -
tcp6       0      0 :::22                   :::*                    LISTEN                                                                                                                                     -
udp        0      0 0.0.0.0:42112           0.0.0.0:*                                                                                                                                                          -
udp        0      0 0.0.0.0:5353            0.0.0.0:*                                                                                                                                                          -
udp        0      0 0.0.0.0:631             0.0.0.0:*                                                                                                                                                          -
udp6       0      0 :::5353                 :::*                                                                                                                                                               -
udp6       0      0 fe80::fed7:dfaa:a02:546 :::*                                                                                                                                                               -
udp6       0      0 :::42625                :::*                                                                                                                                                               -

我已经查阅了相关谷歌搜索结果并咨询过AI,但目前仍未找到解决方法,恳请各位提供帮助。


内容的提问来源于stack exchange,提问作者alexodexo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 20:12:04