Friday 11 June 2021

--fork: run mongodb in background

--fork option is used to run mongoDB in background.

mongod --port 8888 --dbpath /Users/Shared/data/db --logpath /Users/Shared/log/mongo.log –fork

 

Above command runs MongoDB on port 8888. Since it is running in background, I set log file explicitly to log the information.

$mongod --port 8888 --dbpath /Users/Shared/data/db --logpath /Users/Shared/log/mongo.log --fork
about to fork child process, waiting until server is ready for connections.
forked process: 47510
child process started successfully, parent exiting

 

You can connect to MongoDB server running on port 8888 using ‘mongo --port 8888’ command.

$mongo --port 8888
MongoDB shell version v4.4.5
connecting to: mongodb://127.0.0.1:8888/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("8658a606-5c22-4831-89b2-504e7b68760c") }
MongoDB server version: 4.4.5
---
The server generated these startup warnings when booting: 
        2021-06-10T12:43:08.559+05:30: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2021-06-10T12:43:08.560+05:30: This server is bound to localhost. Remote systems will be unable to connect to this server. Start the server with --bind_ip <address> to specify which IP addresses it should serve responses from, or with --bind_ip_all to bind to all interfaces. If this behavior is desired, start the server with --bind_ip 127.0.0.1 to disable this warning
        2021-06-10T12:43:08.561+05:30: Soft rlimits too low
        2021-06-10T12:43:08.561+05:30:         currentValue: 2560
        2021-06-10T12:43:08.561+05:30:         recommendedMinimum: 64000
---
---
        Enable MongoDB's free cloud-based monitoring service, which will then receive and display
        metrics about your deployment (disk utilization, CPU, operation statistics, etc).

        The monitoring data will be available on a MongoDB website with a unique URL accessible to you
        and anyone you share the URL with. MongoDB may use this information to make product
        improvements and to suggest MongoDB products and deployment options to you.

        To enable free monitoring, run the following command: db.enableFreeMonitoring()
        To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
>

How to kill the mongod process in Mac?

Get the process id of mongod service.

$ps -eaf | grep mongod
  502 47510     1   0 12:43PM ??         0:01.75 mongod --port 8888 --dbpath /Users/Shared/data/db --logpath /Users/Shared/log/mongo.log --fork
  502 50467 35373   0 12:46PM ttys001    0:00.00 grep mongod


Kill the process using kill command.

$kill -9 47510
$
$ps -eaf | grep mongod
  502 50514 35373   0 12:47PM ttys001    0:00.00 grep mongod


Since Mongod server is stopped, you can’t connect to it usning mongo shell.

$mongo --port 8888
MongoDB shell version v4.4.5
connecting to: mongodb://127.0.0.1:8888/?compressors=disabled&gssapiServiceName=mongodb
Error: couldn't connect to server 127.0.0.1:8888, connection attempt failed: SocketException: Error connecting to 127.0.0.1:8888 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:374:17
@(connect):2:6
exception: connect failed
exiting with code 1


Note

Fork option is not available in windows.





 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment