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.
Related
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.
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.
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
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.
When running mongo on a remote aws server (ubuntu) what is the best way to keep a mongo instance running after sshing to it to start it?
Take a look here. In short, you should use mongod --fork --logpath /var/log/mongod.log
Start it as a service. Here's the command
sudo service mongod start
You should be able to use the auto-generated init script if you installed the -10gen distro: http://www.mkyong.com/mongodb/how-to-install-mongodb-on-ubuntu/
You can use screen too for this purpose.
First execute the command
screen
Then run your server using
mongod --dbpath=/example/dbpath
Then detach it by pressing ctrl+a and then pressing d
The MongoDB documentation has two useful links
Installing on Ubuntu
http://www.mongodb.org/display/DOCS/Ubuntu+and+Debian+packages
Starting and stopping MongoDB
http://www.mongodb.org/display/DOCS/Starting+and+Stopping+Mongo
You need to use the --fork and --log parameters to detach the process from the ssh login.