Mongo client cannot connect on first try in bash script - bash

I'm working on a bash script for automatic MongoDB server installation and user creation.
#!/bin/bash
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | tee /etc/apt/sources.list.d/mongodb.list
apt-get update
apt-get install -y mongodb-org
update-rc.d mongod defaults
sed -i 's/^auth = true/#auth = true/g' /etc/mongod.conf
sed -i 's/^#noauth = true/noauth = true/g' /etc/mongod.conf
service mongod restart
mongo user_creation.js
The script is run on a clean precise64 Vagrant VM. Add the Debian/Ubuntu repo to install the latest version (we're on Ubuntu 12.04 LTS, the maintained version is 2.0.X), add server to startup, set some variables in the MongoDB config file. Simple stuff really, however, the last line fails each and every time.
MongoDB shell version: 2.6.4 connecting to: test 2014-10-03T12:34.:56.000+0000
warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2014-10-03T12:34.:56.000+0000 Error: couldn't connect to server 127.0.0.1:27017
(127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146
exception: connect failed
I thought maybe the server needed some time to load data or some other kind of initialization between its restart and the client connection so I tried putting a sleep 30 between the last two instructions : didn't work. However, removing the mongo execution from the script and starting it manually works like a charm every time! The .js is not the problem here, I've tested it and it's working alright.
Does anyone have any idea why the mongo cannot connect on its first try and how I could fix this? For now, I'll settle with this terrible (working) hack, but I'd rather have a cleaner, hackless script:
while :
do
RESULT=`mongo --eval "1;"`
echo $RESULT
if [[ "${RESULT:-null}" == *Failed\ to\ connect* ]]
then
sleep 1
else
mongo root_creation.js
break;
fi
done
It's ugly but after 1 to 10 tries, it ends up connecting and working perfectly.

Related

OpenConnect "must be running as root" in Gitlab CI/CD

I'm trying to get my Continuous Delivery working and subsequently uploading binaries to a company server, which is only accessible through VPN connection.
The problem is, every single time I try it, I'm getting the following error:
Connected as 158.196.194.120 + 2001:718:1001:111::7/64, using SSL
DTLS handshake timed out
DTLS handshake failed: Resource temporarily unavailable, try again.
Failed to bind local tun device (TUNSETIFF): Operation not permitted
To configure local networking, openconnect must be running as root
See http://www.infradead.org/openconnect/nonroot.html for more information
Set up tun device failed
Unknown error; exiting.
The strange thing is, that my code uses sudo explicitly in .gitlab-ci.yml, so I'd expect it to have all the rights.
deploy_spline:
stage: deploy
image: martinbeseda/lib4neuro-ubuntu-system-deps:latest
dependencies:
- test_spline
before_script:
- echo "DEPLOY!"
- apt-get -y install lftp openconnect sudo
script:
- mkfifo mypipe
- export USER=${USER}
- echo "openconnect -v --authgroup VSB -u ${USER} --passwd-on-stdin vpn.vsb.cz < mypipe &" > vpn.sh
- chmod +x vpn.sh
- sudo ./vpn.sh
- echo "${PASS}">mypipe
- lftp -u ${USER},${PASS} sftp://moldyn.vsb.cz:/moldyn.vsb.cz/www/releases -e "put build/SSR1D_spline.out; exit"
So, do you know, what's wrong with my code? Or is it some GitLab CD specific problem?
The Gitlab CI runner needs to run in privileged mode to bind the tunnel interface. Check your /etc/gitlab-runner/config.toml file and make sure that your runner has privileged set to true.
[[runners]]
name = "privileged runner"
...
[runners.docker]
privileged = true
Without that setting, the build container doesn't have the ability to bind the interface, even as root.

Error with starting PostgreSQL database on macOS

I am running the latest version of macOS Sierra and I installed PostgreSQL via brew. Then I ran the command:
pg_ctl -D /Users/tmo/PSQL-data -l logfile start
but received for output:
waiting for server to start..../bin/sh: logfile: Permission denied
stopped waiting
pg_ctl: could not start server
Examine the log output.
EDIT: After restarting my operating system and rerunning the command... I'm now receiving a slightly modified output... but the modification is significant.
waiting for server to start.... stopped waiting
pg_ctl: could not start server
Examine the log output.
Where is the "log output" stored?
How do I make this command work?
The problem could be one of two things, that I can see:
A typo in your database path:
/Users/tmo/PSQL-data --> /Users/tmp/PSQL-data
If the above was just a transcription error, I would guess that your postgres user doesn't have write access to the directory where you are setting the logfile. The argument following the -l switch tells PG where to save the logfile. When you don't provide the -l switch with a path, but just a filename, it will use the same dir you use to specify the database cluster (with the -D flag). So in this case, PG is trying to write to /Users/tmp/PSQL-data/logfile, and getting a permission error.
To fix this, I would try:
If the directory /Users/tmp/PSQL-data/ doesn't exist:
sudo mkdir /Users/tmp/PSQL-data
Then create the logfile manually:
sudo touch /Users/tmp/PSQL-data/logfile
Then make the postgres user own the file (I'm assuming user is postgres here)
sudo chown postgres /Users/tmp/PSQL-data/logfile
Try again, and hopefully you can launch the server.
Caveat: I'm not a macOS user, so I'm not sure how the /tmp folder behaves. If it is periodically cleared, you may want to specify a different logfile location, so that you don't need to create and chown the file each time you need to launch the cluster.

Mongo database cannot start

Hello everyone all help is much appreciated. Using MongoDB for the first time, I usually use postgresql. Cannot get any database action, including generating models. Continually get this error:
Mongo::ConnectionFailure: Failed to connect to a master node at localhost:27017
The internet says to try these, none of which work:
1-
sudo rm /var/lib/mongodb/mongod.lock
sudo -u mongodb mongod -f /etc/mongodb.conf --repair
sudo start mongodb
sudo status mongodb
This returns the error that
rm: /var/lib/mongodb/mongod.lock: No such file or directory
2- This is in a file I put in the initialize folder
require 'rubygems'
require 'mongo'
db = Mongo::Connection.new.db("mydb")
db = Mongo::Connection.new("localhost").db("mydb")
db = Mongo::Connection.new("localhost", 27017).db("mydb")
The problem seems to stem from a path, which I have no idea how it works or how to alter it, so if the advice is to alter paths and folders etc. please be very specific. Thank you guys so much.
The mongo daemon has not successfully started.
Usually you do
sudo mongod
Make sure you can connect to the database by typing mongo on the command line which should connect you to the daemon running on 27017.
Then try running your ruby code again.

install mongoDB (child process failed, exited with error number 100)

I tried to install mongoDB on my macbook air.
I've downloaded zipped file from official website and extract that file and move to root directory.
After that, under that directory, I've made /data/db and /log folder.
Here is my mongodb.config which describes the basic config for my DB.
dbpath = /mongodb/data/db
logpath = /mongodb/log/mongo.log
logappend = true
#bind ip = 127.0.0.1
port = 27017
fork = true
rest = true
verbose = true
#auth = true
#noauth = true
Additionally, I want to know what the # means in the config file.
I put this file to /mongodb/bin, /mongodb is the directory I extracted the files into.
I opened terminal and entered ./mongod --config mongodb.config and I got this back.
Juneyoung-ui-MacBook-Air:bin juneyoungoh$ ./mongod --config mongodb.config
about to fork child process, waiting until server is ready for connections.
forked process: 1775
all output going to: /mongodb/log/mongo.log
ERROR: child process failed, exited with error number 100
How can I handle this error and what this means?
The data folders you created were very likely created with sudo, yes? They are owned by root and are not writable by your normal user. If you are the only user of your macbook, then change the ownership of the directories to you:
sudo chown juneyoungoh /data
sudo chown juneyoungoh /data/db
sudo chown juneyoungoh /data/log
If you plan on installing this on a public machine or somewhere legit, then read more about mongo security practices elsewhere. I'll just get you running on your macbook.
I had a similar issue and it was not related to any 'sudo' problem. I was trying to recover from a kernel panic!
When I look at my data folder I found out a mongod.lock file was there. In my case this page helped a lot: http://docs.mongodb.org/manual/tutorial/recover-data-following-unexpected-shutdown/. As they explain,
if the mongod.lock is not a zero-byte file, then mongod will refuse to start.
I tested this solution in my environment and it works perfectly:
Remove mongod.lock file.
Repair the database: mongod --dbpath /your/db/path --repair
Run mongod: mongod --dbpath /your/db/path
There was the same problem on my machine. In the log file was:
Mon Jul 29 09:57:13.689 [initandlisten] ERROR: Insufficient free space for journal file
Mon Jul 29 09:57:13.689 [initandlisten] Please make at least 3379MB available in /var/mongoexp/rs2/journal or use --smallfiles
It was solved by using mongod --smallfiles. Or if you start mongod with --config option than in a configuration file disable write-ahead journaling by nojournal=true (remove the beginning #). Some more disk space would also solve the above problem.
It's because you probably didn't shutdown mongodb properly and you are not starting mongodb the right way. According your mongodb.config, you have dbpath = /mongodb/data/db - so I assume you created the repository /mongodb/data/db? Let me clarify all the steps.
TO START MONGODB
In your mongodb.config change the dbpath = /mongodb/data/db to dbpath = /data/db. On your terminal create the db repository by typing: mkdir /data/db. Now you have a repository - you can start your mongo.
To start mongo in the background type: mongod --dbpath /data/db --fork --logpath /dev/null.
/data/db is the location of the db.
--fork means you want to start mongo in the background - deamon.
--logpath /dev/null means you don't want to log - you can change that by replacing /dev/null to a path like /var/log/mongo.log
TO SHUTDOWN MONGODB
Connect to your mongo by typing: mongo and then use admin and db.shutdownServer(). Like explain in mongoDB
If this technique doesn't work for some reason you can always kill the process.
Find the mongodb process PID by typing: lsof -i:27017 assuming your mongodb is running on port 27017
Type kill <PID>, replace <PID> by the value you found the previous command.
Similar issue with the same error - I was trying to run the repair script
sudo -u mongodb mongod -f /etc/mongodb.conf --repair
Checked ps aux | grep mongo and see that the daemon was running. Stopped it and then the repair script run without an issue.
Hope that could be helpful for someone else.
I had the same error on linux (Centos) and this worked for me
Remove mongod.lock from the dbpath
$ rm /var/lib/mongo/mongod.lock
Repair the mongod process
$ mongod --repair
Run mongod config
$ mongod --config /etc/mongod.conf
I had the same error. I ran it interactively to see the log.
2014-10-21T10:12:35.418-0400 [initandlisten] ERROR: listen(): bind() failed errno:48 Address already in use for socket: 0.0.0.0:27017
Then I used lsof to find out which process was using my port.
$ lsof -i:27017
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mongod 2106 MYUSERID 10u IPv4 0x635b71ec3b65b4a1 0t0 TCP *:27017 (LISTEN)
It was a mongod that I had forked previously and forgot to turn off (since I hadn't seen it running in my bash window).
Simply killing it by running kill 2106, enabled my process to run without the error 100.
Generally, this error comes when the mongod.conf file is not able to
find a certain path for
Database store
or log store
or maybe processid store
or maybe it's not getting the file permission to access the config directories and files which has been declared in mongod.conf
to resolve this error we need to observe the log generated by the MongoDB
it will clearly indicate whether which file or directory you MongoDB is not able to access
the above error may look like below screenshot
create folder "data" and "db" inside it, in "/" path of your server.
actually you should create or modify permissions of folder that the data is going to be stored!

How to resolve this PostgreSQL error on OS 10.6 (Snow Leopard)

I followed the instructions for setting up postgresql from this site
All seems to go fine until I try:
createuser --superuser myname -U
postgres
I get the following exception:
createuser: could not connect to
database postgres: could not connect
to server: No such file or directory
Is the server running locally and
accepting connections on Unix domain
socket "/tmp/.s.PGSQL.5432"?
For the life of me I can't figure out how to resolve this. Any ideas???
I had to remove the existing postgres user before doing the install.
Perhaps you moved your postgres data directory after you installed postgres using macports
Find where your launchctl startup script is located.
ps -ef | grep postgres
Outputs
0 54 1 0 0:00.01 ?? 0:00.01 /opt/local/bin/daemondo --label=postgresql84-server --start-cmd /opt/local/etc/LaunchDaemons/org.macports.postgresql84-server/postgresql84-server.wrapper start ; --stop-cmd /opt/local/etc/LaunchDaemons/org.macports.postgresql84-server/postgresql84-server.wrapper stop ; --restart-cmd /opt/local/etc/LaunchDaemons/org.macports.postgresql84-server/postgresql84-server.wrapper restart ; --pid=none
So I edit
sudo vim /opt/local/etc/LaunchDaemons/org.macports.postgresql84-server/postgresql84-server.wrapper
And find the line
Start() {
su postgres -c "${PGCTL} -D ${POSTGRESQL84DATA:=/opt/local/var/db/postgresql84/wrong_place} start -l /opt/local/var/log/postgresql84/postgres.log"
}
Ahh.. my data directory is in the wrong place. I fix it by changing
/opt/local/var/db/postgresql84/wrong_place
to
/opt/local/var/db/postgresql84/right_place
for both the start and stop command.
Did you install the postgresql84-server port? If so, did you start the server:
$ sudo port load postgresql84-server
If you did both of those, I've noticed that sometimes the MacPorts daemon handler (daemondo) doesn't start handling requests for PostgreSQL until you restart your machine. (This only happens the first time it is started; subsequent attempts should work fine.)

Resources