Have node server to a server application which is deployed on amazon ec2 instance with OS Ubuntu 18.04 LTS version, node- 12.18.3, mongoDB - 4.2.8, npm- 6.14.6 & pm2- 4.4.0.
The command service mongod status returns active(running status) now when pm2 status mongod command is executed it starts and both show online status running but when pm2 status command is executed node shows online status whereas mongod shows errored.
Have tried to make mongod --repair but still the problem persist.
What is the solution for this?
Related
I've got problem with running mongodb on wsl ubuntu.
During using powershell everything works fine. I type node db.js and its working. After switching terminal to ubuntu and run the same command i get an error: "MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017".
Any idea how to resolve this problem?
Try to check mongod service status via cli
use service mongod status to check
If it's not active then try to activate
use this sudo service mongod start
If mongod service is not found then try to install the mongodb
I have 3 servers running a MongoDB replica set.
I currently start the node like this after restarting a server:
mongod --replSet "name_of_replicaset" --bind_ip localhost,this_server_hostname_in_host_file --fork --syslog --auth --keyFile "path_to_file"
The problem is that I have to do this every single time I restart a server.
I've checked online and the official documentation for MongoDB but I cannot find a way to make the server startup the node by itself.
Is there a way to have the server startup the node after it restarting?
You can do this using systemctl.
Create a service for this and enable the same via systemctl enable command. It will automatically start once you restart the server.
in an Ubuntu Server, I am unable to run the node.js app in cluster mode using PM2.
The command I use is :
PM2 start server.js --name Server -i max
When I list the PM2 processes, I can see the Server has Error status.
I have tried looking into the log file generated by PM2 but it's empty.
I am however able to run the same server.js without the cluster mode using :
PM2 start server.js --name Server
doing PM2 Kill and starting all the services again was the solution to above issue.
You could also have used pm2 restart Server to restart it
If you use pm2 kill you will just kill all processes, to clean up afterwards i would recommend to use pm2 flush so all logfiles will be reset
I have gone through this same kind of situations but in my case pm2 is showing error status cause of error in my code.
use the below command
pm2 logs
pm2 logs command helped me by showing some hints to check where exactly the error is occured.
if everything works fine then pm2 list will show you the status online.
you can check the ports running by pm2(not only pm2 but all the process) using below command
sudo netstat -tulpn
I am having trouble making pm2 wait for mongodb to be ready before it starts a process on system reboot. (I am using Ubuntu 16.04 server)
In my systemd service file for pm2 I have this, which I thought would make it wait until after mongodb was started:
[Unit]
Description=PM2 process manager
Documentation=https://pm2.keymetrics.io/
Wants=mongod.service
After=network.target mongod.service
[Service]
Type=forking
But it seems that it runs pm2, and therefore the node.js processes that pm2 launches before the mongodb actually is ready to listen on 27017. My error logs show that my node.js script can't find the db on 27017. But if I manually restart the process (after mongodb has had plenty of time to get ready) it works just great.
How can I delay pm2 starting, or delay pm2 starting a particular app until after mongodb is ready and listening for connections?
Note: I tried adding
ExecStartPost=/bin/sh -c 'while ! /usr/bin/mongo --eval "db.version()" > /dev/null 2>&1; do sleep 0.1; done'
To my mongod.service as per this answer: Systemd: Autostart service after mongodb, but that just prevented pm2 from starting at all.
I just ran into this exact same issue and the solution I finally found was to have pm2 also start mongodb. As long as you start mongodb before you start npm and then save the current process list before having pm2 generate the startup script, everything works great.
So, to get pm2 to run mongodb, you need to create a shell script that pm2 can run. This is what my mongod.sh file looks like:
#!/bin/bash
sudo mongod
I put that mongod.sh file in my user directory and then started it and npm with pm2 like so:
pm2 start ~/mongod.sh
pm2 start npm
You can check to make sure both scripts are running with pm2 and in the right order by running
pm2 status
In the process, you should see both mongod and npm running and the id for mongod should be 0 (or just lower than the npm process).
Once I had both mongod and npm running with pm2, I saved the pm2 process list and reset the startup script:
pm2 save
pm2 unstartup
pm2 startup
I have my server on AWS in which I am running the MongoDB but all of the sudden mongod server get stop and showing message Killed. Then I restart the mongod server again but after 10 to 15 minutes mongod server gets stops automatically and showing the same message. This is happening again and again.
My AWS server is t2.small and I am running mongod on the separate screen on the Linux shell and node server on different screen.
The message it is showing is displayed in the image below.
enter image description here
Please help me to make the mongod server running continuously .
You can run mongod in background as a daemon by using fork option and --logpath option.
mongod --fork --logpath /tmp/m.log
More info here
https://docs.mongodb.com/manual/tutorial/manage-mongodb-processes/