Running MongoDB shell commands from within shell script connection refused - mongodb

I have the following shell script which I am running in UserData section of a CloudFormation template:
#!/bin/sh
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-4.4.list
apt-get update
apt-get install -y mongodb-org
apt install mongodb-clients
systemctl start mongod
systemctl status mongod
systemctl enable mongod
echo "ABOUT TO ENTER WHILE LOOP"
while :
do
echo "waiting to RUN MONGO COMMANDS"
echo "$(systemctl show -p ActiveState --value mongod)"
if [ "$(systemctl show -p ActiveState --value mongod)" = "active" ]
then
echo "RUNNING MONGO COMMANDS"
mongo crawler --eval "db.websites.insertOne({ customerId: '1', url: 'https://dootli.com' })"
mongo crawler --eval "db.createUser({ user: 'username', pwd: 'password', roles: 'clusterAdmin' })"
break
fi
done
As far I can tell the script is valid, how I'm getting the this output (and error) when it is run during the initialization of the EC2 instance:
Reading package lists...
Building dependency tree...
Reading state information...
The following package was automatically installed and is no longer required:
mongodb-database-tools
Use 'apt autoremove' to remove it.
The following additional packages will be installed:
libboost-filesystem1.71.0 libboost-iostreams1.71.0
libboost-program-options1.71.0 libgoogle-perftools4 libpcrecpp0v5
libsnappy1v5 libtcmalloc-minimal4 libyaml-cpp0.6 mongo-tools
The following packages will be REMOVED:
mongodb-org mongodb-org-database-tools-extra mongodb-org-mongos
mongodb-org-server mongodb-org-shell mongodb-org-tools
The following NEW packages will be installed:
libboost-filesystem1.71.0 libboost-iostreams1.71.0
libboost-program-options1.71.0 libgoogle-perftools4 libpcrecpp0v5
libsnappy1v5 libtcmalloc-minimal4 libyaml-cpp0.6 mongo-tools mongodb-clients
0 upgraded, 10 newly installed, 6 to remove and 87 not upgraded.
Need to get 35.2 MB of archives.
After this operation, 44.4 MB disk space will be freed.
Do you want to continue? [Y/n] Abort.
● mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
Active: active (running) since Sat 2021-02-06 20:38:12 UTC; 15ms ago
Docs: https://docs.mongodb.org/manual
Main PID: 2738 (mongod)
Memory: 288.0K
CGroup: /system.slice/mongod.service
└─2738 /usr/bin/mongod --config /etc/mongod.conf
Feb 06 20:38:12 ip-172-31-64-168 systemd[1]: Started MongoDB Database Server.
Created symlink /etc/systemd/system/multi-user.target.wants/mongod.service → /lib/systemd/system/mongod.service.
ABOUT TO ENTER WHILE LOOP
waiting to RUN MONGO COMMANDS
RUNNING MONGO COMMANDS
MongoDB shell version v4.4.3
connecting to: mongodb://127.0.0.1:27017/crawler?compressors=disabled&gssapiServiceName=mongodb
Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect#src/mongo/shell/mongo.js:374:17
#(connect):2:6
exception: connect failed
exiting with code 1
MongoDB shell version v4.4.3
connecting to: mongodb://127.0.0.1:27017/crawler?compressors=disabled&gssapiServiceName=mongodb
Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect#src/mongo/shell/mongo.js:374:17
#(connect):2:6
exception: connect failed
exiting with code 1
Cloud-init v. 20.3-2-g371b392c-0ubuntu1~20.04.1 running 'modules:final' at Sat, 06 Feb 2021 20:37:42 +0000. Up 27.35 seconds.
ci-info: no authorized SSH keys fingerprints found for user ubuntu.
Cloud-init v. 20.3-2-g371b392c-0ubuntu1~20.04.1 finished at Sat, 06 Feb 2021 20:38:12 +0000. Datasource DataSourceEc2Local. Up 56.97 seconds

