have been trying to connect my docker node.js app to a mongodb service to no avail.
I've created a was liberty bridging application ($BRIDGE_APP) with no war or code that is bound to a mongodb service. It is running with good status.
Have to say that the same code is running correctly in my local docker container. I am using mongoose to connect to mongo.The only difference in the code is the way of resolving the mongo connection string:
var DB_CONNECT_STRING = 'mongodb://app:password#127.0.0.1:27017/appname';
if(custom.areWeOnBluemix() && custom.doWeHaveServices())
DB_CONNECT_STRING = custom.getMongoConnectString();
...
console.log('going to connect to mongo#: ' + DB_CONNECT_STRING);
var db = mongoose.createConnection(DB_CONNECT_STRING);
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function (callback) {
console.log('... db open !!!');
});
I push my image to bluemix with no issues:
ice --local push $REGISTRY/$ORG/$CONTAINER_NAME
I then check the env vars:
cf env $BRIDGE_APP
System-Provided:
{
"VCAP_SERVICES": {
"mongodb-2.4": [
{
"credentials": {.....
and then I run my container and bind an ip:
ice run --bind $BRIDGE_APP --name $CONTAINER_NAME -p $PORT $REGISTRY/$ORG/$CONTAINER_NAME:latest
sleep 12
ice ip bind $IP $CONTAINER_NAME
...this is almost completely by the book, but for some reason when I check the logs I'm always getting:
ice logs $CONTAINER_NAME
...
going to connect to mongo#: mongodb://c61deb58-45ea-41....
Example app listening at http://0.0.0.0:8080
connection error: { [MongoError: connect ENETUNREACH] name: 'MongoError', message: 'connect ENETUNREACH' }
I have also tried with mongolab service with no success.
Has anybody somehow eventually tried this type of setup that can provide me some additional clue of what's missing here?
thanking you in advance
It has been my experience that networking is not reliable in IBM Containers for about 5 seconds at startup. Try adding a "sleep 10" to your CMD or ENTRYPOINT. Or set it up to retry for X seconds before giving up.
Once the networking comes up it has been reliable for me. But the first few seconds of a containers life have had troubles with DNS, binding, and outgoing traffic.
I gave a similar answer to a similar question recently. Perhaps your problem is the same as the other poster's.
Related
I am trying to connect to mongodb but getting below error could you please help
var mongo = require('mongodb').MongoClient;
mongo.connect('mongodb://usernamexyz:passwordxyz#hostmxy-mw-e6-u1238.nam.nsroot.net:47017/sampleDB?replicaSet=NAME_2436&readPreference=primary&authSource=admin&w=1', { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log("Mongodb connected"))
.catch(err => console.log(err));
And the error i am getting as below
MongoServerSelectionError: connection <monitor> to 155.30.360.129:37017 closed
at Timeout._onTimeout (C:\Fintech\NodeFirstApp\node_modules\mongodb\lib\core\sdam\topology.js:448:30)
at listOnTimeout (internal/timers.js:531:17)
at processTimers (internal/timers.js:475:7) {
name: 'MongoServerSelectionError',
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
setName: null,
maxSetVersion: null,
maxElectionId: null,
servers: Map {
'hostmxy-mw-e6-u1238.nam.nsroot.net:47017' => [ServerDescription]
},
stale: false,
compatible: true,
compatibilityError: null,
logicalSessionTimeoutMinutes: null,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: null
},
[Symbol(mongoErrorContextSymbol)]: {}
}
Go to Mongodb where you created a cluster and under "ipwhitelist" click on the edit IP address button and then choose "Add current IP address"
There was a firewall issue for me, and here are the steps i followed:
Search for Windows Firewall with Advanced Security
Select Outbound Rules
Select New Rule
Select Port in Rule Type, Click next
Select TCP and Specific remote ports and write in there 27015-27017
Save the rule
This worked for me :)
I setup replica set on mongod server in order to have oplog facility and use it to sync database programmatically with golang client.
It was working fine with local windows server.
mongod.exe --dbpath $dpath --bind_ip 0.0.0.0 --port $port --replSet rs0
mongo.exe' -port $port --eval 'rs.initiate()'
When I used docker it showed me that ReplicaSetNoPrimary Error.
docker run --name mongo21 -d -p 27021:27017 mongo --bind_ip_all --replSet rs0
docker container exec mongo21 mongosh --eval 'rs.initiate()' 127.0.0.1
I noticed that perl client as well as Robo3 T, mongosh connected to the server localhost:27021 without problem.
mongosh shows which mongo uri it uses:
mongodb://127.0.0.1:27017/test?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.1.9
I found I can specify client option before connect:
clientOpts := options.Client().ApplyURI(uri).SetDirect(true)
client, err := mongo.Connect(ctx, clientOpts)
That option fixed golang client connection issue.
Some MongoDB implementations as a service such as IBM Cloud, GCP or AWS might need a certificate to be able to connect. That was my case. The problem was resolved when I added a certificate to the docker file and then passed the parameters as options
let options = {
tls: true,
tlsCAFile: `/path/to/cert`,
useUnifiedTopology: true
};
// connects to a MongoDB database
MongoClient.connect(connectionString, options)
Reference https://cloud.ibm.com/docs/databases-for-mongodb?topic=databases-for-mongodb-mongodb-external-app
I was also getting the same error while connecting to MongoDB atlas.
You have to do following things.
1. allow outbound port 27015-27017 in firewall in your system.
2. update the latest MongoDB and NodeJs.
3. whitelist your system IP in MongoDB atlas cluster under network access.
It worked for me. I hope it works for you also.
Updating "Network Access" did not work for me. What worked for me was to update "Database Access" by "Autogenerate Secure Password" and make sure to click "Update User" to save new password!
MongoDB Atlas > Database Access > complete 3 steps in image
I had the same issue with this. It was fixed when I added the current IP address to IP whitelist inside of MongoDB.
I was using Atlas Mongo Cluster and using 'mongomirror' I am trying to push the data from one replicaset cluster to another. In the process, I was getting this error..
After ot of research, I understood that, the name of cluster visible in UI should not be used.. instead you need to run the below command
rs.status()
This will give a JSON output which has the RS name and respective primary and secondary node endpoints you need to use.
In my case, for abc-temp-shard-cluster, the cluster name I got by running the above command was something like below
atlas-12avca-shard-0/atlas-12avca-shard-00-00.1aqer5.mongodb.net:27017,atlas-12avca-shard-00-01.1aqer5.mongodb.net:27017,atlas-12avca-shard-00-02.1aqer5.mongodb.net:27017
try to enable ipv6 support at mongo server using --ipv6 flag
mongod --dbpath="D:/data_path" -replSet rs0 --bind_ip 0.0.0.0,127.0.0.1,localhost,::1 --ipv6
Is it possible to connect to a docker container running a MongoDB image from an external nodejs application running locally? I've tried connecting via localhost:27017. Here's the docker compose file I'm using:
version: '3'
services:
mongodb:
image: 'bitnami/mongodb:3.6.8'
ports:
- "27017:27017"
environment:
- MONGODB_ROOT_PASSWORD=$MONGODB_ROOT_PASSWORD
- MONGODB_USERNAME=$MONGODB_USERNAME
- MONGODB_PASSWORD=$MONGODB_PASSWORD
- MONGODB_DATABASE=$MONGODB_DATABASE
volumes:
- /data/db:/bitnami
I try connecting to it with the following url with no luck:
mongodb://${process.env.MONGODB_USERNAME}:${process.env.MONGODB_PASSWORD}#localhost:27017
EDIT: Connecting via mongodb://localhost:27017 works, but the authentication url errors out. I printed out the result of this string and there's nothing particularly wrong with it. I verified that the username and password match the users inside mongo in the docker container.
app.listen(port, () => {
console.log(`Example app listening on port ${port}!`);
const url = (() => {
if(process.env.MONGODB_USERNAME && process.env.MONGODB_PASSWORD) {
return `mongodb://${process.env.MONGODB_USERNAME}:${process.env.MONGODB_PASSWORD}#localhost:27017/`;
}
console.log('could not find environment vars for mongodb');
})();
MongoClient.connect(url, (err, client) => {
if(err) {
console.log('DB connection error');
} else {
console.log("Connected successfully to server");
client.close();
}
});
});
If the external nodejs application is also running in a docker container then you need to link the containers. Here is an example of a docker run cmd that links containers. I added environment variables to illustrate what host name and port you would use from inside the container.
docker run -d -it -e DEST_PORT=27017 -e DEST_HOST='mongodb' --link mongodb external-application:latest
It's important to always check the result of docker logs <container-name> --tail 25 -f. From my point of view, I think it is an issue related to permissions on this directory '/bitnami/mongodb'. Check out sameersbn comment how to fix this permission issue.
I'll assume it's the compose specification then. Try the following configuration
environment:
MONGODB_ROOT_PASSWORD:$MONGODB_ROOT_PASSWORD
MONGODB_USERNAME:$MONGODB_USERNAME
MONGODB_PASSWORD:$MONGODB_PASSWORD
MONGODB_DATABASE:$MONGODB_DATABASE
volumes:
- '/data/db:/data/db'
The issue turned out to be that I had changed the password in MONGODB_PASSWORD (it had an # in it so I thought it would have interfered with the string parsing, so I consequently changed it). The problem is, when the container restarts it references the same volume (as it should), so the users were never updated and as a result I was logging in with the wrong credentials.
This issue is totally driving me insane. I spent months with this, trying to make a SIMPLE NODE APP WORK. I finally managed to make an APP work in a nice server (Heroku) and with mysql. Problem? The server only accepts postgres. And this is my nightmare. I just cannot make it work. Searched dozens of webs and problems, all of them with the same error log as me... but I just cannot figure what to do. I'm totally idiot at configuring things, I cannot even start programming my app.
Error: connect ECONNREFUSED 127.0.0.1:5432
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at TCPConnectWrap.afteeConnect [as oncomplete] (net.js:1198:14)
My start of server.js
const pg = require('pg');
const connectionString = process.env.DATABASE_URL || 'postgres://myrole:12345#localhost:5432/mydb';
And here the error.
var pool = new pg.Pool();
pool.connect().then(client => {
It crashes right at connection.
I did everything I searched for. I created "myrole" login role with all permission, password "12345", to connect to "mydb" database. I opened "pgAdmin4" application. Connected to "PostgreSQL 10" and "mydb". I saw that the first one connects to port 3000. I tried port 3000 in the connection string. I searched for the service at Windows. It's running. I JUST DID EVERYTHING and nothing works... I installed and made MySQL database to run in local in just 2 hours. But Heroku doesn't accept MySQL and I don't want to put any credit card. What's happening here?
I was having the same issue and I'll put my situation here and hopefully help someone else.
I was testing some AWS Lambda functions and since the code runs in a container and the container has its own localhost so my postgres connection was failing because there is no postgres server running on the container. Remember that your machine's localhost is not the same as the container's one, if your app is running inside a container the instead of localhost use your machine IP.
in your case, you shuld include connectionString inside new Pool() as property of Object
var pool = new pg.Pool({connectionString}); pool.connect().then()
or here is a more detailed version
let c = {
host: 'localhost',
port: 5432,
user: 'user',
password: 'password',
database: 'mydb',
options: `application_name=${app}&application_cmd=${cmd}`
};
const connection_string = {connectionString : `postgresql://${c.user}:${c.password}#${c.host}:${c.port}/${c.database}?${c.options}`};
const pool = new Pool(connection_string);
more details can be found here node-postgres.com
I have that code:
var express = require('express'),
stylus = require('stylus'),
logger = require('morgan'),
bodyParser = require('body-parser'),
mongoose = require('mongoose');
var env = process.env.NODE_ENV = process.env.NODE_ENV || 'development';
var app = express();
function compile(str, path){
return stylus(str).set('filename', path);
}
app.set('views', __dirname + '/server/views');
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(stylus.middleware(
{
src: __dirname + '/public',
compile: compile
}
));
app.use(express.static(__dirname + '/public'));
mongoose.connect('mongodb://localhost/multivision');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error..'));
db.once('open', function callback(){
console.log('multivision db opened');
});
app.get('/partials/:partialPath', function(req, res){
res.render('partials/' + req.params.partialPath);
});
app.get('*', function(req, res) {
res.render('index');
});
var port = 3131;
app.listen(port);
console.log('Listening on port ' + port + '...');
but when I'm trying nodemon server.js it throws an error:
connection error.. { [MongoError: connect ECONNREFUSED
127.0.0.1:27017] name: 'MongoError' message: 'connect ECONNREFUSED 127.0.0.1:27017' }
how can I improve that? I've already installed mongoose using npm install mongoose --save in my directory
Yeah there are dozens question like this but none of these helped me.. I'm new at nodejs and probably missing something
I was also facing the same issue, when I was executing node server on my project directory. For me the MongoDB service was not started, that makes this issue.
So I had to run services.msc and activated the service.
After that I was able to run my command.
D:\SVenu\MyApp>node server
Saving User
App is listening on port: 3000
Already Exist
Already Exist
Already Exist
Already Exist
Done save
run services.msc and activate the Mongodb service.
Now Mongodb will connect
the status of the Mongodb indicated as Running
Your mongodb service is probably down.
Run sudo service mongod start to start the daemon process
I was having the same problem, and found that it was a mongod issue.(I am running Ubuntu 16.04). Looking through the errors there was a directory missing. After adding the directory, I needed to change permissions, and finally, set the mongod service to start at boot.
$ sudo mkdir -p /data/db
$ sudo chown -R $USER /data/db
$ sudo systemctl enable mongod.service
I know this issue is old, but i came across a similar issue and the above solutions did not work for me, I'm using Ubuntu 20.04 LTS.
What i did to make it work was just running mongo service using this command:
$ mongod
Then everything worked fine
If you're in windows and you had this issue,
just go the installer exe app for the mongodb
and click "Repair"
this works for me
try this:
mongod.exe --dbpath c:\data\db
c:\data\db is the place where you put your db.
and when you see something like this :
2016-08-18T10:22:31.020+0800 I CONTROL [main] Hotfix KB2731284 or later update is not installed, will zero-out data files
2016-08-18T10:22:31.022+0800 I CONTROL [initandlisten] MongoDB starting : pid=4356 port=27017 dbpath=c:\data\db 64-bit host=sevencai-PC0
2016-08-18T10:22:31.022+0800 I CONTROL [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2016-08-18T10:22:31.023+0800 I CONTROL [initandlisten] db version v3.2.8
2016-08-18T10:22:31.023+0800 I CONTROL [initandlisten] git version: ed70e33130c977bda0024c125b56d159573dbaf0
......
and then node yourserver.js
maybe everything will be fine!
Follow as below:
=> run services.msc and start mondodb server
and save the file in node project, you can see the server connected!!
In Most of the case you will get the error bcz the service MongoDB Database Server (MongoDB) Might be stopped.
Just start the service to be connected with DB
it's doesn't work as localhost but IP address works 127.0.0.1 and solves this problem:
// .connect("mongodb://localhost:27017/lofydb")
const mongoose = require("mongoose");
mongoose.connect("mongodb://127.0.0.1:27017/lofydb")
// .connect("mongodb://127.0.0.1:27017")
.then(() => {
console.log("Connected to Database");
})
.catch((err) => {
console.log("Not Connected to Database ERROR! ", err);
});
please check the MongoDB service is active and running.
Refer the running services
If it is running and available on MongoDB compass or on MongoDB shell and if you are working with node version >17.0.0 or so it will give error.
So plz change to the stable version of node.
Make sure MongdoDB is running
To run MongoDB as a macOS service, run:
brew services start mongodb-community#5.0
And when you see:
==> Successfully started mongodb-community (label:
homebrew.mxcl.mongodb-commu
OR type on command line, to check the mongo service.
mongod
After run service, You can easily connect with mongdoDB localhost or remote connection.
I had the same issue and it had to do with the fact that my MongoDB service wasn't running locally. Make sure you followed all the correct installation steps for whatever OS you are trying to run MongoDB service here: https://www.mongodb.com/docs/manual/administration/install-community/
Then follow the respective guide for the running the service under "Run MongoDB Community Edition". So for example, for macOS you would do
brew services start mongodb-community#6.0
if you wanted to run as a macOS service, or
mongod --config /opt/homebrew/etc/mongod.conf --fork
if you wanted to run manually as a background service on Apple M1 processor. The same page I shared above also has commands you can run for verifying if your MongoDB process is running.
Step 1: open conf file to edit.
sudo vim /etc/mongod.conf
Step 2: find port and change it.
net: port: 27017 -> default port before any change
After changing port, my issue solved :
Here is my code:
var express = require("express"),
app = express(),
server = require("http").createServer(app),
io = require("socket.io").listen(server),
redis = require("redis"),
env = {PORT: process.env.PORT || 8080, IP: process.env.IP || "localhost"};
client = redis.createClient(env.PORT , env.IP);
client.on("error", function(err) {
console.log(err);
});
server.listen(env.PORT);
console.log("Server started # " + env.IP + ":" + env.PORT);
After trying to run, I received the followings on the console:
Running Node Process
Your code is running at 'http://modified.address.c9.io'.
Important: use 'process.env.PORT' as the port and 'process.env.IP' as the host in your scripts!
info: socket.io started
Server started # modified.ip.address.1:8080
[Error: Auth error: undefined]
I tried establishing the connection, and it connects to the IP and PORT perfectly. However, the error [Error: Auth error: undefined] appears and stops there. I Googled the error, the supports from the IDE I used..., and surprisingly, there are only 7 links to my problems. So I think it may be a hole in my knowledge or it is not really a problem yet a thing I don't know to work it out. All I could pull out from those Google results were (I was not sure) I need to use client.auth(pass) right after creating it. But where should I find the password? When I installed it npm install redis I didn't configure anything and wasn't told to set password whatsoever. So I reach the impasse.
I use Cloud9 IDE (c9.io), and the modules used as shown in the code above.
----With best regards,
----Tim.
I've found out what was wrong.
I did install Redis, but that is a Redis library that acts like a bridge between Redis driver and NodeJS. On Cloud9, I have to manually install Redis, too.
So it would take 2 commands to actually install Redis:
Install the Redis Driver on Cloud9
nada-nix install redis
Install Redis library for NodeJS
npm install redis
Thanks for anyone who was trying to help me.
You can run the redis-server using your own config file.You can create your own config like below.
//port and ip of ur redis server
port 6371
bind 127.0.0.1
//password for this server
requirepass ucanmentionurpwd
//storing snapshots of the data
save 60 1
dbfilename dump.rdb
dir /tmp/db
//starting redis server
redis-server //ur config file location
See this link for redis configuration
https://raw.github.com/antirez/redis/2.6/redis.conf
If you mention requirepass with your password means only you need to do
client.auth('urPwd');
Otherwise no need to call the client.auth method.