Search code examples
mongodbvagrantshdebian-buster

No connection to Mongo shell


This is not a question, but a post to help others out. I see alot of complaints on SO about no connection to mongodb.

I am building a Debian 10 box with MongoDB 4.2. Install of base MongoDB package went smooth. I have no firewall set and no private ip network set (yet). My issue started when I attempted to use the mongo shell in a sh script to insert a new admin user. I was always getting stopped with an error saying no connection to mongodb on 127.0.0.1:27017. But when I would SSH into Debian box, I could connect to mongo shell fine. After two days of struggling, I finally installed a "sleep 5" statement between "systemctl start mongod" statement and the "mongo admin --eval 'db.createUser(....". I have proven that I can destroy the vagrant/virtual box, issue a vagrant up command with a good automatic connection results everytime now. Vagrant (virtualbox) is being used on my local windows machine. My assumption is that mongodb was still trying to start when the script hit the createUser statement.


Solution

  • After two days of struggling, I finally installed a "sleep 5" statement between "systemctl start mongod" statement and the "mongo admin --eval 'db.createUser(....". I have proven that I can destroy the vagrant/virtual box, issue a vagrant up command with a good automatic connection results everytime now. Vagrant (virtualbox) is being used on my local windows machine. My assumption is that mongodb was still trying to start when the script hit the createUser statement. The problem that I was encountering only appeared when I was developing a local instance of mongodb. When I deployed my build script to digitalocean, I did not encounter anything that needed a time delay to allow build script to finish that step.