Issue
Installed mongodb few weeks ago and it was working fine, but today I found an error in the server(centos7)
When I tried to connect it was the answer
mongosh -u user-p password --authenticationDatabase admin
Current Mongosh Log ID: *****
Connecting to: mongodb://<credentials>@127.0.0.1:27017/?directConnection=true&serverSelectionT imeoutMS=2000&authSource=admin&appName=mongosh+1.9.1
MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017
Version of mongo
db version v6.0.6
Build Info: {
"version": "6.0.6",
"gitVersion": "***",
"openSSLVersion": "OpenSSL 1.0.1e-fips 11 Feb 2013",
"modules": [],
"allocator": "tcmalloc",
"environment": {
"distmod": "rhel70",
"distarch": "x86_64",
"target_arch": "x86_64"
}
}
This is the error when I try to restart in centos web panel:
Environment variable MONGODB_CONFIG_OVERRIDE_NOFORK == 1, overriding "processManagement.fork" to false
Log file
{"t":{"$date":"2023-05-30T23:16:54.136+00:00"},"s":"I", "c":"CONTROL", "id":20698, "ctx":"-","msg":"***** SERVER RESTARTED *****"}
{"t":{"$date":"2023-05-30T23:16:54.138+00:00"},"s":"I", "c":"CONTROL", "id":23285, "ctx":"main","msg":"Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'"}
{"t":{"$date":"2023-05-30T23:16:54.140+00:00"},"s":"I", "c":"NETWORK", "id":4915701, "ctx":"main","msg":"Initialized wire specification","attr":{"spec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":17},"incomingInternalClient":{"minWireVersion":0,"maxWireVersion":17},"outgoing":{"minWireVersion":6,"maxWireVersion":17},"isInternalClient":true}}}
{"t":{"$date":"2023-05-30T23:16:54.140+00:00"},"s":"I", "c":"NETWORK", "id":4648601, "ctx":"main","msg":"Implicit TCP FastOpen unavailable. If TCP FastOpen is required, set tcpFastOpenServer, tcpFastOpenClient, and tcpFastOpenQueueSize."}
{"t":{"$date":"2023-05-30T23:16:54.407+00:00"},"s":"I", "c":"REPL", "id":5123008, "ctx":"main","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"TenantMigrationDonorService","namespace":"config.tenantMigrationDonors"}}
{"t":{"$date":"2023-05-30T23:16:54.407+00:00"},"s":"I", "c":"REPL", "id":5123008, "ctx":"main","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"TenantMigrationRecipientService","namespace":"config.tenantMigrationRecipients"}}
{"t":{"$date":"2023-05-30T23:16:54.407+00:00"},"s":"I", "c":"REPL", "id":5123008, "ctx":"main","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"ShardSplitDonorService","namespace":"config.tenantSplitDonors"}}
{"t":{"$date":"2023-05-30T23:16:54.407+00:00"},"s":"I", "c":"CONTROL", "id":23330, "ctx":"main","msg":"ERROR: Cannot write pid file to {path_string}: {errAndStr_second}","attr":{"path_string":"/var/run/mongodb/mongod.pid","errAndStr_second":"No such file or directory"}}
I couldnt find /var/run/mongodb, but it was working fine, any idea what could happen or what to do?
Thank you
Solution
I guess you run into this one: https://serverfault.com/questions/74796/directory-in-var-run-gets-deleted-after-hard-reboot
Check the service file, e.g. with systemctl cat mongod
In order to make mongod running, I put these lines in the file:
[Service]
User=mongod
Group=mongod
Environment="OPTIONS=-f /etc/mongod.conf"
EnvironmentFile=-/etc/sysconfig/mongod
ExecStart=/usr/bin/mongod $OPTIONS
ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb
ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb
ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb
ExecStartPre=/usr/bin/mkdir -p /var/lib/mongo
ExecStartPre=/usr/bin/chown mongod:mongod /var/lib/mongo
ExecStartPre=/usr/bin/chmod 0700 /var/lib/mongo
PermissionsStartOnly=true
PIDFile=/var/run/mongodb/mongod.pid
Type=forking
or the newer version would be
[Service]
User=mongod
Group=mongod
Environment="OPTIONS=-f /etc/mongod.conf"
EnvironmentFile=-/etc/sysconfig/mongod
ExecStart=/usr/bin/mongod $OPTIONS
ExecStartPre+=/usr/bin/mkdir -p /var/run/mongodb
ExecStartPre+=/usr/bin/chown mongod:mongod /var/run/mongodb
ExecStartPre+=/usr/bin/chmod 0755 /var/run/mongodb
ExecStartPre+=/usr/bin/mkdir -p /var/lib/mongo
ExecStartPre+=/usr/bin/chown mongod:mongod /var/lib/mongo
ExecStartPre+=/usr/bin/chmod 0700 /var/lib/mongo
PIDFile=/var/run/mongodb/mongod.pid
Type=forking
Note, usually you don't edit service file /usr/lib/systemd/system/mongod.service
directly. It might be reverted when you run any updates of MongoDB. Instead, create a copy to /etc/systemd/system/mongod.service
and put your customized settings there.
Answered By - Wernfried Domscheit Answer Checked By - David Goodson (WPSolving Volunteer)