MongoDB: log mongod to stdout - mongodb

I have been running MongoDB on my home laptop for awhile now. running mongod outputs to stdout as it should. super...
I just installed it on my work laptop but it is logging to a file.
$ sudo mongod
all output going to: /usr/local/var/log/mongodb/mongo.log
MongoDB should by default be logging to stdout according to the docs. This installation of mongoDB was done via home brew. Why is mongod logging to a file, and how do i get it to log to stdout instead?

If you run cat `which mongod` you can see that the Ruby wrapper around the mongod binary appends the default config if the --config arguments isn't passed.
ARGV << '--config' << '/usr/local/etc/mongod.conf' unless ARGV.find { |arg| arg =~ /--config/ }
exec "/usr/local/Cellar/mongodb/2.2.3-x86_64/mongod", *ARGV
The default config contains the following
# Store data in /usr/local/var/mongodb instead of the default /data/db
dbpath = /usr/local/var/mongodb
# Append logs to /usr/local/var/log/mongodb/mongo.log
logpath = /usr/local/var/log/mongodb/mongo.log
logappend = true
# Only accept local connections
bind_ip = 127.0.0.1
as you can see both logappend and logpath is set. The logpath setting is normally None meaning /dev/stdout.
To the point. mongod --config false will output to STDOUT. That will run the mongod binary without a config. If that isn't ideal, change the config settings in /usr/local/etc/mongod.conf.

Besides turning off file logging as others have suggested, you can also do the following:
sudo mongod; tail -f /usr/local/var/log/mongodb/mongo.log

from the docs:
systemLog.destination Type: string
The destination to which MongoDB sends all log output. Specify either
file or syslog. If you specify file, you must also specify
systemLog.path.
If you do not specify systemLog.destination, MongoDB sends all log
output to standard output.

