I am trying to connect using the following code.
I am running the nodejs app locally and I cannot connect to the MongoLab DB using the MongoDb Native Driver.
After about 30sec the error returned says:
{"name":"MongoError","message":"no valid seed servers in list"}
Should I declare something else somewhere? What am I missing?
When I run it live on Heroku it connects just fine.
Also, when I run it from the terminal (shell) it connects just fine.
var MongoClient = require('mongodb').MongoClient;
var util = require('util');
var assert = require('assert');
var auth = {
user: 'root',
pass: 'blabla',
host: 'blabla.mongolab.com',
port: 63879,
name: 'heroku_blabla'
};
// Connection URL
var url = util.format('mongodb://%s:%s#%s:%d/%s',
auth.user, auth.pass, auth.host, auth.port, auth.name);
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server");
db.close();
});
I solved it, but I don't know what caused the problem.
I suspected that it was a DNS problem.
So I went to the terminal and first run:
ping ds00000.mongolab.com
(change 00000 to the port of your MongoLab DB)
to check the host.
Then I run: nc -w 3 -v ds000000.mongolab.com 000000
Finally, if those two succeed then it is not a problem with the port.
So, then I changed the host name in my code with the IP of that host name in mongoLab, and that solved the error that appeared.
Note: The IP of the mongoLab DB host appears in the response of second command in your terminal.
Related
I have a node js app. And I use Redis from Heroku Redis(with async-redis library).
Actually, I have two different Heroku accounts and two different Node.js apps hosted by Heroku. But except Redis credentials, both apps are the same code.
The interesting thing on my app I can connect to first Heroku Redis instance. But I can't connect to new Heroku Redis instance. Besides I deleted and created new instances, bu they don't work.
The error is:
Error: Redis connection to redis-123.compute.amazonaws.com:28680 failed - read ECONNRESET\n
at TCP.onStreamRead (internal/stream_base_commons.js:162:27)
My connection statement like this:
var redisPassword = 'password123';
var redisOptions = { host: 'redis-123.cloud.redislabs.com', port: '17371', auth_pass: redisPassword }
//var redisPassword = 'password123';
//var redisOptions = { host: 'redis-123.compute.amazonaws.com', port: '28680', auth_pass: redisPassword }
const client = redis.createClient(redisOptions);
client.on('connect', function () {
console.log('Redis client connected');
});
client.on('error', function (err) {
console.log('An error on Redis connection: ' + err);
});
As I can see there is the only thing that different on Heroku Redis instances. My first Redis instance hosts at cloud.redislabs.com but the second instance(that i can't connect) hosts at compute.amazonaws.com.
Any help will be much appreciated.
I encountered this situation and it turned out the with "Heroku Redis" connecting via TLS worked (the url that starts with rediss) once I adjusted my client code to connect following the example provided in the Heroku redis docs:
https://devcenter.heroku.com/articles/connecting-heroku-redis#ioredis-module
const Redis = require("ioredis");
const client = new Redis(process.env.REDIS_URL, {
tls: {
rejectUnauthorized: false
}
});
Where process.env.REDIS_URL is rediss://<details>
I couldn't find the root problem. But after comment of Chris, I checked again Heroku Redis addons I used.
Heroku Redis gives me an instance from amazonaws.com, and Redis Enterprise Cloud gives me an instance from redislabs.com. When I added and used Redis Enterprise Cloud, I could connect to it.
But Heroku Redis's connection problem still is a secret for me.
I have access to a remote postgres DB from pgAdmin4 and I also could access from nodejs using a Mac. Right now I'm using the same code to access the DB in Windows. The code for my connection is the following:
const { Client } = require('pg'); //Importing the Postgres package
const hosts= require('../hosts'); //Using the file containig all hosts
const connectionData = { //Begin creating the connection settings object
host: hosts.DBHost, //DB host
port: hosts.DBPort, //DB hosts port
database: hosts.DB, //DB
user: hosts.DBUser, //DB user
password: hosts.DBPassword, //DB user password
}
My test is the following:
var client = new Client(connectionData); //New client instance using the above connection settings
client.connect(); //Open the connection to the database()
sql = "select * from myTable";
client.query(sql)
.then(response => {
console.log ({"data": response}); //This isn't shown
})
.catch(err => {
console.log({"error": err}); //This isn't shown neither
})
No error, no exception, the DB server doesn't respond!
Why isn't the server responding?
I suspect that you have the same problem like in this other post. Since it is not a 100% duplicate I will post this again:
There is a known issue in the pg module and NodeJS 14.
The proposed solution is to make sure you have pg>=8.0.3 installed.
This can be done by updating pg in the dependencies.
Also make sure, that any other library depending on the pg module, is also up to date and has the latest pg version.
If this is not possible for any reason - using Node 12 should also work.
It it possible to use kerberos authentication with mongodb without specifying password when running the node app in windows environment?
I can successfully log in to my mongo server from command line:
mongo.exe --host xxxx.yyyy.zzzz.com --port 27017 --username FLast#YYYY.ZZZZ.COM --authenticationMechanism=GSSAPI --authenticationDatabase=$external
But I can't do the same thing in the code:
const MongoClient = require('mongodb').MongoClient;
const f = require('util').format;
const assert = require('assert');
const server = 'xxxx.yyyy.zzzz.com';
const principal = 'FLast#YYYY.ZZZZ.COM';
const urlEncodedPrincipal = encodeURIComponent(principal);
// Let's write the actual connection code
MongoClient.connect(f("mongodb://%s#%s/?authMechanism=GSSAPI&gssapiServiceName=mongodb", urlEncodedPrincipal, server), function (err, client) {
assert.equal(null, err);
client.close();
});
I get back an error: AssertionError [ERR_ASSERTION]: null == 'Error: SEC_E_LOGON_DENIED The logon failed.'
What caught my eye is that 10Gen's own tests all specify password when running in windows environment (see second half of https://github.com/mongodb/node-mongodb-native/blob/master/test/functional/kerberos_tests.js)
So is it possible to authenticate without providing the password when running in windows?
My requirement is to connect to telnet server from client side and run commands on server machine using nodejs.
Here is the code i am using:
const net = require("net");
const cp = require("child_process");
net.connect({host: 192.168.192.136, port:23}, function() {
console.log("connected");
cp.exec('pwd', (err, stdout, stderr) => {
if (err) {
console.error(err);
return;
}
console.log(stdout);
});
});
With this i am able to connect to server but when i run command using cp.exec it is running on local machine not on connected server.
1) how to run that command on server after connection?
2) Why connection is established to server without username or password. although when i try to connect to it through terminal it asks for username and password.
I also tried with some node js modules from npmjs but didn't get success.
Any kind of help will be appreciated. Thanks.
Use remote-exce module to do the same.
Here link to documentation:-
https://www.npmjs.com/package/remote-exec
Then Use telnet client module
Link to documentation
https://www.npmjs.com/package/telnet-client
On Nodejs v15 I don't think you need exec for that. I've managed to establish a telnet connection via the following method:
const net = require("net");
/*
* establish new client connection to the server
*/
let client = net.connect({
host: "192.168.192.136",
port: 23,
}, ()=> {
console.log("connected");
client.write("TELNET COMMAND HERE", ()=>{
console.log("Command sent!")
})
});
Tutorial reference: https://nodejs.org/api/net.html#net_net_createconnection_options_connectlistener
I just added MongoDB to the dependencies of my Node.js project created with the npm init command. My index.js code is:
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
// Connection URL
var url = 'mongodb://localhost:27017/mydatabase';
// Use connect method to connect to the server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected successfully to server");
db.close();
});
But when I execute the code it throws the following error:
AssertionError: null == { MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
I really don't know how to fix it. I followed some guides that reported the same error but I couldn't fix it.
Should I poen a port on my modem? Should I replace "localhost" with my IP? Should I do anything else?
Please help me!
UPDATE:
I installed a MongoDB server on my Android device and replacing the url variable with mongodb://ANDROID-DEVICE-LOCAL-IP:27017/mydatabase it now works.
How can I accomplish it placing the database on my computer? Is the firewall blocking incoming connections? Is this why it works on Android but not on Windows?
I fixed the issue!
I ran mongod -dbpath "MY-PATH" in the cmd and now it works.
The error occurred because nothing was listening on 27017 port. Now the mongod program is listening for connections on that port and so the connection from my project is no more refused.
Make a configuration file like config.js
module.exports = {
'secretKey': '12345-67890-09876-54321',
'mongoUrl' : 'mongodb://localhost:27017/cubs'
}
And require that file in your server code
var config = require('./config');
var mongoose = require('mongoose');
mongoose.connect(config.mongoUrl);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
console.log("Connected correctly to server");
});