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

时间:2018-11-20 13:05:47

标签: node.js mariadb

我尝试根据this教程使用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的新手,我认为它应该可以立即使用。有人吗?

2 个答案:

答案 0 :(得分:0)

问题是在phpMyAdmin中我没有添加我的家庭IP地址,所以我可以连接。对于刚开始使用它的用户-您可以使用相同的名称和密码创建多个用户,以便实际上可以从多个IP(例如localhost::1127.0.0.1)访问完全相同,但仍需确定)。

我添加了其他具有相同凭据的用户,这些用户指向我的IP,它解决了问题。

答案 1 :(得分:0)

对于其他具有相同错误消息的人,特别是如果连接在前几次有效但之后无效,则如果您不使用 conn.end 结束连接,则可能会发生错误。不是 OP 问题,而是其他问题。