Mongodb doesn't connect to localhost when using node.js+ mongoose - node.js

I am making an app on Node.js and trying to connect to MongoDB via mongoose. When I run it, in cmd it shows "connected to MongoDB database", but this is what it shows when I run mongo. It doesn't say connected
Mongo connection status
Also, when I try to connect MongoDB compass to localhost:27017, it shows the error " read ECONNRESET".
So clearly, for some reason, my MongoDB is not connected to the localhost. Can someone help me out? here is my mongoose connection code in app.js.
App.js Mongoose connection code

Can you check your mongod.conf file to see the port the server is set to run on.If on linux and running as a service use systemctl status mongod to ensure the process is running. Hope it helps.

Maybe you should run the command- " mongod " in another window and check.

If it's not connecting, simply go in that path:
C:\Program Files\MongoDB\Server\5.0\bin
and click shift + right, open window PowerShell or any other command prompt, and then type mongod like that:
PS C:\Program Files\MongoDB\Server\5.0\bin> mongod
and connect to it again.

Just followed these simple steps:
Type here to search -- Services -- Click on that
Find -- MongoDB Server(MongoDB) -- Click on that
On the left site of window You will see -- Start-- link. Click on that,
and your problem is solved.

Related

how to restart mongodb in terminal?

I installed mongodb in VS code by npm install mongodb. And again I downloaded mongodb from official website and installed it as a windows service since I don't know if installing in vscode with npm is enough. It worked for a while.
and now it doesn't work as my database connection with mongoose fails and nothing is running in 27017 port in localhost. Seems like server stopped.
how to restart mongodb server? what's the CLI/terminal command etc for that?
On debian/ubuntu
sudo service mongod restart
Go to services via start button and check for the MongoDB services shown in below pic. And rest steps you can follow which #apoorva has mentioned.
Open a mongo shell and execute,
use admin
db.shutdownServer()
You may start again with mongod.
Learn more here, https://docs.mongodb.com/manual/tutorial/manage-mongodb-processes/#StartingandStoppingMongo-SendingshutdownServer%28%29messagefromthemongoshell
If You Installed MongoDB as a Windows Service
The MongoDB service is started upon successful installation.(please check on the below path)
To begin using MongoDB, connect a mongo.exe shell to the running MongoDB instance. Either:
From Windows Explorer/File Explorer, go to
C:\Program Files\MongoDB\Server\4.4\bin\ directory and double-click on mongo.exe.
Or, open a Command Interpreter with Administrative privileges and run:
"C:\Program Files\MongoDB\Server\4.4\bin\mongo.exe"
for more details check. : https://treehouse.github.io/installation-guides/windows/mongo-windows.html
Also, you first need to start the mongodb daemon for your connections to listen then you should connect using mongoose.

Starting MongoDB Replica Set

So, I was making an app that needs to use MongoDB transactions. But the Mongoose documentation told me that "MongoDB currently only supports transactions on replica sets, not standalone servers." So I thought I basically need to switch my Standalone MongoDB instance to Replica Set (whatever that means).
The MongoDB documentation gave me the instruction of how to do this with a few steps:
Shutdown the Standalone MongoDB instance
Restart the instance using the --replSet option
mongod --port 27017 --dbpath /var/lib/mongodb --replSet rs0 --bind_ip localhost
Connect the Mongo Shell
Call rs.initiate() inside the Shell
I'm stuck at step 2. All I know, when you want to start using MongoDB, you have to start its daemon first, using sudo systemctl start mongod, and then start using it by connecting your app. but that step told me to use mongod command to start the Mongod daemon, instead of systemctl. I tried the command but got the following error:
DBException in initAndListen, terminating","attr":{"error":"IllegalOperation: Attempted to create a lock file on a read-only directory: /var/lib/mongodb"}}
At first, I thought it was some kind of a privilege issue, so I ran it again with sudo but then it ended up destroying my entire database and prevented me from starting the MongoDB the "normal way" with giving me errors that I cannot remember.
I just reinstalled the whole MongoDB to get it back to work fine. Now I'm at the same place as yesterday, unable to convert to replica set, only now my entire database is gone. What do I do to enable it?
When running the mongod as a service, use /etc/monogd.conf to set the configuration. Note that the location or name of this file might have been changed in the mongod.service file in your system.
See replication options for how to set that in the file.

Stop mongoDB using node.js [duplicate]

