24

I have seen on tutorials that they use --fork as parameter of mongod. But when I try to do so, it says unknown option --fork. So how to --fork mongodb on windows?

sadaf2605
  • 7,332
  • 8
  • 60
  • 103

6 Answers6

23

You can write start /b (/b - means execute without new cmd window) before mongod command. It will start your mongod command asynchronous and release console prompt. So, has similar effect, like fork.

It can be used in .bat scripts, for example, starting replica set:

start /b mongod --replSet m101 --logpath "1.log" --dbpath data\rs1 --port 27017 --smallfiles

start /b mongod --replSet m101 --logpath "2.log" --dbpath data\rs2 --port 27018 --smallfiles

start /b mongod --replSet m101 --logpath "3.log" --dbpath data\rs3 --port 27019 --smallfiles

...

Community
  • 1
  • 1
Hersh
  • 630
  • 4
  • 16
  • 1
    how would you then stop each or any of the mongods ? – sdfor Jun 19 '14 at 20:31
  • 3
    mongo localhost:27017 --eval "db.adminCommand({shutdown : 1})" mongo localhost:27018 --eval "db.adminCommand({shutdown : 1})" mongo localhost:27019 --eval "db.adminCommand({shutdown : 1})" – Hersh Jun 20 '14 at 07:14
  • not sure what version of windows and mongodb you are using but this does not work with win 7 and MongoDB v4 –  Sep 16 '18 at 14:10
14

--fork is actually a Linux command not a Windows or mongod command. I do not believe the same exists on Windows at all.

Linux has two primitives here, fork and exec however Windows only really has createProcess which is effectively fork-and-exec.

Setting up a service and running it in fork mode is not the same, a service is more like a init.d script however that is currently the only way really.

Cygwin can emulate fork on Windows, very slowly, as described here: What is the closest thing windows has to fork()?

Community
  • 1
  • 1
Sammaye
  • 43,242
  • 7
  • 104
  • 146
3

The --fork option is not for MongoDB for Windows users and you must execute this every mongod command in different window:

mongod --replSet m101 --logpath "1.log" --dbpath /data/rs1 --port 27017 --smallfiles --oplogSize 64  

mongod --replSet m101 --logpath "2.log" --dbpath /data/rs2 --port 27018 --smallfiles --oplogSize 64 

mongod --replSet m101 --logpath "3.log" --dbpath /data/rs3 --port 27019 --smallfiles --oplogSize 64 

MongoDB for Unix-like platforms option --fork

Enables a daemon mode for mongod that runs the process to the background. This is the normal mode of operation, in production and production-like environments, but may not be desirable for testing.

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
2

Windows doesn't support the fork mechanism like Unix-alikes do, the closest equivalent on Windows would be to run mongod as a service, as explained in the Mongo manual.

fvu
  • 32,488
  • 6
  • 61
  • 79
1

In windows there is no fork command worked since there is alternate to use mongodb as service by using following command: To Start Service

net start mongodb

To stop service: Open cmd and use below command

net stop mongodb

enter image description here

Pramod Kharade
  • 2,005
  • 1
  • 22
  • 41
0

The command --fork only works for Linux/Unix. If you need --fork for running mongod in background, on Windows that can be achieved by installing mongod as a service.

When use mongod --install option to install mongodb service, the --dbpath and --logpath are required. In my case, I need use administrator command console to install the service.

For example:

mongod --dbpath=C:\mongodb\data --logpath=C:\mongodb\data\log\service.log --install

Then browse the log file to see service name and other information including any possible errors.

2014-06-25T18:21:14.245-0700 Trying to install Windows service 'MongoDB'
2014-06-25T18:21:14.253-0700 Service 'MongoDB' (MongoDB) installed with command line 'C:\mongodb-win32-x86_64-2008plus-2.6.1\bin\mongod.exe --dbpath=C:\mongodb\data --logpath=C:\mongodb\data\log\service.log --service'
2014-06-25T18:21:14.254-0700 Service can be started from the command line with 'net start MongoDB'

The default service name is "MongoDB". You can name the service name with --serviceName option when install the service.

Then start the service with net use.

net use MongoDB

Note when installing the service, it's better to use absolute path for --dbpath and --log. Otherwise the service might have a problem to start. Details have been discussed here: Cannot start MongoDB as a service

Community
  • 1
  • 1
euccas
  • 755
  • 9
  • 13