As other answers have said, it is likely that you have the "logpath" setting made in a configuration file. If you don't know where that is, you can find that out by running mongod like:
sudo su -
strace -e open mongod
This spits out some information - every file mongod tries to open - in which you should be able to find the config file. For me, it looked like this (partially):
…
open("/proc/cpuinfo", O_RDONLY) = 3
open("/dev/urandom", O_RDONLY) = 3
open("/etc/mongodb.conf", O_RDONLY) = 4
all output going to: /var/log/mongodb/mongodb.log
open("/var/log/mongodb/mongodb.log", O_WRONLY|O_CREAT|O_APPEND, 0666) = 4
open("/var/log/mongodb/mongodb.log", O_WRONLY|O_CREAT|O_APPEND, 0666) = 1
…
After you have found the file, comment out (with a #) the logpath= line and your log should be output to stdout.

On my machine brew created a mongod.conf file in /usr/local/etc/.
Moreover, you should see something like this in line 7 (or so) of your log file /usr/local/var/log/mongodb/mongo.log:
Tue Jan 8 10:14:25 [initandlisten] options: { bind_ip: "127.0.0.1", config: "/usr/local/etc/mongod.conf", dbpath: "/usr/local/var/mongodb" }

It's an old question and above answer might not be valid anymore.
Documentation says:
systemLog.destination Type: string
The destination to which MongoDB sends all log output. Specify either
file or syslog. If you specify file, you must also specify
systemLog.path.
If you do not specify systemLog.destination, MongoDB sends all log
output to standard output.
Changing the configuration file /etc/mongod.conf does not work for me.
$ cat /etc/mongod.conf
# mongod.conf for MongoDB local Server
# where to write logging data.
#systemLog:
# destination: file
# logAppend: true
# logRotate: reopen
# path: /var/log/mongodb/mongod.log
# Where and how to store data.
storage:
dbPath: /var/lib/mediation/mongodb
directoryPerDB: true
$ mongod --config /etc/mongod.conf
BadValue: --fork has to be used with --logpath or --syslog
Also this one is failing:
$ mongod --config /etc/mongod.conf --logpath ""
{"t":{"$date":"2022-10-24T12:43:20.863Z"},"s":"F", "c":"CONTROL", "id":20574, "ctx":"-","msg":"Error during global initialization","attr":{"error":{"code":2,"codeName":"BadValue","errmsg":"logpath cannot be empty if supplied"}}}
The solution for me is this one:
mongod --config /etc/mongod.conf --logpath /proc/$$/fd/0

Related

Find out what happened to my data in mongodb?

I'm using mongodb in a project. I shut down mongodb and when I restarted mongodb all my databases and collections were gone. How do I figure out what happened?
In order to find the files, you can check the db path. You can do this by using
grep dbPath /etc/mongod.conf
(on default installations). This should return a line with the path to the files.
If the first command does not work, you can use the command
ps aux | grep mongo
and search for the mongod process. There should be something like this on the output:
/usr/bin/mongod --config /etc/mongod.conf
If there is a --dbpath argument on the process, you should look there for the files.
And in case your files are not in the dbpath fodler, the last resource I can think of is searching for the files on the whole file system:
sudo find / -name 'mongo*'
(you should find the mongod.lock file in the folders that were already used by Mongo).
If you happen to find your files in another folder that is not being used by Mongo, then you'll have to change the dbPath on config.

monit is not able to read mongod.lock

I am writing a monit script for mongo(in ubuntu). Script given below
check process mongodb
with pidfile "/var/lib/mongodb/mongod.lock"
start program = "/sbin/start mongodb"
stop program = "/sbin/stop mongodb"
But I am getting error log
monit: Error reading pid from file '/var/lib/mongodb/mongod.lock'
Please let me know if any work around in monit for this issue.
Monit needs a pid file (containing the pid of the mongod process) so you have to create it when you start mongod
#!/bin/bash
mongod &
echo "$!" > /var/run/mongod.pid
and set /var/run/mongod.pid as pidfile
A bit late, but I found the pid file here: /var/run/mongodb.pid
I had the same error using MongoDB, this solved my problem:
chown -R mongod:mongod /var/lib/mongodb

Homebrew on OSX Mavericks: updated MongoDB to 2.4.8, start results in "error command line: unrecognized line in '/usr/local/bin/mongod'"

I updated MongoDB using Homebrew, but now I can't start it anymore.
Console:
$ mongo
MongoDB shell version: 2.4.8
connecting to: test
Tue Dec 10 09:51:57.364 Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145
exception: connect failed
Daemon:
$ mongod
error command line: unrecognized line in '/usr/local/bin/mongod'
use --help for help
This is the content of /usr/local/bin/mongod:
#!/usr/bin/env ruby
ARGV << '--config' << '/usr/local/etc/mongod.conf' unless ARGV.find { |arg|
arg =~ /^ *--config$/ or arg =~ /^ *-f$/
}
exec "/usr/local/Cellar/mongodb/2.4.8/mongod", *ARGV
And this is the content of /usr/local/etc/mongod.conf:
# Store data in /usr/local/var/mongodb instead of the default /data/db
dbpath = /usr/local/var/mongodb
/usr/local/bin/mongod
# Only accept local connections
bind_ip = 127.0.0.1
The data folder exists:
$ ls /usr/local/var/mongodb
journal mongod.lock
No idea how I can approach to fix this issue. Any help is highly appreciated.
you seem to have an extra line in your mongod.conf which just has the db path directory. Remove that and retry.

How to set permernent dbpath for mongodb

I understand that mongo db need to be started before I can interact with it. But what I don't understand why do I set the dbpath every time? I thought we only need to configure that path once. Am I correct?
You can solve this two ways:
change your dbpath to the hard coded one which will point to /data/db/
Or make a startup script that will actually call the MongoDB instance for you
You could make a few scripts, as I said in my last point, to do this for you, as an example:
=== rnMongo.sh ===
./mongod --dbpath
Then with a single command:
./rnMongo.sh
Or as an upstart job:
# mongodb - Mongo Starter
author "lol"
description "Starts the MongoDB servers"
start on started network-services
#expect fork
exec /home/ubuntu/mongodb/bin/mongod --auth
#echo "Mongodb is now running";
#exit 0;
#stop
stop on runlevel [016]
#pre-stop
Something along those lines
Just add mongod --dbpath /home/user/mongodb to your startup applications ;)
sudo mongod --port portnumber --dbpath /path to your folder
By default it is set to
sudo mongod --port 27017 --dbpath /var/lib/mongodb

