连接到数据库时出现以下错误
node_server_1 | Error connecting to database: (conn=-1, no: 45028, SQLState: HY000) retrieve connection from pool timeout after 10002ms
node_server_1 | (pool connections: active=0 idle=0 limit=10)
node_server_1 | SqlError: (conn=-1, no: 45028, SQLState: HY000) retrieve connection from pool timeout after 10002ms
node_server_1 | (pool connections: active=0 idle=0 limit=10)
node_server_1 | at Object.module.exports.createError (/home/simha/app/node_modules/mariadb/lib/misc/errors.js:57:10)
node_server_1 | at Pool._requestTimeoutHandler (/home/simha/app/node_modules/mariadb/lib/pool.js:344:26)
node_server_1 | at listOnTimeout (node:internal/timers:559:17)
node_server_1 | at processTimers (node:internal/timers:502:7) {
node_server_1 | text: 'retrieve connection from pool timeout after 10002ms\n' +
node_server_1 | ' (pool connections: active=0 idle=0 limit=10)',
node_server_1 | sql: null,
node_server_1 | fatal: false,
node_server_1 | errno: 45028,
node_server_1 | sqlState: 'HY000',
node_server_1 | code: 'ER_GET_CONNECTION_TIMEOUT'
node_server_1 | }
这是我的代码
import dotenv from "dotenv";
import mariadb from "mariadb";
if (process.env.NODE_ENV !== "production") {
dotenv.config();
}
console.log(process.env.DB_HOST)
console.log(process.env.DB_USER)
console.log(process.env.DB_PASSWORD)
console.log(process.env.DB_NAME)
const pool = mariadb.createPool({
hostname: 'mariadb',
user: 'simha',
password: 'krishna',
database: 'blogs'
});
try {
console.log(process.env.DB_HOST,process.env.DB_USER,process.env.DB_PASSWORD,process.env.DB_NAME)
const conn = await pool.getConnection();
console.log("Connected to database.");
conn.release();
} catch (err) {
console.error("Error connecting to database:", err.message);
console.log(err)
await pool.end();
process.exit(1);
}
export default pool;
我能够使用命令行连接到数据库
mariadb --host mariadb -P 3306 --user simha –pkrishna
我不明白这是什么问题
1条答案
按热度按时间egdjgwm81#
这听起来像是一个“网络连接”问题。
问:您的节点应用程序是否运行在与验证命令行的主机相同的主机上?
例如:
https://stackoverflow.com/a/53418273/421195
问题是,在phpMyAdmin中,我没有添加我的家庭IP地址,以便我可以连接。对于那些刚刚开始使用它的人-你可以创建多个具有相同名称和密码的用户,这样你就可以从多个IP访问(如localhost,::1或127.0.0.1,这是完全相同的,但仍然需要确保)。
我已经添加了额外的用户与相同的凭据指向我的IP,它解决了问题。
另一种可能性:
https://stackoverflow.com/a/68406734/421195
对于具有相同错误消息的其他人,特别是如果连接在前几次工作但之后不工作,如果不使用conn. end结束连接,则可能会发生错误。不是OP的问题,但可能是其他的问题。
最后,一切都可能正常工作:
https://stackoverflow.com/a/52225583/421195
池中的每个连接都将超时,因为使用release()不会关闭连接,而是将其返回池。
因此,您的断开连接是非常正常的,您应该适当地处理错误。
连接将自动重新创建,请参见https://github.com/mysqljs/mysql#poolcluster-options
这个例子可能会给予你一些有用的提示:
https://stackoverflow.com/a/59761773/421195
使用setTimeout()重试连接: