这是我的第一个stackoverflow帖子,我目前正在为学校做一个项目,但不幸的是我无法修复这个bug。
我有一个node.js脚本,它应该处理一个JSON文件并将其插入MariaDB数据库。然而,当我执行该脚本时,在10秒后出现以下错误消息(超时)。我怀疑这不是由于脚本的处理功能,而是之前已经超时。
数据库和脚本都在我的树莓派4与Rasbian
来自德国法兰克福的Alex
错误消息:
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'
}
字符串
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 :::* -
型
当我使用一个没有连接池的简单脚本时,我会得到一个不同的错误消息:
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:1 27: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'
}
型
我已经阅读了错误消息上的每一个Google条目,并询问了AI,但不幸的是,到目前为止还没有任何方法奏效。
2条答案
按热度按时间disho6za1#
错误消息“EASP.REFUSED::1:3306”表明它正在尝试通过IPv6连接,而mariadb只在IPv4中侦听(从netstat输出)。
尝试更改代码以强制IPv4连接:
字符串
fivyi3re2#
Reda的回答在技术上是正确的,但是,我想建议一种替代方法:配置MariaDB同时侦听IPv4和IPv6。这种方法提供了与未来应用程序的更无缝集成,因为它允许使用主机名“localhost”,而无需指定IP地址。
此外,采用IPv6符合当前的最佳实践,并可为您的设置提供未来保障。以双堆栈模式运行服务可确保IPv4和IPv6连接的兼容性。作为专业人士,我们越来越熟悉IPv6及其优势也很重要。
要实现这一点,您需要在MariaDB配置文件中添加一个额外的bind-address条目,该条目通常位于/etc/mysql/my.cnf或/etc/mysql/mariadb.conf.d/50-server. cnf。只需在现有的bind-address行之后添加以下行:
字符串