繁体   English   中英

错误:在node.js中连接ECONNREFUSED-数据库

[英]Error: connect ECONNREFUSED in node.js - database

我是node.js的新手,可以从书中学习它。 我的书中有一些代码,启动时会产生一个错误。 我在这里寻找解决方案,但有人对我有解决方案。 所以我决定问自己一个问题。

我的代码是在postgreSQL上运行的,它是require('pg'); 它已经安装在node_modules

这是我来自一个文件的所有代码:postgreSQL.js:

var pg = require('pg');
var conString = "tcp://root:root@localhost:5432/dataBase";
var client = new pg.Client(conString);
client.connect();

client.query(
    'insert into users (name, age) values ("$1, $2") returning id',
    ['Michal', 29],
    function(err, result){
        if(err) throw err;
        console.log('Id form this record '+ result.rows[0].id);
    }
);

当我要启动它时,会在cmd中生成一个错误:

      throw er; // Unhandled 'error' event
            ^
Error: connect ECONNREFUSED
    at exports._errnoException (util.js:746:11)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1010:19)

我正在尝试检查telnet中的端口,从这里调试我的代码和所有解决方案。 有人帮忙吗?

根据他们的Documentation ,看来通信所期望的协议是postgres而不是tcp。 尝试将您的conString更改为

'postgres://root:root@localhost:5432/dataBase'

这会将协议从tcp更改为postgres,这是数据库与之通信时所期望的。

原因可能是与postgreSQL的连接。 尝试telnet localhost 5432 如果正在侦听,请尝试通过命令行连接:

视窗:

cd C:\path\to\postgres\bin
psql.exe -U root dataBase

Linux:

psql -U root dataBase

如果连接失败,则需要验证与数据库的连接:

  1. 移植正在监听的端口(5432)
  2. 在pg_hba.conf配置
  3. 用户名和数据库名(在本例中为root和dataBase)

首先通过此命令检查mysql的连接端口

ps ax |grep mysqld

结果应该像“ mysql.sock --port = 3300”

然后转到node_modules下的mysql文件夹并打开ConnectionConfig.js文件>更改端口以更正一个“ 3300”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM