how to restart mongodb in terminal? - node.js

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.

Related

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

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.

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.

postgresql commands not being executed

I downloaded postgresql-9.1.
but when I execute the command in terminal:
$ createdb mydb
I get the following message:
createdb: could not connect to database postgres: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
please tell me how to sort this problem.I have even tried to uninstall and then reinstall it. I am trying to run this on linux mint 15 (cinnamon).
Use ps to see if the postgresql process is actually running. If not, start it (service postgresql start). Normally should start up itself on boot after installation but who knows. Also, some error may prevent it from starting on boot.
If postgresql is running, then you may need to edit pg_hba.conf in the data directory. Again, normally the database should listen for local connections by default but who knows.

Resources