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:
ssh
into the box and use mongo
shell tunnel-ssh
I can establish a connection to the remote server 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?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.