Running mongodb forever using forever in windows - node.js

I am trying to run MongoDB server forever locally on Windows 10. And for that I'm using forever server.js which throws an error i.e, Forever is not installed.
I did npm install. But then too showing the same error.
I have followed this guide, but it's for Ubuntu. I am using Windows 10.
I need something like if I close the server cmd also, it should keep running.

In Windows, you can use the mongod command to register Mongo as a service
mongod --directoryperdb --dbpath "C:\Program Files\MongoDB\Server\3.4\data\db" --logpath "C:\Program Files\MongoDB\Server\3.4\log\mongo.log" --logappend --rest --install
Open the cmd(command prompt) as administrator
run the above command
open services and search for mongo service
If it is not running already, click start to start the service

You need to install mongo as a service.
To do so,
Open cmd in administrator mode.
Create two directories to store data and logs
mkdir c:\data\db
mkdir c:\data\log
Create a configuration file. The file must set systemLog.path. Include additional configuration options as appropriate.For example, create a file at C:\Program Files\MongoDB\Server\3.6\mongod.cfg that specifies both systemLog.path and storage.dbPath like this:
systemLog:
destination: file
path: c:\data\log\mongod.log
storage:
dbPath: c:\data\db
install mongo service, run the following command:
mongod.exe --config "C:\Program Files\MongoDB\Server\3.4\mongod.cfg" --install
Start service using: >net start mongodb
To stop service use : >net stop mongodb
You can refer official documents here

1. mongod --install --dbpath D:\data\db --logpath D:\log\mlog.txt
2. net start MongoDB
All above command should be run as administrator and you can change dbPatha and logpath with your path
How to start & stop a mongo DB server from the command line

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.

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.

Running mongod in shell works but it does not work as a service

I am running mongod under Linux OS. I wanted to change my data directory from the default /var/lib/mongodb to another location say /nfs/mongodb.
When I run mongodb from shell(i.e. sudo /usr/bin/mongod --dbpath /nfs/mongodb) It works just fine.
Next step, I tried to run mongodb as a service(sudo service mongodb start)
I modified the file /etc/mongodb.conf and changed the line dppath=xxx to point to the new directory I created. When I run mongodb as service I get this error:
couldn't open file /nfs/mongodb/journal/j._0 for writing errno:1 Operation not permitted, terminating
Why the mongodb works in the shell and not as a service?
This is very likely because of permission issue.
When you run sudo mongod, it runs as root and can write to any directory.
In contrast, when running as a service, MongoDB typically runs as a limited-privilege user to prevent any security issue to escalate to root level access.
The solution is to chmod or chown the intended dbpath directory so that it's writeable by the service's user.
Note: you may want to check out the Production Notes for tips on running MongoDB optimally.

Could not access local Parse server dashboard with Mongodb

I am trying to install mongodb and parse server on my windows 10 pc, after completing everything without error, I'm still unable to access the dashboard, the browser show This site can't be reach. Below shows the step on how I did it, please help me to identify the problem, thanks.
At Node.js command prompt
cd C:\MongoDB\Server\3.4\bin
mongod --authOUTPUT: waiting for connections on port 27017
Launch another Node.js command prompt
cd C:\MongoDB\Server\3.4\bin
mongo
use db_parsetestOUTPUT: switched to db db_parsetest
Launch another Node.js command prompt
cd C:\
parse-server --appId com.test.parsetest --masterKey parsetestkey --databaseURI mongodb://USERNAME:PASSWORD#localhost:27017/db_parsetestOUTPUT: [1116] parse-server running on http://localhost:1337/parse
Launch another Node.js command prompt
cd C:\
parse-dashboard --appId com.test.parsetest --masterKey parsetestkey --serverURL "http://localhost:1337/parse" --appName ParseTestOUTPUT: The dashboard is now available at http://0.0.0.0:4040/
At the browser, I try to browse http://0.0.0.0:4040/, but it couldn't reach.

MongoDB not working. "ERROR: dbpath (/data/db) does not exist."

I'm getting the following error when I try to run "mongod" in the terminal. I've tried uninstalling, reinstalling, and restarting the machine. Any suggestions on how to get it working would be amazing.
ERROR:
dbpath (/data/db) does not exist.
Create this directory or give existing directory in --dbpath.
See http://dochub.mongodb.org/core/startingandstoppingmongo
Side note:
Node also stopped working on my machine around the same time that I got this error.
events.js:72
throw er; // Unhandled 'error' event
^
Error: failed to connect to [localhost:27017]
Any help would be much appreciated!
This should work to ensure that the directory is set up in the right place so that Mongo can find it:
sudo mkdir -p /data/db/
sudo chown `id -u` /data/db
You need to create the directory on root /data/db or set any other path with the following command :
mongod --dbpath /srv/mongodb/
See the example link
I solved the problem with :
sudo mongod --dbpath=/var/lib/mongodb and then mongo to access the mongodb Shell.
Change the user of the new data directory:
chown mongodb [rute_directory]
And try another time to start the mongo service
service mongod start
I solve the same problem with this.
Daemons (usually ending with d) are normally started as services. Starting the service (daemon) will allow mongodb to work as designed (without permission changes if integrates well with your distro). I start it using the service named mongodb instead of starting mongod directly--on distro with systemd enable on startup then run like:
sudo systemctl enable mongodb
sudo systemctl start mongodb
or, on distro with upstart (if you have /etc/init) or init (if you have /etc/init.d) ( https://www.tecmint.com/systemd-replaces-init-in-linux/ ) instead run:
sudo service mongodb enable
sudo service mongodb start
If you have a distro with rc ("run commands") such as Gentoo (settings in /etc/init.d) (https://forums.gentoo.org/viewtopic-t-854138-start-0.html) run:
rc-update add mongodb default
/etc/init.d/mongodb start
In a distro/version of FreeBSD which still has rc (check whether your version switched to systemd, otherwise see below):
add the following line to /etc/rc.conf:
mongod_enable="YES"
then:
sudo service mongod start
After starting the service, an unpriveleged user can use mongo, and each user will have separate data.
I also got the error that "The file /data/db doesn't exist" when I tried to save my file using the "mkdir -p /data/db" command(using both with and without sudo command). But later on one site, a person named Emil answered that the path "/data/db" no longer works on Mac, so use "~/data/db" instead
i.e., use the command
mkdir -p ~/data/db
instead of previous command.
Moreover, use
mongod --dbpath ~/data/db
to run mongod
It worked for me, hope it work for others too facing the same problem

Resources