无法将Node.js连接到远程数据库MariaDB

5
我尝试使用基于Node.js的教程连接到我的MariaDB数据库。
    const mariadb = require('mariadb');
const pool = mariadb.createPool({
     host: 'myhost.com',
     user:'root', 
     password: 'password',
     database: 'db_p',
     connectionLimit: 2
});

async function asyncFunction() {
  let conn;
  try {
    console.log('establishing connection')
    conn = await pool.getConnection();
    console.log('established')
    const rows = await conn.query("SHOW TABLES");
    console.log(rows);

  } catch (err) {
    console.log(err)
      throw err;
  } finally {
      if (conn) return conn.end();
  }
}

但我遇到的问题是这个错误:
establishing connection
{ Error: retrieve connection from pool timeout
    at Object.module.exports.createError (/Users/jan/Developer/microservice/node_modules/mariadb/lib/misc/errors.js:55:10)
    at rejectTimeout (/Users/jan/Developer/microservice/node_modules/mariadb/lib/pool.js:267:16)
    at Timeout.rejectAndResetTimeout [as _onTimeout] (/Users/jan/Developer/microservice/node_modules/mariadb/lib/pool.js:287:5)
    at ontimeout (timers.js:486:15)
    at tryOnTimeout (timers.js:317:5)
    at Timer.listOnTimeout (timers.js:277:5)
  fatal: false,
  errno: 45028,
  sqlState: 'HY000',
  code: 'ER_GET_CONNECTION_TIMEOUT' }
(node:76515) UnhandledPromiseRejectionWarning: Error: retrieve connection from pool timeout
    at Object.module.exports.createError (/Users/jan/Developer/microservice/node_modules/mariadb/lib/misc/errors.js:55:10)
    at rejectTimeout (/Users/jan/Developer/microservice/node_modules/mariadb/lib/pool.js:267:16)
    at Timeout.rejectAndResetTimeout [as _onTimeout] (/Users/jan/Developer/microservice/node_modules/mariadb/lib/pool.js:287:5)
    at ontimeout (timers.js:486:15)
    at tryOnTimeout (timers.js:317:5)
    at Timer.listOnTimeout (timers.js:277:5)
(node:76515) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This errororiginated either by throwing inside of an async function without a catch block, or byrejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:76515) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我过去两年一直在编写JS代码,但是对于Node.js还比较陌生,我认为它应该可以开箱即用。有人能帮忙吗?


所以与数据库的连接超时了。您能否通过其他方式(例如命令行客户端)从运行您的Node代码的机器访问数据库? - robertklep
不是这样的。我可以访问phpMyAdmin,但我使用Node的原因是我们的开发人员已经离开了,我需要创建一个微服务来连接数据库,以便我们不会被困在黑暗中。PHP代码指向“localhost”,所以我在phpMyAdmin中找到了主机名,但仍然出现相同的错误。 - jean d'arme
我尝试使用mysql包,但是我得到了一个错误:ER_HOST_NOT_PRIVILEGED:主机'我的IP在这里'没有被允许连接到这个MariaDB服务器。 - jean d'arme
那是另一个问题,但可能更容易解决(授予客户端IP地址的权限)。 - robertklep
好的,问题解决了 - 我授予了我的IP权限。当然,这只是暂时的,但至少我现在可以工作了 :) 感谢您的帮助! - jean d'arme
2
@jeand'arme - 是的,那很可能是答案。自己回答这个问题,然后接受你的答案。(让我们关闭这个问题,而不是删除它。) - Rick James
3个回答

4
问题在于我没有将我的家庭IP地址添加到phpMyAdmin中,所以无法连接。对于那些刚开始使用的人-您可以创建多个具有相同名称和密码的用户,以便您可以实际从多个IP(如 localhost , :: 1 或 127.0.0.1 )访问,这几乎是相同的,但仍然需要确保。
我添加了一个具有相同凭据并指向我的IP的附加用户,问题得到解决。

4

对于其他遇到同样错误信息的人,特别是如果连接在最初的几次运行后无法再次连接,这可能是因为您没有使用conn.end结束连接而导致的错误。这不是OP的问题,但也许会影响其他人。


3
干杯!在使用pool.connect()(本意是用于pool.getConnection())时,我正在使用conn.release()。 使用conn.end()解决了我的问题。 - pxljoy
@pxljoy你的评论解决了这个问题,谢谢。 - mercury
@pxljoy你的评论解决了这个问题,谢谢。 - mercury

0
对我来说,问题通过将port: 3307作为另一个pool创建参数添加而得到解决。 端口3306似乎是默认的,但有些服务器似乎更喜欢3307。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接