I have a droplet in Digital Ocean that is running MongoDB (4.0.3) and Ubuntu (18.04). I created this with their one-click formation. I followed the digital ocean tutorials to secure mongodb and to configure remote access. Everything works great if I ssh into the box and use the mongo shell. However, I am having trouble establishing a connection to the server from my laptop via nodejs/mongoose.
Can you help me figure out what is incorrect with my configuration? Or help me interpret the error messages
Here is what is working:
[terminal] - ssh into the box and use mongo shell
[node] - using tunnel-ssh I can establish a connection to the remote server
[node] - using the npm mongodb package I can create a connection to the database
However, I am unable to make a connection with mongoose using tunnel-ssh. Here is the code I am trying:
const mongoose = require('mongoose')
const tunnel = require('tunnel-ssh');
const config = {
username: 'user',
password: 'pass',
host: 'myLaptopIp',
port: 22,
dstHost: 'severRunningMongo',
dstPort: 27017, // this was open via `sudo ufw allow from myLaptopIp to any port 27017`
localHost: '127.0.0.1',
localPort: 27017
};
tunnel(config, (error, server) => {
if (error) {
console.log('SSH connection error: ' + error);
}
const url = 'mongodb://${dbuser}:${dbpass}#127.0.0.1:27017/${dbname}';
mongoose.connect(url, { useNewUrlParser: true });
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'DB connection error:'));
db.once('open', function() {
console.log('DB connection successful');
});
});
The error I am getting is
Error: connect ECONNREFUSED myLaptopIp:22
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1137:16)
Emitted 'error' event on Server instance at:
at Client.emit (events.js:311:20)
at Socket.<anonymous> (/~/node_modules/ssh2/lib/client.js:294:10)
at Socket.emit (events.js:311:20)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: 'myLaptopIp',
port: 22,
level: 'client-socket'
}
If I change the localPort to 2000 OR if I change the mongo connection url to include the mongoServerIp, I get the error:
MongoNetworkError: failed to connect to server [127.0.0.1:27017] on first connect [Error: connect ECONNREFUSED 127.0.0.1:27017
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1137:16) {
name: 'MongoNetworkError'
You know when you ask a question and then 1 minute later you think of something new to try, and that new thing completely works?
I think I don't need to use tunnel-ssh at all because I already opened the server to allow remote access from my laptop. This works for me
const mongoose = require('mongoose')
const url = 'mongodb://{dbUser}:{dbPass}#{mongoServerIp}:27017/{dbName}?authSource=admin';
mongoose.connect(url);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'DB connection error:'));
db.once('open', function() {
console.log('DB connection successful');
});
I did need to add the ?authSource=admin to the end of the url or it wouldn't work. I added my dbuser to the admin db as suggested in DO tutorial
I'm still not sure why the tunnel doesn't work, I would have expected it to still work?
Related
I received this error when I wanted to connect to redis instance created on render.com. It should be mentioned that I enabled the instance to connect servers outside render.com.
Error: connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1247:16) {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 6379
}
this is my redis config
import redis from "redis";
const client = redis.createClient({
url: process.env.REDIS_URL_EXTERNAL,
});
export default client;
and this is how I connected the client in the server.js file
(async () => {
await client.connect();
})();
I tried to connect to a Redis instance created on Render.com using the redis library in Node.js. I expected the connection to be successful, and for the Connected to Redis message to be logged to the console. However, I received the mentioned error.
I am connecting to a remote Redis using the following code. The problem connecting because it does not accept the giving IP instead points to localhost.
const redisURL = "redis://20.213.158.211:6379" // random remote IP address
const client = redis.createClient({redisURL});
client.on('connect', function(){
client.select(5);
console.log('Connected to Redis');
});
client.on("error", (err) => console.log("Redis Client Error", err));
client.connect();
It throws the following error
Redis Client Error Error: connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 6379
}
The problem is it still trying to connect to 127.0.0.1(localhost) instead of the giving IP address above.
const config = {
username: "username",
password: "password",
host: "0.0.0.0",
port: 22,
dstPort: 27017,
};
tunnel(config, function (error, server) {
if (error) {
console.log("SSH connection error: " + error);
}
console.log("SSH ok");
mongoose.connect(
"mongodb://127.0.0.1:27017/megaparking?retryWrites=true&w=majority'"
);
console.log("Connect ok");
var db = mongoose.connection;
db.on("error", console.error.bind(console, "DB connection error:"));
db.once("open", function () {
console.log("DB connection successful");
});
});
I want to connect with MongoDB installed on server, but I have the issue:
127.0.0.1:27017 is already in use cause
I was opening sever locally and I want connected with MongoDB on dedicated server, which has also 127.0.0.1 host and port 27017. What I should do with that code? Or maybe I should create a MongoDB connection string by this data?
The issue:
Error: listen EADDRINUSE: address already in use 127.0.0.1:27017
at Server.setupListenHandle [as _listen2] (net.js:1318:16)
at listenInCluster (net.js:1366:12)
at doListen (net.js:1503:7)
at processTicksAndRejections (internal/process/task_queues.js:81:21)
Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1345:8)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
code: 'EADDRINUSE',
errno: -4091,
syscall: 'listen',
address: '127.0.0.1',
port: 27017
}
[nodemon] app crashed - waiting for file changes before starting...
I am learning NodeJS in w3schools.com at this link, in MongoDB section i get error and i can't solve it.
Create a database called "mydb":
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydb";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
console.log("Database created!");
db.close();
});
Save the code above in a file called "demo_create_mongo_db.js" and run the file
C:\Users\myName>node demo_create_mongo_db.js
then i get this error
(node:6104) 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.
C:\Users\Milad\node_modules\mongodb\lib\utils.js:725
throw error;
^
MongoNetworkError: failed to connect to server [localhost:27017] on first connect [Error: connect ECONNREFUSED 127.0.0.1:27017
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1137:16) {
name: 'MongoNetworkError',
[Symbol(mongoErrorContextSymbol)]: {}
}]
at Pool.<anonymous> (C:\Users\Milad\node_modules\mongodb\lib\core\topologies\server.js:438:11)
at Pool.emit (events.js:311:20)
at C:\Users\Milad\node_modules\mongodb\lib\core\connection\pool.js:561:14
at C:\Users\Milad\node_modules\mongodb\lib\core\connection\pool.js:994:11
at C:\Users\Milad\node_modules\mongodb\lib\core\connection\connect.js:31:7
at callback (C:\Users\Milad\node_modules\mongodb\lib\core\connection\connect.js:264:5)
at Socket.<anonymous> (C:\Users\Milad\node_modules\mongodb\lib\core\connection\connect.js:294:7)
at Object.onceWrapper (events.js:418:26)
at Socket.emit (events.js:311:20)
at emitErrorNT (internal/streams/destroy.js:92:8) {
name: 'MongoNetworkError',
[Symbol(mongoErrorContextSymbol)]: {}
}
I tried the next chapter lesson but get the same error.
I am using windows10x64
First install MongoDB!
I thought Node.js had installed MongoDB on my machine.
before running node app you should first install mango on your machine
check on the below link how to download and install it:
https://www.mongodb.com/try/download/community
Trying to connect to the mongodb free database on mongodb-atlas
I've almost tried everything Checked the docs, Changed the url, Changing password and even deleted the user and created the new one but still not resolve the problem
here is myurl.js file
module.exports = {
mongoURL: "mongodb+srv://nansDB:nansDB123#nodecluster-qs6cv.mongodb.net/test?retryWrites=true&w=majority",
secret: "mystrongsecret"
}
here is my index.js file
const express = require('express');
const mongoose = require('mongoose');
const app = express();
//mongodb Configuration
const db = require('./setup/myurl').mongoURL;
//attempt to connect to database
mongoose.connect(db, { useNewUrlParser: true })
.then(()=> console.log('MongoDb Connect successfully'))
.catch(err => console.log(err));
const port = process.env.PORT || 3000;
here are the error lines I'm getting whenever I tried to run
{ MongoNetworkError: failed to connect to server [nodecluster-shard-00-02-qs6cv.mongodb.net:27017] on first connect [MongoNetworkError: connection 5 to nodecluster-shard-00-02-qs6cv.mongodb.net:27017 closed]
at Pool.<anonymous> (D:\nodejsProject\bigStack\node_modules\mongodb-core\lib\topologies\server.js:431:11)
at Pool.emit (events.js:189:13)
at connect (D:\nodejsProject\bigStack\node_modules\mongodb-core\lib\connection\pool.js:557:14)
at callback (D:\nodejsProject\bigStack\node_modules\mongodb-core\lib\connection\connect.js:109:5)
at runCommand (D:\nodejsProject\bigStack\node_modules\mongodb-core\lib\connection\connect.js:129:7)
at Connection.errorHandler (D:\nodejsProject\bigStack\node_modules\mongodb-core\lib\connection\connect.js:321:5)
at Object.onceWrapper (events.js:277:13)
at Connection.emit (events.js:189:13)
at TLSSocket.<anonymous> (D:\nodejsProject\bigStack\node_modules\mongodb-core\lib\connection\connection.js:350:12)
at Object.onceWrapper (events.js:277:13)
at TLSSocket.emit (events.js:189:13)
at _handle.close (net.js:597:12)
at TCP.done (_tls_wrap.js:388:7)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
Its solved actually my whitelisted ip was changed because of connection issue [reconnecting to wifi]. Although my suggestion is checked whether the ip you whitelisted is exists or not.