By Josh
I am trying to run a node app and am getting mongoerror failed to connect to server [IP] on first connect
How do I solve this? I want to have a separate database server than my web server. I have tried public and private IP’s and neither worked.
When I do mongod it shows the database is running so that it out.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
By default, the MongoDB One-Click application is secured with a UFW-managed firewall. In order to allow your Node application to access the MongoDB instance, you must open a port in the firewall. This command will allow access from the Node app’s IP address and nowhere else:
- sudo ufw allow from node_ip_address to any port 27017
Additionally, MongoDB is configured to only listen on the local interface by default. To allow remote connections, modify the value of bindIp in /etc/mongod.conf If you do so, you are highly advised to first review the security checklist from the MongoDB documentation. In addition to enabling one of the forms of authentication supported by MongoDB.
The relevant section of the config file should look like:
# [ ... ]
net:
port: 27017
bindIp: 127.0.0.1,ip_of_mongo_droplet
# [ ... ]
For more information on running MongoDB, including enabling authentication, check out:
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.