If I have a record in /etc/postgresql/9.4/main/pg_hba.conf which specifically trusts my specific user
# TYPE DATABASE USER ADDRESS METHOD
local all myuser trust
Since I'm on debian I restart postgresql like this
sudo /etc/init.d/postgresql restart
Here is my entire source file testing this out:
const pg = require('pg');
const connectionString = "postgres://myuser:mypassword#localhost/mydbname";
const client = new pg.Client(connectionString);
client.connect();
const query = client.query('SELECT * FROM USERS');
query.on('end', () => { client.end(); });
and this is the error I consistently get:
error: password authentication failed for user "myuser"
at Connection.parseE (/home/myuser/webserver/node_modules/pg/lib/connection.js:539:11)
at Connection.parseMessage (/home/myuser/webserver/node_modules/pg/lib/connection.js:366:17)
at Socket.<anonymous> (/home/myuser/webserver/node_modules/pg/lib/connection.js:105:22)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at Socket.Readable.push (_stream_readable.js:134:10)
at TCP.onread (net.js:551:20)
It's also worth noting that doing the following works:
psql -h localhost -U myuser mydb
What am I doing wrong here?
As the documentation states, local is only for UNIX socket connections, while you are establishing a TCP connection to localhost.
Use a line like this:
host all myuser 127.0.0.1/32 trust
to trust all connections from localhost using IPv4 (use the adress ::1/128 for IPv6).
Related
I am trying to fetch data from GCP cloud MySQL database using a cloud function. The cloud function is written in Nodejs. This function is making a query to fetch data from the database. I have created a Serverless VPC connector that I have attached to function and MySQL. MYSQL has private IP enabled. However I get the below error:
Error: connect ETIMEDOUT at
PoolConnection.Connection._handleConnectTimeout
(/workspace/node_modules/mysql/lib/Connection.js:409:13) at
Object.onceWrapper (events.js:519:28) at Socket.emit
(events.js:400:28) at Socket.emit (domain.js:537:15) at
Socket._onTimeout (net.js:495:8) at listOnTimeout
(internal/timers.js:557:17) at processTimers
(internal/timers.js:500:7) Error: connect ETIMEDOUT at
PoolConnection.Connection._handleConnectTimeout
(/workspace/node_modules/mysql/lib/Connection.js:409:13) at
Object.onceWrapper (events.js:519:28) at Socket.emit
(events.js:400:28) at Socket.emit (domain.js:537:15) at
Socket._onTimeout (net.js:495:8) at listOnTimeout
(internal/timers.js:557:17) at processTimers
(internal/timers.js:500:7)
Below is my Cloud function code:
const mysql = require('mysql');
const mysqlConfig = {
connectionLimit: 1,
host: '10.217.208.5',
port: '3306',
user: 'root',
password: 'tmc',
database: 'tmc'
};
let mysqlPool;
exports.helloWorld = (req, res) => {
mysqlPool = mysql.createPool(mysqlConfig);
mysqlPool.query(`SELECT * FROM tmc`, function (error, results) {
console.log(error);
console.log(results)
res.status(200).send(results);
});
};
My Cloud SQL instance
Network for private connections:
GCP function VPC connector
In my case it was a firewall issue. I allowed a particular IP and things worked. If u face a similar issue you may also try allowing 0.0.0.0/0 (Just for testing) inside SQL connections settings. 0.0.0.0/0 opens SQL to the world
I am trying to run the below program to create a mongo database using Node.js by running node app.js.
app.js
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://hostname:27017/mydb";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
console.log("Database created!");
db.close();
});
Below is the error I'm getting :-
(node:20815) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
MongoNetworkError: failed to connect to server [hostname_fqdn:27017] on first connect [Error: connect ECONNREFUSED 10.127.45.59:27017
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1134:16) {
name: 'MongoNetworkError'
}]
at Pool.<anonymous> (/root/myfolder/node_modules/mongodb/lib/core/topologies/server.js:438:11)
at Pool.emit (events.js:223:5)
at /root/myfolder/node_modules/mongodb/lib/core/connection/pool.js:562:14
at /root/myfolder/node_modules/mongodb/lib/core/connection/pool.js:995:11
at /root/myfolder/node_modules/mongodb/lib/core/connection/connect.js:32:7
at callback (/root/myfolder/node_modules/mongodb/lib/core/connection/connect.js:280:5)
at Socket.<anonymous> (/root/myfolder/node_modules/mongodb/lib/core/connection/connect.js:310:7)
at Object.onceWrapper (events.js:313:26)
at Socket.emit (events.js:223:5)
at emitErrorNT (internal/streams/destroy.js:92:8) {
name: 'MongoNetworkError'
}
The file/node_modules/package.json all are located in a CentOS Virtual Machine.
You need to start the MongoDB Service after installing.
Edited:
If you have already started service and connecting mongo via terminal, then try to remove mongo lock file (rm /var/lib/mongodb/mongod.lock) and repair mongod (mongod -–repair). Now start mongo service and see if you can connect. I had similar issue with EC2 and Compass and tried above to resolve.
I have a problem with connection to a database with mongoose on localhost.
In my server.js file I have:
var express = require('express');
var app = express(); //Create our app with express
var mongoose = require('mongoose'); //Mongoose for mongoDB
var database = require('./config/database.js'); //Load the database config
...
//Configuration =====================
mongoose.connect('database.url'); //Connect to mongoDB database
...
In my database.js file I have:
// Config/database.js
module.exports = {
url : 'mongodb://127.0.0.1:27017/test'
};
The error I get in my node.js command prompt is:
events.js:141
throw er; // Unhandled 'error' event
^
Error: failed to connect to [database.url:27017]
at null.<anonymous> (C:\*filepath*\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\server.js:540:25)
at emitThree (events.js:97:13)
at emit (events.js:175:7)
at null.<anonymous> (C:\*filepath*\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\connection_pool.js:140:15)
at emitTwo (events.js:87:13)
at emit (events.js:172:7)
at Socket.<anonymous> (C:\*filepath*\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\connection.js:478:10)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at emitErrorNT (net.js:1256:8)
When I use mongodb://127.0.0.1:27017/test in place of database.url it works just fine.
I'm currently using node.js v4.2.6 and mongodb 3.2 on Windows 10.
How can I get module.exports to pass the url to the server.js?
Remove '', because now you are trying connect to mongodb with url 'database.url', but you need use url property from database that contains right url mongodb://127.0.0.1:27017/test
mongoose.connect(database.url);
Have you tried taking it out of speech marks?
mongoose.connect('database.url');
is trying to connect to the address 'database.url'
mongoose.connect(database.url);
will get the url property of database
I have the C# app that sends the UDP packets for the specific IP and port and it works well, because I have the other app that receives those packages. Now I would like to send them so that I can display it on my webpage - I've read that the node.js will fit perfectly here.
I installed the current version of node.js under windows environment and took the following code:
var PORT = 19777;
var MULTICAST_GROUP = "224.0.0.251";
var dgram = require("dgram");
var client = dgram.createSocket("udp4");
client.on("message", function(message, rinfo) {
console.log("received: ",message,rinfo);
});
client.on("listening", function() {
console.log("listening on ",client.address());
client.setBroadcast(true);
client.setTTL(64);
client.setMulticastTTL(64);
client.setMulticastLoopback(true);
client.addMembership(MULTICAST_GROUP);
client.send(payload, 0, payload.length, PORT, MULTICAST_GROUP, function(err,bytes) {
console.log("err: "+err+" bytes: "+bytes);
// client.close();
});
});
client.on("close", function() {
console.log("closed");
});
client.on("error", function(err) {
console.log("error: ",err);
});
client.bind(19777);
And now I started sending packets on the following ip "224.0.0.251" and port 19777 from my C# app - however, after runnint the client app I've got the following error:
C:\Users\user\Desktop>node client.js
error: { [Error: bind EADDRINUSE] code: 'EADDRINUSE', errno: 'EADDRINUSE', sysc
all: 'bind' }
What am I doing wrong? And - after fixing this issue - will I be able to just see all packets in the console? Thanks.
------------- EDIT:
Following the advice of John, I modified the app so that only node.js is listening now on that port. Thanks to this I made a progress and on the output of my console I get:
C:\Users\user\Desktop>node client.js
listening on { address: '0.0.0.0', family: 'IPv4', port: 19777 }
C:\Users\user\Desktop\client.js:19
client.send(payload, 0, payload.length, PORT, MULTICAST_GROUP, function(er
^
ReferenceError: payload is not defined
at Socket.<anonymous> (C:\Users\godyckim\Desktop\client.js:19:17)
at Socket.emit (events.js:104:17)
at startListening (dgram.js:139:10)
at dgram.js:230:7
at dns.js:85:18
at process._tickCallback (node.js:355:11)
at Function.Module.runMain (module.js:503:11)
at startup (node.js:129:16)
at node.js:814:3
but still no packets are visible.. Ok, I see that I didn't define the payload variable... What I want to achieve is to receive the packets that are sent by udp and either display it in the node.js console (that would be a start for me), or transfer it further so that I can see them in the browser. In the second case - what exactly I'm doing wrong now? Thanks!
I'm running a node server using socket.io 0.9.16 and after restarting
my server recently I got the above mentioned error:
{ [Error: connect ECONNREFUSED]
stack: 'Error: connect ECONNREFUSED\n at exports._errnoException (util.js:676:11)\n at Object.afterConnect [as oncomplete] (net.js:938:19)\n --------------------\n at Protocol._enqueue (
/var/www/node_modules/mysql-activerecord/node_modules/mysql/lib/protocol/Protocol.js:110:48)\n at Protocol.handshake (/var/www/node_modules/mysql-activerecord/node_modules/mysql/lib/protocol/Protoc
ol.js:42:41)\n at Connection.connect (/var/www/node_modules/mysql-activerecord/node_modules/mysql/lib/Connection.js:98:18)\n at Connection._implyConnect (/var/www/node_modules/mysql-activerecord
/node_modules/mysql/lib/Connection.js:296:10)\n at Connection.query (/var/www/node_modules/mysql-activerecord/node_modules/mysql/lib/Connection.js:154:8)\n at get (/var/www/node_modules/mysql-ac
tiverecord/index.js:387:15)\n at Manager.<anonymous> (/var/www/mutual.js:101:71)\n at Manager.authorize (/var/www/node_modules/socket.io/lib/manager.js:910:31)\n at Manager.handleHandshake (/
var/www/node_modules/socket.io/lib/manager.js:786:8)\n at Manager.handleRequest (/var/www/node_modules/socket.io/lib/manager.js:593:12)',
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
fatal: true }
nmap localhost returns:
Starting Nmap 5.21 ( http://nmap.org ) at 2014-07-22 21:02 EDT
Nmap scan report for localhost (127.0.0.1)
Host is up (0.0000050s latency).
Hostname localhost resolves to 2 IPs. Only scanned 127.0.0.1
rDNS record for 127.0.0.1: localhost.localdomain
Not shown: 992 closed ports
PORT STATE SERVICE
22/tcp open ssh
25/tcp open smtp
53/tcp open domain
80/tcp open http
587/tcp open submission
3000/tcp open ppp
3306/tcp open mysql
9000/tcp open cslistener
netstate -an | grep "LISTEN " returns
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:3000 0.0.0.0:* LISTEN
...
iptables --list returns
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere tcp dpt:3000
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Relevant server code:
var mysql = require('mysql-activerecord'),
marked = require('marked'),
conn = new mysql.Adapter({
server: 'server-name',
username: 'user',
password: 'pass',
database: 'database'
}),
moment = require('moment');
var io = require('socket.io').listen(3000);
Relevant client code:
a=io.connect("http://server-name:3000/",{query:stuff})
Thanks in advance for the help. Let me know if you need anymore information. Sorry
if this post is a bit long.
The ECONNREFUSED has nothing to do with socket.io, if you look at the backtrace provided in the error, you can see it's coming from the mysql module. So it appears that your mysql server is not listening on server-name on port 3306 (or at least something is blocking access to it).