英文:
nodejs not able to connect to maria db. Able to connect through command line
问题
以下是翻译好的内容,不包括代码部分:
I am getting the following error when connecting to db
我在连接数据库时遇到以下错误:
Error connecting to database: (conn=-1, no: 45028, SQLState: HY000) retrieve connection from pool timeout after 10002ms
连接数据库时出错:(conn=-1, no: 45028, SQLState: HY000) 在10002毫秒后从连接池中检索连接超时
(pool connections: active=0 idle=0 limit=10)
(连接池连接:活动=0 空闲=0 限制=10)
SqlError: (conn=-1, no: 45028, SQLState: HY000) retrieve connection from pool timeout after 10002ms
SqlError:(conn=-1, no: 45028, SQLState: HY000) 在10002毫秒后从连接池中检索连接超时
(pool connections: active=0 idle=0 limit=10)
at Object.module.exports.createError (/home/simha/app/node_modules/mariadb/lib/misc/errors.js:57:10)
在 Object.module.exports.createError (/home/simha/app/node_modules/mariadb/lib/misc/errors.js:57:10) 处发生
at Pool._requestTimeoutHandler (/home/simha/app/node_modules/mariadb/lib/pool.js:344:26)
在 Pool._requestTimeoutHandler (/home/simha/app/node_modules/mariadb/lib/pool.js:344:26) 处发生
at listOnTimeout (node:internal/timers:559:17)
在 listOnTimeout (node:internal/timers:559:17) 处发生
at processTimers (node:internal/timers:502:7)
在 processTimers (node:internal/timers:502:7) 处发生
{
{
text: 'retrieve connection from pool timeout after 10002ms\n' +
text: '在10002毫秒后从连接池中检索连接超时\n' +
' (pool connections: active=0 idle=0 limit=10)',
' (连接池连接:活动=0 空闲=0 限制=10)',
sql: null,
sql: null,
fatal: false,
fatal: false,
errno: 45028,
errno: 45028,
sqlState: 'HY000',
sqlState: 'HY000',
code: 'ER_GET_CONNECTION_TIMEOUT'
code: 'ER_GET_CONNECTION_TIMEOUT'
}
and this is my code
这是我的代码
I am able to connect to the database using command line
我可以使用命令行连接到数据库
mariadb --host mariadb -P 3306 --user simha –pkrishna
mariadb --host mariadb -P 3306 --user simha –pkrishna
not able to understand what's the issue
不明白问题出在哪里
英文:
I am getting the following error when connecting to db
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 | }
and this is my code
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;
I am able to connect to the database using command line
mariadb --host mariadb -P 3306 --user simha –pkrishna
not able to understand whats the issue
答案1
得分: 1
这听起来可能是一个"网络连接"问题。
Q:您的节点应用程序是否在与您验证命令行的主机相同的主机上运行,具有相同的IP地址?
例如:
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()重试连接:
'use strict'; const dbpool = require('mysql').createPool({ connectionLimit: 10, acquireTimeout: 30000, waitForConnections: true, database: 'mydatabase', host: 'localhost', multipleStatements: true, password: 'mypassword', user: 'root', }); const sql = 'SELECT * FROM sometable;'; const attemptConnection = () => dbpool.getConnection((err, connection) => { if (err) { console.log('error connecting. retrying in 1 sec'); setTimeout(attemptConnection, 1000); } else { connection.query(sql, (errQuery, results) => { connection.release(); if (errQuery) { console.log('Error querying database!'); } else { console.log('Successfully queried database.'); } }); } }); attemptConnection();
英文:
This sounds like it could be a "network connectivity" issue.
Q: Is your node app running on the same host - with the same IP - as the host where you verified the command line?
FOR EXAMPLE:
> https://stackoverflow.com/a/53418273/421195
>
> The problem was that in phpMyAdmin I didn't added my home ip address
> so I can connect. For those who are just starting with it - you can
> create multiple users with the same name and password so you can
> actually have access from multiple IP's (like localhost, ::1 or
> 127.0.0.1 which is quite the same, but still required just for sure).
>
> I have added additional user with same credentials pointing to my IP
> and it solved the problem.
>
ANOTHER POSSIBILITY:
> https://stackoverflow.com/a/68406734/421195
>
> For others with the same error message, particularly if the connection
> works the first few times but not after that, the error can happen if
> you don't end the connection with conn.end. Not OPs problem, but
> perhaps others.
FINALLY: everything might actually be working correctly:
>
> https://stackoverflow.com/a/52225583/421195
>
> Each connection in the pool will time out, because using release()
> does not close the connection but give it back to the pool.
>
> So your disconnects are pretty normal and you should handle the error
> appropriately.
>
> The connections are recreated automatically, see
> https://github.com/mysqljs/mysql#poolcluster-options
This example might give you some helpful tips:
> https://stackoverflow.com/a/59761773/421195
>
> Retry to connect with setTimeout():
>
> 'use strict';
>
> const dbpool = require('mysql').createPool({
> connectionLimit: 10,
> acquireTimeout: 30000,
> waitForConnections: true,
> database: 'mydatabase',
> host: 'localhost',
> multipleStatements: true,
> password: 'mypassword',
> user: 'root',
> });
>
> const sql = 'SELECT * FROM sometable;';
>
> const attemptConnection = () =>
> dbpool.getConnection((err, connection) => {
> if (err) {
> console.log('error connecting. retrying in 1 sec');
> setTimeout(attemptConnection, 1000);
> } else {
> connection.query(sql, (errQuery, results) => {
> connection.release();
> if (errQuery) {
> console.log('Error querying database!');
> } else {
> console.log('Successfully queried database.');
> }
> });
> }
> });
>
> attemptConnection();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论