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

m4pnthwp  于 2022-11-08  发布在  Node.js
关注(0)|答案(3)|浏览(250)

我尝试使用基于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的新手,我认为它应该开箱即用。有人知道吗?

ubbxdtey

ubbxdtey1#

问题是在phpMyAdmin中我没有添加我的家庭IP地址,所以我可以连接.对于那些刚开始使用它的人-你可以用相同的名字和密码创建多个用户,这样你就可以从多个IP进行访问(比如localhost::1127.0.0.1,它们是完全相同的,但仍然是必需的).
我已经添加了其他用户与相同的凭据指向我的IP,它解决了这个问题。

qxgroojn

qxgroojn2#

对于具有相同错误消息的其他人,特别是如果连接在最初几次工作,但在那之后就不工作了,则如果不使用conn.end结束连接,则可能发生错误。这不是OP的问题,而是其他问题。

zf9nrax1

zf9nrax13#

对我来说,通过添加port: 3307作为另一个pool创建参数解决了这个问题。
Port 3306 seems to be default,但有些服务器似乎更喜欢3307。

相关问题