Does MongoDB require me to manually run mongod in shell? - node.js

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

Related

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" ...

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.

No primary server available when failover happens: MongoDB, Node.js, Mongoose

I am currently facing an issue when failover happens in mongodb replica set. The app fails to reconnect to the newly elected primary server and fails to perform all subsequent write operations.
Restarting app reconnects successfully.
The failover happens instantly and a new primary is elected. However, the app fails to connect to the new primary.
mongodb version: 3.2.6
mongoose version: 4.3.4
node.js version:0.10.26
I was also facing a similar problem then I just changed
mongoose.connect(db)
to
mongoose.connect(db, {useNewUrlParser: true})
and now it is working fine
I have a primary, secondary and an arbiter set up running in three different nodes. This is how I connect using mongoose and the failover works perfectly fine.
mongoose.connect('mongodb://user:pwd#a.com:27017,b.com:27017,c.com:27017/dbName');
So, everything expect mongodb:// are variables.
I had this problem but it turned out to be that I was trying to access from a non-whitelisted IP.
mongoose.connect(url, { useNewUrlParser: true, useUnifiedTopology: true })
use like this, it will work fine.

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