Can';t将Node.js连接到远程数据库

Can';t将Node.js连接到远程数据库,node.js,mariadb,Node.js,Mariadb,根据教程,我尝试使用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 asyn

根据教程,我尝试使用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还不熟悉,我认为它应该是现成的。有人吗?

问题是,在phpMyAdmin中,我没有添加我的家庭ip地址以便连接。对于那些刚刚开始使用它的用户-您可以创建多个具有相同名称和密码的用户,这样您就可以从多个IP访问(如
localhost
::1
127.0.0.1
,这是完全相同的,但仍然是必需的)


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

因此与数据库的连接超时。您可以通过其他方式(如命令行客户端)从同时运行节点代码的计算机访问数据库吗?不完全可以。我可以访问phpMyAdmin,之所以使用Node是因为我们的开发人员走了,我需要创建连接到数据库的微服务,这样我们就不会被蒙在鼓里。PHP代码指向
localhost
,因此我在phpMyAdmin中找到了主机名,但仍然是相同的错误。我尝试使用
mysql
包,得到了“错误:ER\u HOST\u NOT\u PRIVILEGED:HOST“my ip here”不允许连接到此MariaDB服务器这是另一个问题,但可能更容易解决(授予客户IP地址的权限)。@jeand'arme-是的,这是可能的答案。自行回答此问题,然后接受您的答案。(让我们在不删除的情况下关闭此问题。)