I'm using the nginx/PHP/MongoDB stack and trying to set up my development environment on Windows by creating two batch files.
start.bat
cd C:\www\nginx
start nginx
cd C:\www\php
start php-cgi.exe -b 127.0.0.1:9000
cd C:\www\mongodb
start mongod
stop.bat
cd C:\www\nginx
nginx -s quit
cd C:\www\php
taskkill /F /IM php-cgi.exe
cd C:\www\mongodb
mongo --eval "use admin; db.shutdownServer(); quit" # this doesn't work
mongo --eval stop_mongod.js # this doesn't work either
Using taskkill to stop mongod isn't an option, since that may lead to data corruption.
Any suggestions?
I'm not sure that's a proper way to do, sending a kill could probably cause damage to your mongo server, and you'll need to repair your database after in case of crash.
Maybe you already solved this question but here is what I do :
mongo admin --eval "db.shutdownServer()"
I'm automatically connected on the admin's collection and next, I just have to run the extinction.
Here is the official link about how to stop Mongodb properly : http://api.mongodb.org/wiki/current/Starting%20and%20Stopping%20Mongo.html
Best
Open Command Prompt/Powershell as Administrator.
Execute
net stop MongoDB
From the mongo shell documentation:
use dbname
This command does not work in scripted mode. Instead you will need to explicitly define the database in the connection (/dbname in the example above).
Alternately, you can also create a connection within the script:
db2 = connect("server:27017/otherdbname")
I've come up with the following code:
Save the following snippet in stop_mongod.js file:
db = connect("localhost:27017/admin");
db.shutdownServer();
quit();
Adjust the connection string if necessary. Then from the command line or within your batch script:
mongo stop_mongod.js
If the server is running as the foreground process in a terminal, this
can be done by pressing
Ctrl-C
Another way to cleanly shut down a running server is to use the shutdown command,
> use admin
> db.shutdownServer();
Open Command prompt as an administrator
net stop mongodb
From the Windows command line. I'm using MongoDB 3.4 Server on 64-bit Windows 7 Pro SP1.
The following links explain the steps:
configure-a-windows-service-for-mongodb-community-edition
manually-create-a-windows-service-for-mongodb-community-edition
The following console command line terminates MongoDB at start-up:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Windows\System32>"C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe" --remove
2017-12-06T08:15:47.883-0600 I CONTROL [main] Trying to remove Windows service 'MongoDB'
2017-12-06T08:15:47.884-0600 I CONTROL [main] Service 'MongoDB' removed
The MongoDB service create command line (shown below) is located in the Windows registry here:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\MongoDB
To check, do the following:
C:\Windows\System32>sc.exe create MongoDB binPath= "\"C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe\" --service --c
onfig=\"C:\Program Files\MongoDB\Server\3.4\mongod.cfg\"" DisplayName= "MongoDB" start= "auto"
[SC] CreateService SUCCESS
C:\Windows\System32>net start MongoDB
The MongoDB service is starting.
The MongoDB service was started successfully.
C:\Windows\System32>net stop MongoDB
The MongoDB service is stopping.
The MongoDB service was stopped successfully.
C:\Windows\System32>"C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe" --remove
2017-12-06T08:16:36.504-0600 I CONTROL [main] Trying to remove Windows service 'MongoDB'
2017-12-06T08:16:36.505-0600 I CONTROL [main] Service 'MongoDB' removed
C:\Windows\System32>
If you install mongodb as a service on windows. you can stop the service using following command in elevated command prompt
net stop mongodb
I guess, using TASKKILL /IM mongod.exe is fine to terminate the mongodb server gracefully.

Mongo shell not connecting

Mongo shell not connecting
I have dedicated centos server
I installed mongodb as a service in my server from here
I even restarted server after installing mongodb
When i tries to run mongo shell it always saying
Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146
I tried to remove that mongod.lock but no joy
I dont understand why this happens, because when mongod starts as a service it creates a log in system and telling that it has opened the door to listen incoming connection on port 27017. But it is throwing error while i connecting to mongo shell.
Please see this steps i have used --> click here
Well i wants to use this mongo service first of all at local based with only 127.0.0.1 access. I can change that in mongod.conf file afterward.
Be sure with iptables, Firwalll causing this kind of issue on live server.
So please do this if you facing this kind of same issue,
Do iptables off for a moment
# /etc/init.d/iptables stop
# sudo service mongod start
# mongo
Here what i wanted to see finally,
MongoDB shell version: 2.6.7
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
show dbs
admin (empty)
local 0.078GB
Thanks guys who considered my question and looking in it.
Thanks a lot.

MongoDB stuck on clientcursormon (waiting for connections on port 27017)

After simply installing MongoDB and creating \data\db within the MongoDB folder, I proceeded to run mongod.exe --dbpath C:\MongoDB\data\db. It runs a bunch of initandlisten code and then when it waits for connections on port 27017 it hangs and writes out mem res:47 virt:171 mapped:80 connections:0. I am very new to Mongo and haven't found a definitive answer on why this is hanging and how to fix it.
If you want to run the mongod.exe process as a service under Windows (to avoid it taking up a command prompt) you should following these directions for MongoDB 2.6.0 (2.6.0 has a bug with the normal approach to configuring MongoDB as a service).
There is a known issue for MongoDB 2.6.0, SERVER-13515, which prevents
the use of the instructions in this section. For MongoDB 2.6.0, use
Manually Create a Windows Service for MongoDB to create a Windows
Service for MongoDB instead.
http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/#manually-create-windows-service
For MongoDB versions 2.6.1 and later follow the normal directions:
http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/#configure-a-windows-service-for-mongodb
Mongo waiting on 27017 even after reinstall
The waiting for connections message in the console output indicates that the mongod.exe process is running successfully.
After you install MongoDB, you need to connect to MongoDB through mongo.exe shell using another command prompt.
As soon as you are done with that, the waiting message in first console would change to connection accepted.

Resources