AWS - Node app won't connect to running mongo instance - node.js

I installed mongo on my elastic beanstalk node.js app and started the mongo daemon process. I'm not quite sure how to connect to the database though. On my local node app, I'm able to connect with these credentials:
module.exports = {
'url' : 'mongodb://127.0.0.1:27017/test'
}
I'm assuming that it doesn't connect because I need a user, password, and to create a database to connect to, but I'm not sure how to go about doing that on the remote database. I'm also finding resources on setting mongo up on t1.micro to be very scarce, so there's not much help there.

I didn't realize that I had to start up the mongo processes myself. Run mongod.

Related

NodeJS MongoDB API in Docker Container can't connect to Database running on Host

I have a MongoDB Database set up on my host server and a docker container running on that host with a NodeJS application. In that application, I try to connect to the Database on the host but the request always times out.
I set the network to "host" and used the adress "host.docker.internal". I also tried installing mongodb-shell and it actually works! So basically I can connect to the database with the shell in the container but not with the MongoDB NodeJS API.
Code of the NodeJS application:
const mongodb = require('mongodb');
mongodb.MongoClient.connect("mongodb://host.docker.internal:27017/");
using mongo mongodb://host.docker.internal:27017/ in the shell works and I connect correctly.
I finally found out the issue.
I had to bind it to the correct IPs / in my config I used the argument --bind_ip_all.
Thanks a lot for the help!

Can't connect to mongodb container when i add path to a database

I’m running mongoDB as a docker container locally, and I initiate a database creation when starting the container. But I can’t seem to connect to the database from my nodejs application when I add path to the dB. For example i can connect when I use “mongoDB://admin:password#localhost:27017” but not when I use “mongoDB://admin:password#localhost:27017/myDB”.
I'm running the container with docker-compose. Also when I log the output of running the mongo container I see a failure message that says:
"result":"UserNotFound: Could not find user \"admin\" for db \"journalDB\""}}
JournalDB is the name of the db i want to connect to, and i want to be able to connect to it from my nodejs application like so:
mongoose
.connect("mongoDB://admin:password#localhost:27017/journalDB", {
useNewUrlParser: true,
useUnifiedTopology: true,
})
I have read the answer to a similar question here and this documentation , but I feel they have only explained the possible problem, but have not helped me fix it.
I would really appreciate it if there's a straightforward solution.
Maybe try
.connect("mongoDB://admin:password#localhost:27017/journalDB?authSource=admin" ...

Bitnami MEAN stack can't connect to local mongoDb

I am trying to connect to my local mongodb with my node express app hosted on litghtsail MEAN bitnami.
The connection string im using with mongoosse client look like this: mongodb://app:sang#127.0.0.1:27017/prisedesang
Im able to connect whit that user remotely with a SSH tunnel. Im able to connect with that same user localy using mongo cli.
However when I use the app I get ERR_CONNECTION_REFUSED when I try to connect to the db hosted on the same server... Any suggestion?
Can you try using MongooseJS? You can connect your application with MongoDB using MongooseJS, an object modeling driver for Node.js. It is already installed in the MEAN stack so you only have to add the following lines to your app.js file:
var Mongoose = require('mongoose');
var db = Mongoose.createConnection('mongodb://USER:PASSWORD#localhost/DATABASE');

Why I am able to access mongodb without running local mongo server?

I am beginning with server side programming by following a online course. As per my understanding, we use mongoose.connect(url) to connect to mongodb where url can be localhost url on which mongodb server is running.
But recently I forgot to run the local mongodb server using mongod. When I ran my node app with following code, it worked just fine :
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect(process.env.MONGO_URL || 'mongodb://localhost:27017/TodoApp');
const {Todo} = require('./../server/models/todos');
const {ObjectID} = require('mongodb');
Todo.remove({}).then((result)=>{
console.log(result);
});
It worked fine with no error even though mongodb server was not running. When I checked the local database using Robo-3T tool, the documents were removed. I have verified this again.
I am unable to understand why my node app was able to connect to mongodb://localhost:27017/TodoApp even though mongodb server was not running on localhost:27017. Also, why Robo-3T tool was able to connect to this localhost: 27017 if server was not running.
Thanks.
The mongodb server was already running. Looks like the server does not shuts down on closing the terminal. It still runs in background. We have to close it using Ctrl+C.
More info: mongodb doesn't stop when I close terminal
Thanks #Sergio Tulentsev.
The reason is simple, the process is running in the background. If you search for 'services' - (in windows), scroll down to check for MongoDb, you will see that the process is running (Started). If set to Automatic, it starts up even after every restart.
Another reason is MongoDB server runs as a service in the background. Even if you restart your computer, it may be running.
I recognized at my win 10, MongoDB server is automatically started. So, you don't need to run mongod on your local.
At win 10 you can check it by:
type services in the search box;
open Services window;
Find MongoDB Server in the list;
Check Startup Type.
Probably your startup type is Automatic, you can change MongoDB Server startup type here.
It occurs when you don't stop the server explicitly and close your terminal.The server is running in background.You can see that in your task manager as well. Press Ctrl+C to quit and it will stop your server.

Link mongoDB instance database to meteor local

I've recently setup an ec-2 instance to deploy a meteor application on AWS. My app works correctly with mongoDB database.
Now I want to connect my meteor project's database to the EC2 database and then I've tried some command lines :
start my meteor project with a new mongo url with the following command line : "MONGO_URL="mongodb://username:password#xx.xx.xxx.xxx:27017/Tasks" meteor" but it returns the following screen
connect to mongo with the other following command line :
"meteor mongo --url xx.xx.xxx.xxx:27017" but it returns a timeout. I thought it was due to the lack of username and password however when I add this option meteor does not assume the command.
connect to mongo in my js collection file with
"export const Tasks = mongoose.connect("mongodb://username:password#xx.xx.xxx.xxx:27017/Tasks").connection;" but it returns "Error: connect ECONNREFUSED" (second screen)
Is there any tips to manage to establish database connection from local meteor ?
Thank you for answer,
Martin
If the database and the Meteor processes are in the same machine, you can call it from "localhost".
MONGO_URL="mongodb://user:password#localhost/Tasks"
If you really want to use the IP + Port approach, you have to ensure that it's accessible from the EC2 Security Groups you're using.
The same applies for connecting manually in Mongoose but you should't be using Mongoose with Meteor, it has it's own way of handling collections. If you need something more powerful, or a way to extend it yourself checkout the Meteor Simple Schema project.

Resources