Why I am able to access mongodb without running local mongo server? - node.js

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.

Related

Why is my Node.js service stopping unexpectedly under GraalVM?

I have a fairly simple Node.js service that basically just fields a few HTTP requests. This runs fine via the GraalVM node command. However, when I use node --jvm --polyglot service.js My Node service dies shortly after starting. Nothing else in the code has changed.
What is interesting is that the following code seems to kill my Node.js service
const { MongoClient } = require("mongodb")
console.log("got MongoClient")
And when I run Graal Node without --jvm --polyglot everything works fine.
If I comment out the Mongo stuff, running with --jvm --polyglot, everything works fine.
What could possibly be going on where trying to run the MongoDB Node.js driver under GraalVM could be causing problems?
It may not be that it dies, but after starting my HTTP service
const server = app.listen(port, () => console.log(`Server running... test at http://${hostname}:${port}/ping`))
it no longer accepts HTTP requests. ???
The best approach would be to raise an issue on GraalVM's repos, probably on the Graal.js one: https://github.com/oracle/graaljs. It could be a bug.
You can also debug the process and maybe that will reveal additional details of what's happening: https://www.graalvm.org/tools/chrome-debugger/

Does MongoDB require me to manually run mongod in shell?

I am creating node.js app with MongoDB database. I am using mongoose module.
mongoose
.connect('mongodb://localhost:27017/myapp', {
useNewUrlParser: true,
useUnifiedTopology: true,
})
While running code above, the connection used to establish on it's own. Also when I opened MongoDB Compass and filled the connection string it just worked.
Then I used mongod in PowerShell to test behaviour of my app when I disconnect database. I opened connection, ran the app and closed connection with ^C.
Then when I wanted to run my app like I used to before, it didn't work and now I have to always open PowerShell and start mongod before using Mongo Compass or connecting via app.
Moreover data from the "myapp" database disappeared completely.
I am just curious what happened.
Try running a mongodb Service in background.
Refer THIS
service mongod status
service mongod start
service mongod stop

Server Port over from Mac OS to Ubuntu does not work (Express + ReactJS)

I have the exact copy and setting ported to Ubuntu and for some reason I get a
'Failed to load resource: net::ERR_EMPTY_RESPONCE' via chrome DevTools.
The express server is running with PM2 and I have mongoDB running as a service. The goal was to have the server run and connect on reboot (for a store POS system).
On my Mac, it runs connect and such forth no problem.
On my Ubuntu box, the React app does launch, I just cannot seem to connect to the database. The db setting are the exact same (ports and all) from my mac to ubuntu.
I used MongoDB Compass on the ubuntu system to view the database once the service is started and it does connect, and I see all of the migrated data. When I use postman to try to get a response from my server, it just hangs and nothing happens. I'm not even sure how to diagnostic this issue.
Please inform me what information you need and I'll happily forward it.
The issue was that on my mac, I didn't have a process.env.MONGOLAB_URI setup, so it went to the testDB. But, it seems that on my Ubuntu system there is a default process.env that got created or is used.
var mongoUri = process.env.MONGOLAB_URI || 'mongodb://localhost/testDB'
mongoose.connect(mongoUri);
mongoose.Promise = global.Promise;
simply change this line to this
var mongoUri ='mongodb://localhost/testDB'
allowed it to work. Future note on migration if anyone runs into this problem!

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.

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

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.

Resources