I have created a Systemd unit file to run Mongodb on system startup. I have also created a systemd service to run my node application on startup after mongodb is started. The service for mongodb works fine, but for some reason my service for the Node application tries to run and then gives the error: "MongoError: failed to connect to server [localhost:27017] on first connect". If I start the mongodb service using $systemctl start mongodb
and then start my Node application using $/usr/bin/node /node_app_slot/server.js
It seems to work fine. So the problem seems to be with my systemd unit file for my Node server.
I used this for the mongodb systemd service https://gist.github.com/jwilm/5842956
And here is my node_server.service:
[Unit]
Wants=network.target mongodb.service
After=network.target mongodb.service
[Service]
ExecStart=/usr/bin/node /node_app_slot/server.js
[Install]
WantedBy=multi-user.target
Im doing this on an intel edison set up in Access Point mode using hostapd. The OS is Yocto and is up to date with the latest release.
I can't see where Im going wrong. I will really appreciate it if someone could guide me in the right direction!
Thanks!
Your systemd
syntax is correct, although you may want to use the network-online.target
instead of network.target
.
A workaround to consider is to add a 5 second sleep before your MongoDB app starts. Since this is happening at server boot time, the extra 5 seconds are unlikely to make a practical difference but may solve your problem.
ExecStartPre=/bin/sleep 5
It is curious that you are copy/pasting a MongoDB systemd
service file from the internet when MongoDB ships with their own systemd
service files. You didn't mention your OS version, systemd version or MongoDB version, but I would still recommend referencing the official MongoDB
systemd configuration files if aren't using a version of MongoDB that ships with the files.