Autorestart MongoDB on Ubuntu - linux

I have mongodb set to autoload in systemctl. But when I make too many requests to it, it crashes with the mark failed. how can I make autorestart mongodb so that even after an error it starts up and works again, now I have to manually prescribe systemctl start mongod

As #papey say, there is need to check Restart=always field in this file /lib/systemd/system/mongod.service

Related

Starting MongoDB Replica Set

So, I was making an app that needs to use MongoDB transactions. But the Mongoose documentation told me that "MongoDB currently only supports transactions on replica sets, not standalone servers." So I thought I basically need to switch my Standalone MongoDB instance to Replica Set (whatever that means).
The MongoDB documentation gave me the instruction of how to do this with a few steps:
Shutdown the Standalone MongoDB instance
Restart the instance using the --replSet option
mongod --port 27017 --dbpath /var/lib/mongodb --replSet rs0 --bind_ip localhost
Connect the Mongo Shell
Call rs.initiate() inside the Shell
I'm stuck at step 2. All I know, when you want to start using MongoDB, you have to start its daemon first, using sudo systemctl start mongod, and then start using it by connecting your app. but that step told me to use mongod command to start the Mongod daemon, instead of systemctl. I tried the command but got the following error:
DBException in initAndListen, terminating","attr":{"error":"IllegalOperation: Attempted to create a lock file on a read-only directory: /var/lib/mongodb"}}
At first, I thought it was some kind of a privilege issue, so I ran it again with sudo but then it ended up destroying my entire database and prevented me from starting the MongoDB the "normal way" with giving me errors that I cannot remember.
I just reinstalled the whole MongoDB to get it back to work fine. Now I'm at the same place as yesterday, unable to convert to replica set, only now my entire database is gone. What do I do to enable it?
When running the mongod as a service, use /etc/monogd.conf to set the configuration. Note that the location or name of this file might have been changed in the mongod.service file in your system.
See replication options for how to set that in the file.

What's difference between these redis starting commands

sudo /etc/init.d/redis-server start
sudo service redis-server start
sudo systemctl start redis-server
sudo redis-server --daemonize yes
The last one is "nearest to the metal", it directly starts the Redis server process with no special options, and is "stand-alone". I would use this type of command when just "messing around" in the Terminal with quick tests and when trying to get an initial configuration tested and running.
The first 3 are all basically wrappers around starting the Redis server process to make it compatible with systemd or other Linux startup systems. They potentially add more layers of management, like:
reporting to the systemctl logs
saving the process id so the process can be killed or restarted
potentially specifying a different config file
potentially waiting for other services to become available before starting Redis
I would prefer one of the first three for routine, every-day, managed starting up of Redis on a production system.

What is the difference between 'service apache2 reload' and 'sudo systemctl restart apache2'?

What is the difference between service apache2 reload and sudo systemctl restart apache2?
I understand that one uses sudo and others don't.
Also, I can understand the difference between reloading and restart.
But what is the major difference between these two commands?
Restart = stop + start
Reload = remain running + re-read configuration files
We could define it like this:
Restart--> STOP the service and then it will START the service.
Now comes Reload option.
Reload--> Read .service file for which you have executed the command
and if any changes happened it will start using those changes now, so
each time a change happens in any service file a reload is needed. You
could even see this message coming, lets say you have changed a
service and you forgot to reload it so whenever you run any systemctl
command towards that service it will throw an error to reload it.

Systemd service for Node server can't connect to MongoDB

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.
I had similar problem. The problem may be the fact that you try to connect before the network service has finished starting. You can take a look at here to see if it solves your problem:
https://unix.stackexchange.com/questions/126009/cause-a-script-to-execute-after-networking-has-started

PostgreSQL 9.5 doesn't start after reboot with systemd

I'm having a problem with PostgreSQL 9.5+173 on Ubuntu 16.04 and I happened to stumble across the following threads in my research that somewhat describes the behavior I'm seeing:
https://www.postgresql.org/message-id/CAFyxdeT%2B%3Dx-d0oNbFPoe%2B4xnt0Qdfi%2BzAEn%2BrQmEK0AZbJFRtg%40mail.gmail.com
https://www.postgresql.org/message-id/562E4453.5090803%40aklaver.com
Long story short I have a fresh install of Ubuntu 16 with nothing on it and PostgreSQL running. I've stopped PostgreSQL changed the data directory and port and a couple other settings and it starts back up fine.
I can start and stop PostgreSQL manually via systemctl without any problems. I can also connect to the database and can verify that it is running via a ps ax | grep postgres.
However, after I reboot PostgreSQL will not start up. Any attempt to start it up via systemctl start postgresql.service doesn't do anything and does not fail. The only way I am able to get it started is if I call systemctl start postgresql#9.5-main.service.
I did some investigation and looked at both the postgresql.service and postgresql#9.5-main.service scripts and realized that the postgresql.service script does nothing as stated in the thread above and that the postgresql#9.5-main.service has the PartOf directive which means it should be getting triggered from the postgresql.service as the sytemd docs state, but it isn't for some reason. Basically I'm at a loss as to why everything works before I reboot and then doesn't work after I reboot. Is there something I'm missing? I'm starting to go CRAZY over something so simple.
Update: I added an ExecStartPre=/bin/touch /tmp/postgresq.log to the postgresql#9.5-main.service to see if it's actually getting called on boot and it is not. Manually calling systemctl start postgresql#9.5-main.service creates the file in the /tmp directory.
Update: I have also found that calling systemd daemon-reload after reboot will allow me to start postgres via the systemctl start postgresql command.
Did you try doing systemctl enable postgresql? This will tell systemd to start this service after boot. Try rebooting after that.
Turns out that the problem was the fact that I symlinked /etc/postgresql/9.5/main/ across partitions to a custom partition that wasn't available right away, so when PostgreSQL tried to start on boot it couldn't because it's configuration files were not available. This describes what was happening since I could start PostgreSQL manually after I logged in.

Resources