I modified the code to work. I identified that the main issue is mongodb-clients which causes failures of monogdb. Also your command db.createUser is invalid and will lead to failure as well. I did not fix that, as its not related to your issue about connection refused. You can make new question why your db.createUser is incorrect (I don't know how to fix that, its mongodb specific).
#!/bin/sh
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-4.4.list
apt update
apt install -y mongodb-org
systemctl enable mongod
systemctl start mongod
echo "ABOUT TO ENTER WHILE LOOP"
while :
do
echo "waiting to RUN MONGO COMMANDS"
sleep 5
echo "$(systemctl show -p ActiveState --value mongod)"
if [ "$(systemctl show -p ActiveState --value mongod)" = "active" ]
then
echo "RUNNING MONGO COMMANDS"
mongo crawler --eval "db.websites.insertOne({ customerId: '1', url: 'https://dootli.com' })"
[ $? != 0 ] && continue
echo "db.websites.insertOne command successful"
break
fi
done

Related

Grafana-server Init Failed - Ubuntu 20.04

Using Grafana 8.3 in Ubuntu 20.04. When I went to the desktop environment to use Grafana, it wouldn’t connect. In the command line, when I type:
sudo netstat -lp
I noticed port 3000 wasn’t listed anymore.
starting the grafana server i get this below
a#a:~$ grafana-server
Grafana-server Init Failed: Could not find config defaults, make sure homepath command line parameter is set or working directory is homepath
when checking the status, I get this:
● grafana-server.service - Grafana instance
Loaded: loaded (/lib/systemd/system/grafana-server.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Wed 2022-01-05 16:06:11 MST; 11min ago
Docs: http://docs.grafana.org
Process: 4621 ExecStart=/usr/sbin/grafana-server --config=${CONF_FILE} --pidfile=${PID_FILE_DIR}/grafana-server.pid --packaging=deb cfg:default.paths.lo>
Main PID: 4621 (code=exited, status=1/FAILURE)
Jan 05 16:06:11 antonio- systemd[1]: grafana-server.service: Scheduled restart job, restart counter is at 5.
Jan 05 16:06:11 antonio- systemd[1]: Stopped Grafana instance.
Jan 05 16:06:11 antonio- systemd[1]: grafana-server.service: Start request repeated too quickly.
Jan 05 16:06:11 antonio- systemd[1]: grafana-server.service: Failed with result 'exit-code'.
Jan 05 16:06:11 antonio- systemd[1]: Failed to start Grafana instance.
thte only way I can get grafana to work in the desktop envionment is if I use and keep this open in the terminal:
$ sudo grafana-server -homepath /usr/share/grafana
Why has this happened and how can I use grafana without have to do this terminal step everytime?
thanks, I basically solved the problem by not solving the problem.
sudo apt-get purge grafana
sudo apt-get remove grafana
sudo apt-get remove --auto-remove grafana
then I deleted the config folder (contains the config file)
rm -R /etc/grafana/
then I reinstalled grafana
$ sudo apt-get install -y gnupg2 curl
$ curl https://packages.grafana.com/gpg.key | sudo apt-key add -
$ sudo add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"
$ sudo apt-get update
$ sudo apt-get -y install grafana
$ sudo systemctl start grafana-server
Now its running as before, with default port 3000 on startup

Using MongoDB on RaspberryPi via SSH connection in VS Code

I installed MongoDB via the official documentation on my Raspberry Pi, which I access using SSH remote connection in VSCode from my actual Desktop.
The installation looked all good, but I can't get the service to start using
sudo systemctl start mongod.
This is what I get from
sudo systemctl status mongod
mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
Active: failed (Result: core-dump) since Wed 2021-08-04 12:42:10 UTC; 1s ago
Docs: https://docs.mongodb.org/manual
Process: 4442 ExecStart=/usr/bin/mongod --config /etc/mongod.conf (code=dumped, signal=ILL)
Main PID: 4442 (code=dumped, signal=ILL)
Aug 04 12:42:10 ubuntu systemd[1]: Started MongoDB Database Server.
Aug 04 12:42:10 ubuntu systemd[1]: mongod.service: Main process exited, code=dumped, status=4/ILL
Aug 04 12:42:10 ubuntu systemd[1]: mongod.service: Failed with result 'core-dump'.
I tried changing all kinds of permissions and unfortunately no log files are created in the process. My last idea would be that I have to change the ipbind setting in order to make it work, but I already tried commenting it out in the config file, as well as replacing it with 0.0.0.0
At this point I don't know how to progress, so any help would be welcome!
Turns out the latest version (5.0) is not compatible with Raspberry Pi 4.
I got it to work by installing MongoDB 4.4.8 using the command
sudo apt-get install -y mongodb-org=4.4.8 mongodb-org-server=4.4.8 mongodb-org-shell=4.4.8 mongodb-org-mongos=4.4.8 mongodb-org-tools=4.4.8
I also then locked the versions using
echo "mongodb-org hold" | sudo dpkg --set-selections && echo "mongodb-org-server hold" | sudo dpkg --set-selections && echo "mongodb-org-shell hold" | sudo dpkg --set-selections && echo "mongodb-org-mongos hold" | sudo dpkg --set-selections && echo "mongodb-org-tools hold" | sudo dpkg --set-selections
to prevent apt-get from updating.
More information: https://www.mongodb.com/community/forums/t/core-dump-on-mongodb-5-0-on-rpi-4/115291/14

Not able to run mongodb version 4.0 on my local machine

Getting this error when I try to run command mongo:
MongoDB shell version v4.0.0
connecting to: mongodb://127.0.0.1:27017
2018-07-24T13:08:13.968+0800 E QUERY [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect#src/mongo/shell/mongo.js:251:13
#(connect):1:6
exception: connect failed
After run this command sudo service mongod status Its giving me output something like this :
mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
Active: failed (Result: core-dump) since Sel 2018-07-24 13:18:18 +08; 45s ago
Docs: https://docs.mongodb.org/manual
Process: 18153 ExecStart=/usr/bin/mongod --config /etc/mongod.conf (code=dumped, signal=ABRT)
Main PID: 18153 (code=dumped, signal=ABRT)
root#him: systemd[1]: Started MongoDB Database Server.
root#him: mongod[18153]: 2018-07-24T13:18:18.777+0800 I CONTROL [main] Automatically disabling TLS 1.0, to f
root#him: mongod.service: Main process exited, code=dumped, status=6/ABRT
root#him: mongod.service: Unit entered failed state.
root#him: mongod.service: Failed with result 'core-dump'.
The data files are created under root not the mongodb user when you run from the terminal.
You need to give permissions:
cd /var/lib/mongodb
sudo chown -R mongodb:mongodb *
Hope this helps.
I had the same error, maybe because of ubuntu upgrade from 18.04 to 20.04. Could not start mongod manually, the reason was absence of /data/db and its permissions. Then mongod would start manually but still errorring on mongodb.service. At the end reinstalling mongodb worked. It was a new install so did not care about backups.
$ sudo apt remove mongodb mongo-tools mongodb-clients mongodb-server mongodb-server-core
$ sudo apt autoremove
$ sudo rm -rf /var/lib/mongodb/
$ sudo apt install mongodb

Mongodb not able to start in Ubuntu 15.04

I have installed MongoDB 3.0.6 in Ubuntu 15.04 using the following commands.
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
echo "deb http://repo.mongodb.org/apt/debian wheezy/mongodb-org/3.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list
sudo apt-get update
sudo apt-get install -y mongodb-org
In the end in the installation process I got this:
Job for mongod.service failed. See "systemctl status mongod.service" and "journalctl -xe" for details.
invoke-rc.d: initscript mongod, action "start" failed.
dpkg: error processing package mongodb-org-server (--configure):
subprocess installed post-installation script returned error exit status 1
Setting up mongodb-org-mongos (3.0.6) ...
Setting up mongodb-org-tools (3.0.6) ...
dpkg: dependency problems prevent configuration of mongodb-org:
mongodb-org depends on mongodb-org-server; however:
Package mongodb-org-server is not configured yet.
dpkg: error processing package mongodb-org (--configure):
dependency problems - leaving unconfigured
No apport report written because the error message indicates its a followup error from a previous failure.
Processing triggers for systemd (219-7ubuntu6) ...
Processing triggers for ureadahead (0.100.0-19) ...
Errors were encountered while processing:
mongodb-org-server
mongodb-org
E: Sub-process /usr/bin/dpkg returned an error code (1)
After that when I start the server using the below command I get an error and the server is not started.
sudo service mongod start
And the error is
Job for mongod.service failed. See "systemctl status mongod.service" and "journalctl -xe" for details.
Now when I run:
sudo systemctl status mongod.service
I get the following:
mongod.service - LSB: An object/document-oriented database
Loaded: loaded (/etc/init.d/mongod)
Active: failed (Result: exit-code) since Thu 2015-10-08 13:46:08 IST; 22s ago
Docs: man:systemd-sysv-generator(8)
Process: 6604 ExecStart=/etc/init.d/mongod start (code=exited, status=1/FAILURE)
Oct 08 13:46:07 gariya-GA-A55M systemd[1]: Starting LSB: An object/document-oriented database...
Oct 08 13:46:07 gariya-GA-A55M mongod[6604]: * Starting database mongod
Oct 08 13:46:08 gariya-GA-A55M mongod[6604]: ...fail!
Oct 08 13:46:08 gariya-GA-A55M systemd[1]: mongod.service: control process exited, code=exited status=1
Oct 08 13:46:08 gariya-GA-A55M systemd[1]: Failed to start LSB: An object/document-oriented database.
Oct 08 13:46:08 gariya-GA-A55M systemd[1]: Unit mongod.service entered failed state.
Oct 08 13:46:08 gariya-GA-A55M systemd[1]: mongod.service failed.
Someone please help, I am new to Ubuntu and MongoDB and I am not getting what is the problem.
There is a log file: /var/log/mongodb/mongod.log
In the log I've found the following:
Failed to unlink socket file /tmp/mongodb-27017.sock errno: Operation not permitted
Most probably this was from previous MongoDB version. I deleted the file (rm -rf /tmp/mongodb-27017.sock) and finished the installation:
apt-get install mongodb-org
Now it works. :-)
Recently i ended up with same issue and i tried all the possible solutions explained up here. But it didn't worked out for me. I was getting the below error.
# sudo service mongod restart
Restarting mongod (via systemctl): Job for mongod.service failed because the control process exited with error code. See "systemctl status mongod.service" and "journalctl -xe" for details. [FAILED]
Eventually i figured out the solution which worked for me.
I went to /run/mongodb folder and deleted the file called mongod.pid. Then tried to restart the mongo using # sudo service mongod restart and it's worked!
My log said:
[initandlisten] Found an invalid index { v: 2, key: { version: 1 }, name: "incompatible_with_version_32", ns: "admin.system.version"
so:
$cd /var/lib
$sudo rm -rf ./mongodb
$sudo mkdir mongodb
$sudo chown -R mongodb.mongodb mongodb/
$sudo service mongod restart
and then all worked fine.
sudo nano /etc/systemd/system/mongodb.service
[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
[Service]
User=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf
[Install]
WantedBy=multi-user.target
sudo systemctl start mongodb
sudo systemctl status mongodb
sudo systemctl enable mongodb
Is job!
The first thing you should check is a mongodb log file located at /var/log/mongodb/mongod.log
Look for the line with status E containing words like 'failed', 'fatal', etc.
Make sure that the /data/db directory exists
Make sure your user have write privileges in that directory:
$ sudo chown id -u /data/db
Execute mongod
If you still have problems and if the file /data/db/mongod.lock exists, remove it and execute mongod --repair.
I removed the mongodb lock file-
sudo /data/db/mongod.lock
Then restarted the mongodb
sudo service mongod restart
This solved the issue.
I was having the same issue, after searching I got the answer that, when I executed
sudo service mongod restart
I changed the permissions from user mongodb to root of the database folder, so the service was not able to read the data.
So to solve I just reverted the permissions back to the user mongodb
sudo chown -R mongodb:mongodb /path-to-db-folder
sudo service mongod restart

MongoDB server won't start

I just installed Arch Linux today. I'm setting up my development stack, and I'm stuck with mongodb.
I have followed this wiki when installing:
https://wiki.archlinux.org/index.php/MongoDB#Installing_MongoDB
When I try to get into mongo console, I get this:
jan#arch:~$ mongo
MongoDB shell version: 2.4.5
connecting to: test
Tue Jul 9 19:38:13.365 JavaScript execution failed: Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:L112
exception: connect failed
jan#arch:~$
What am I missing here?
EDIT: I figured out when I run mongod directly from terminal, it starts server fine. However the systemctl start mongodb doesn't start it.
Okay, I've figured it out by scanning journalctl output:
ul 09 19:49:33 arch mongod[1122]: all output going to: /var/log/mongodb/mongod.log Jul 09 19:49:33 arch mongod[1122]: can't open [/var/log/mongodb/mongod.log] for log file: errno:13 Permission denied Jul 09 19:49:33 arch mongod[1122]: Bad logpath value: "/var/log/mongodb/mongod.log"; terminating.
So I searched for user that might be utilized by mongodb:
jan#arch:~$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
...
mongodb:x:998:2::/var/lib/mongodb:/bin/bash
And chowned required folders:
jan#arch:~$ sudo chown -R mongodb:x /var/log/mongodb/
jan#arch:~$ sudo chown -R mongodb:x /var/lib/mongodb/
Started it again:
jan#arch:~$ sudo systemctl start mongodb
It works now.
jan#arch:~$ mongo
MongoDB shell version: 2.4.5
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
>