无法连接到mysql远程数据库

时间:2020-12-27 18:04:12

标签: node.js mysql-connector

我尝试使用 node.jsmysql 连接到远程数据库:

const mysql = require('mysql2/promise');

(async () => {
    try {
        db = await mysql.createConnection({
            host: 'jdbc:mysql://REMOTE_IP',
            port: '3306',
            user: '***',
            password: '***',
            database: '***',
        });
        console.log('DB connection established.');
    } catch(e) {
        console.log(`DB connection error: ${e}.`);
    }
})();

但我进入了控制台:

DB connection error: Error: getaddrinfo ENOTFOUND jdbc:mysql://REMOTE_IP.

我已尝试通过 workbench 连接相同的详细信息,并且可以连接。 为什么此 node.js 代码不起作用?

1 个答案:

答案 0 :(得分:0)

根据 documentation,您应该只为 host 设置主机名或 IP 地址:

const db = await mysql.createConnection({
    host: 'REMOTE_IP',
    port: '3306',
    user: '***',
    password: '***',
    database: '***',
});

(注意:我还在 const 变量前添加了一个 db。)

相关问题