为什么在nodejs中出现错误连接-mysql(尝试远程连接)

时间:2018-09-13 18:00:40

标签: mysql node.js dbconnection

我最近开始学习Nodejs以连接到MySQL,但遇到连接问题。我尝试使用腻子进行连接,但没有任何问题,

这是我的 example.route.js 文件

const express = require('express'),
    router = express.Router(),
    mysql = require('mysql');

var connection = mysql.createConnection({
    host: <ipaddress>,
    port: <default Port>,
    user: 'root',
    password: <given password>,
    database: <db name>,
    connectionTimeout: 30000
});

connection.connect((error) => {
    if (error) {
        console.log('error connecting: ' + error);
    } else {
        console.log('Connected to server');
    }
});

connection.query('SELECT * FROM test', function(err, results, fields) {

    console.log('results in table: ' + results);
    console.log('fields in table: ' + fields); // -1

    connection.release();

    if (err) throw error;
});
connection.end();

module.exports = router;

我收到以下错误=> Error: connect ECONNREFUSED (将在几小时内更新为完全错误)

正如我之前提到的,我使用 PUTTY 来确保连接时出现问题,但是我能够使用给定的数据库名称,具有相同用户名和密码的主机进行连接。

不确定使用MySQL的 ubuntu服务器

是否有帮助

任何人都知道为什么我遇到连接错误?我会很感激的

0 个答案:

没有答案
相关问题