How can I run MongoDB as a Windows service?

How can I set up MongoDB so it can run as a Windows service?
After trying for several hours, I finally did it.
Make sure:
you added the <MONGODB_PATH>\bin directory to the system variable PATH
run command prompt as administrator
Steps:
step 1: execute this command:
D:\mongodb\bin>mongod --remove
Step 2: execute this command after opening command prompt as administrator:
D:\mongodb\bin>mongod --dbpath=D:\mongodb --logpath=D:\mongodb\log.txt --install
NOTE: you can also append --serviceName MongoDB after the command above.
That's All!
After that right there in the command prompt execute:
services.msc
// OR
net start MongoDB
And look for MongoDB service and click start.
NOTE: Make sure to run command prompt as administrator.
If you don't do this, your log file (D:\mongodb\log.txt in the above example) will contain lines like these:
2016-11-11T15:24:54.618-0800 I CONTROL [main] Trying to install Windows service 'MongoDB'
2016-11-11T15:24:54.618-0800 I CONTROL [main] Error connecting to the Service Control Manager: Access is denied. (5)
and if you try to start the service from a non-admin console, (i.e. net start MongoDB or Start-Service MongoDB in PowerShell), you'll get a response like this:
System error 5 has occurred.
Access is denied.
or this:
Start-Service : Service 'MongoDB (MongoDB)' cannot be started due to the following error: Cannot open MongoDB service
on computer '.'.
At line:1 char:1
+ Start-Service MongoDB
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service],
ServiceCommandException
+ FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceComman
I think if you run it with the --install command line switch, it installs it as a Windows Service.
mongod --install
It might be worth reading this thread first though. There seems to be some problems with relative/absolute paths when the relevant registry key gets written.
not only --install,
also need --dbpath and --logpath
and after reboot OS you need to delete "mongod.lock" manually
Unlike other answers this will ..
START THE SERVICE AUTOMATICALLY ON SYSTEM REBOOT / RESTART
MongoDB Install
Windows
(1) Install MongoDB
(2) Add bin to path
(3) Create c:\data\db
(4) Create c:\data\log
(5) Create c:\data\mongod.cfg with contents ..
systemLog:
destination: file
path: c:\data\log\mongod.log
storage:
dbPath: c:\data\db
(6) To create service that will auto start on reboot .. RUN AS ADMIN ..
sc.exe create MongoDB binPath= "\"C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe\" --service --config=\"C:\data\mongod.cfg\"" DisplayName= "MongoDB" start= "auto"
(7) Start the service .. RUN AS ADMIN ..
net start MongoDB
IMPORTANT: Even if this says 'The MongoDB service was started successfully' it can fail
To double check open Control Panel > Services, ensure the status of the MongoDB service is 'Running'
If not, check your log file at C:\data\log\mongod.log for the reason for failure and fix it
(Do not start MongoDB via Control Panel > Services, use .. net start MongoDB)
(8) Finally, restart your machine with MongoDB running and it will still be running on restart
If you ever want to kill it ..
net stop MongoDB
sc.exe delete MongoDB
The below steps apply to Windows.
Run below in an administrative cmd
mongod --remove
This will remove the existing MongoDB service (if any).
mongod --dbpath "C:\data\db" --logpath "C:\Program Files\MongoDB\Server\3.4\bin\mongod.log" --install --serviceName "MongoDB"
Make sure that C:\data\db folder exists
Open services with:
services.msc
Find MongoDB -> Right click -> Start
You can use the command below for running mongodb as a windows service
"C:\mongodb\bin\mongod" --bind_ip yourIPadress --logpath "C:\data\dbConf\mongodb.log" --logappend --dbpath "C:\data\db" --port yourPortNumber --serviceName "YourServiceName" --serviceDisplayName "YourServiceName" --install
If you use mongodb with default parameters, you can use these values :
yourIPadress : 127.0.0.1 or localhost
yourPortNumber : 27017 (default port) or dont put --port
serviceDisplayName : only if you run more than one service (since mongodb 1.8)
There's more information on this command here
http://www.mongodb.org/display/DOCS/Windows+Service
I tried all answers and then did it the way https://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/#configure-a-windows-service-for-mongodb-community-edition describes it.
Use a config file...
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
This PowerShell Script Downloads and Installs MongoDB as Windows Service:
Set-ExecutionPolicy RemoteSigned
$mongoDbPath = "C:\MongoDB"
$mongoDbConfigPath = "$mongoDbPath\mongod.cfg"
$url = "http://downloads.mongodb.org/win32/mongodb-win32-x86_64-2008plus-2.4.9.zip"
$zipFile = "$mongoDbPath\mongo.zip"
$unzippedFolderContent ="$mongoDbPath\mongodb-win32-x86_64-2008plus-2.4.9"
if ((Test-Path -path $mongoDbPath) -eq $True)
{
write-host "Seems you already installed MongoDB"
exit
}
md $mongoDbPath
md "$mongoDbPath\log"
md "$mongoDbPath\data"
md "$mongoDbPath\data\db"
[System.IO.File]::AppendAllText("$mongoDbConfigPath", "dbpath=C:\MongoDB\data\db`r`n")
[System.IO.File]::AppendAllText("$mongoDbConfigPath", "logpath=C:\MongoDB\log\mongo.log`r`n")
[System.IO.File]::AppendAllText("$mongoDbConfigPath", "smallfiles=true`r`n")
[System.IO.File]::AppendAllText("$mongoDbConfigPath", "noprealloc=true`r`n")
$webClient = New-Object System.Net.WebClient
$webClient.DownloadFile($url,$zipFile)
$shellApp = New-Object -com shell.application
$destination = $shellApp.namespace($mongoDbPath)
$destination.Copyhere($shellApp.namespace($zipFile).items())
Copy-Item "$unzippedFolderContent\*" $mongoDbPath -recurse
Remove-Item $unzippedFolderContent -recurse -force
Remove-Item $zipFile -recurse -force
& $mongoDBPath\bin\mongod.exe --config $mongoDbConfigPath --install
& net start mongodb
This was the only thing that worked for me. As everything had to be an absolute path:
C:\Program Files\MongoDB\Server\3.2\bin>mongod --install --dbpath=c:/data/db --logpath=c:/data/logs/log.txt
I also had to run it from admin cmd
The recommended way mongod --install results in error:
2015-12-03T18:18:28.896+0100 I CONTROL --install has to be used with a log file for server output
After having installed mongodb in C:\mongodb you need to simply add the logpath:
mongod --install --logpath C:\mongodb\logs\mongo.log
The path to the logfile must exist and it must be an absolute Windows path. Then you start the MongoDB service by typing:
net start MongoDB
Working on Mongo DB: 3.6
(1) Install MongoDB
(2) Add bin to environment path variable
(3) Create c:\data\db
(4) Create c:\data\mongod.log
(5) Run below Command on bin folder
.\mongod.exe --install --logpath c:\data\mongod.log --logappend
--bind_ip 12
7.0.0.1 --dbpath c:\data\db
(6) To start mongo db as service
net start MongoDB
(7) Finally run mongo in command line to check mongo shell is open or not.
Consider using
mongod --install --rest --master
For version 2.4.3 (current version as of posting date), create a config file and then execute the following:
C:\MongoDB\bin\mongod.exe --config C:\MongoDB\mongod.cfg --service
1) echo logpath=F:\mongodb\log\mongo.log > F:\mongodb\mongod.cfg
2) dbpath=F:\mongodb\data\db [add this to the next line in mongod.cfg]
C:\>F:\mongodb\bin\mongod.exe –config F:\mongodb\mongod.cfg –install
Reference
This is what worked for me:
sc.exe create MongoDB binPath= "d:\MongoDB\bin\mongod.exe --service --config=d:\MongoDB\bin\mongod.config" displayname= "MongoDB 2.6 Standard" start= "auto"
escaping the binPath was failing for me as described in the Mongo documentation
Failed:
sc.exe create MongoDB binPath= "\"C:\Program Files\MongoDB 2.6 Standard\bin\mongod.exe\" --service --config=\"C:\Program Files\MongoDB 2.6 Standard\mongod.cfg\"" DisplayName= "MongoDB 2.6 Standard" start= "auto"
The simplest way is,
Create folder C:\data\db
Create file C:\data\db\log.txt
Open command prompt as "Run as Administrator" and make sure the mogodb bin directory path is correct and write
C:\Program Files\MongoDB\Server\3.4\bin> mongod.exe --install mongod --dbpath="c:\data\db" --logpath="c:\data\db\log.txt"
Start mongodb service:
net run MongoDB
Working on MongoDB 3.4 [Windows]
Create dir C:/mongodb/data
Create a file in C:/mongodb/mongodb.config using this configuration:
storage:
engine: wiredTiger
dbPath: "C:/mongodb/data"
directoryPerDB: true
journal:
enabled: true
systemLog:
destination: file
path: "C:/mongodb/data/mongod.log"
logAppend: true
timeStampFormat: iso8601-utc
net:
bindIp: 127.0.0.1
port: 27017
wireObjectCheck : false
To install MongoDb as a service, run this command in powershell with admin power
mongod --config="C:\mongodb\mongodb.config" --install --service
Open Services.msc and look for MongoDb, then start it
Currently (up to version 2.4.4), if any path (dbpath/logpath/config) contains spaces, then the service won't start, and show the error: "The service is not responding to the control function".
I'm on version 2.4.9 and using a config file. The service wouldn't start until I surrounded the equals sign in the config file with spaces:
dbpath = D:\Mongo data
logpath = C:\mongodb\logs\mongo.log
logappend = true
Originally I had:
logpath=C:\mongodb\logs\mongo.log
I also discovered that when installing the service that you have to use an absolute path for the config file eg:
c:\mongodb\bin\>mongodb.exe C:\mongodb\bin\mongod.conf --install
Don't be tempted to put inverted commas around a dbpath with spaces. The service will appear to start when you execute net start MongoDB but it will terminate. Check the log files for confirmation that the service has really started.
Run "cmd.exe" as administrator and then run "sc.exe" to add a new Windows service.
for example:
sc.exe create MongoDB binPath= "c:\program files\mongodb\server\3.2\bin\mongod.exe"
I found that you should pass : dbpath , config and logfile to mongod with the install flag
example :
mongod --dbpath=c:\data\db --config=c:\data\db\config.cfg --logpath=c:\data\db\log.txt --install
note : I have mongod path in my path variable .
You can control the service with :
net start mongodb
net stop mongodb
These are the steps to install MongoDB as Windows Service :
Create a log directory, e.g. C:\MongoDB\log
Create a db directory, e.g. C:\MongoDB\db
Prepare a configuration file with following lines
dbpath=C:\MongoDB\db
logpath=C:\MongoDB\log
Place the configuration file with name mongod.cfg in folder "C:\MongoDB\"
Following command will install the Windows Service on your
sc.exe create MongoDB binPath= "\"C:\MongoDB\Server\3.4\bin\mongod.exe\" --service --config=\"C:\MongoDB\mongod.cfg\" DisplayName= "MongoDB 3.4" start= "auto"
Once you run this command, you will get the [SC] CreateService SUCCESS
Run following command on Command Prompt
net start MongoDB
If you install MongoDB 2.6.1 or newer using the MSI download from an Administrator Command Prompt, a service definition should automatically be created for you.
The MongoDB documentation also has a tutorial to help you Manually Create a Windows Service definition if needed.
This answer is for those who have already installed mongo DB using MSI installer.
Let's say your default installed location is "C:\Program Files\MongoDB\Server\3.2\bin"
Steps to run mongo as a window service
Open command prompt as administrator
Type cd C:\Program Files\MongoDB\Server\3.2\bin (check path properly, as you may have a different version installed, and not 3.2).
Press enter
Type net start MongoDB
Press enter
Press Windows + R, type services.msc and check if Mongo is running as a service.
I just had to restart the MongoDB (v4.4) service after editing the config file on a Windows box. Here's what I did:
Press Win+R to open the Run panel
Type in "services.msc" and press Enter
Search for "MongoDB" - you can press "m" to jump to it.
Right click - select "Restart"
Thats it!
mongod --config "C:\Program Files\MongoDB\Server\3.6\mongod_primary.cfg" --install --serviceName "MongoDB_Primary" --serviceDisplayName "MongoDB Primary"
check windows services
if you have service for mongo remove it by run bellow command mongod --remove
create mongo.cfg file with bellow content
systemLog:
destination: file
path: c:\data\log\mongod.log
storage:
dbPath: c:\data\db
path: where you want to store log datas
dbPath: your database directory
then run bellow command
sc.exe create MongoDB binPath= "\"C:\Program Files\MongoDB\Server\3.2\bin\mongod.exe\" --service --config=\"C:\Program Files\MongoDB\Server\3.2\mongod.cfg\"" DisplayName= "MongoDB" start= "auto"
binPath : mongodb installation directory
config: .cfg file address
DisplayName:Your Service Name
start service
net start MongoDB
now every things are done . enjoy that
In my case, I create the mongod.cfg beside the mongd.exe with the following contents.
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: D:\apps\MongoDB\Server\4.0\data
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: D:\apps\MongoDB\Server\4.0\log\mongod.log
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
#processManagement:
#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
Then I run either the two command to create the service.
D:\apps\MongoDB\Server\4.0\bin>mongod --config D:\apps\MongoDB\Server\4.0\bin\mongod.cfg --install
D:\apps\MongoDB\Server\4.0\bin>net stop mongodb
The MongoDB service is stopping.
The MongoDB service was stopped successfully.
D:\apps\MongoDB\Server\4.0\bin>mongod --remove
2019-04-10T09:39:29.305+0800 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2019-04-10T09:39:29.309+0800 I CONTROL [main] Trying to remove Windows service 'MongoDB'
2019-04-10T09:39:29.310+0800 I CONTROL [main] Service 'MongoDB' removed
D:\apps\MongoDB\Server\4.0\bin>
D:\apps\MongoDB\Server\4.0\bin>sc.exe create MongoDB binPath= "\"D:\apps\MongoDB\Server\4.0\bin\mongod.exe\" --service --config=\"D:\apps\MongoDB\Server\4.0\bin\mongod.cfg\""
[SC] CreateService SUCCESS
D:\apps\MongoDB\Server\4.0\bin>net start mongodb
The MongoDB service is starting..
The MongoDB service was started successfully.
D:\apps\MongoDB\Server\4.0\bin>
The following are not correct, note the escaped quotes are required.
D:\apps\MongoDB\Server\4.0\bin>sc.exe create MongoDB binPath= "D:\apps\MongoDB\Server\4.0\bin\mongod --config D:\apps\MongoDB\Server\4.0\bin\mongod.cfg"
[SC] CreateService SUCCESS
D:\apps\MongoDB\Server\4.0\bin>net start mongodb
The service is not responding to the control function.
More help is available by typing NET HELPMSG 2186.
D:\apps\MongoDB\Server\4.0\bin>
Plz try the steps in Mongo DB: 3.6 & Windows 10
mongod --remove
mongod --dbpath=C:/data/db --port 27017 --logpath C:/data/log/log.txt --service
mongod --dbpath=C:/data/db --port 27017 --logpath C:/data/log/log.txt --install
net start MongoDB