Report this

What is the reason for this report?

Accessing MongoDB hosted on DigitalOcean with Next.js Web App

Posted on January 5, 2022

I am using Mongo URI syntax in my .env.local file:

mongodb://root:<password>@<ip>:22/diversable

However, this method doesn’t work and here is the error code I am getting:

MongoServerSelectionError: Invalid message size: 759714643, max allowed: 67108864
    at Timeout._onTimeout (/Users/andrew/Documents/Projects/ddodir-next/node_modules/mongodb/lib/sdam/topology.js:330:38)
    at listOnTimeout (node:internal/timers:557:17)
    at processTimers (node:internal/timers:500:7) {
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map(1) { '159.203.4.243:22' => [ServerDescription] },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    logicalSessionTimeoutMinutes: undefined
  }
}


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!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there,

The error you’re encountering when trying to connect to MongoDB hosted on DigitalOcean from your Next.js application seems to be due to an incorrect MongoDB URI format, specifically the port number.

Here are the steps to troubleshoot and resolve this issue:

1. Correct the MongoDB URI

  • The MongoDB URI format should be mongodb://username:password@host:port/database. However, the port number you’ve used (22) is typically for SSH connections, not MongoDB.
  • The default port for MongoDB is 27017. Unless you’ve specifically configured MongoDB to use a different port, you should use this default port in your URI.

Update your URI to something like this:

mongodb://root:<password>@<ip>:27017/diversable

2. Ensure MongoDB is Listening on the Correct Port

  • Log in to your DigitalOcean droplet and check if MongoDB is running and listening on the correct port (27017 by default).
  • You can use a command like sudo netstat -plntu to check the ports that are in use.

3. Configure MongoDB to Allow Remote Connections

  • By default, MongoDB is configured to listen to connections only from localhost. If you haven’t already, you’ll need to configure MongoDB to accept connections from your Next.js application’s IP address or from all IP addresses (which is less secure).
  • Modify the MongoDB configuration file (usually located at /etc/mongod.conf) and update the bindIp under net settings to 0.0.0.0 (for all IPs) or to a specific IP address.

4. Check Firewall Settings

  • Ensure that your DigitalOcean droplet’s firewall allows incoming connections on the MongoDB port (usually 27017).
  • You can modify the firewall settings using DigitalOcean’s control panel or via the command line on your droplet.

5. Verify Database Credentials

  • Double-check that the username, password, and database name in your MongoDB URI are correct. Ensure that the user root has the necessary permissions on the diversable database.

6. Test the Connection Locally

  • Before integrating it into your Next.js application, test the connection to your MongoDB instance using a MongoDB client like MongoDB Compass or the mongo shell. This will help you isolate the problem to either the database or the application code.

7. Secure Your Database

  • Exposing your MongoDB directly to the internet can be risky, especially with a username like root. Consider setting up a VPN or an SSH tunnel for more secure access if this is for production use.
  • Also, ensure that your MongoDB instance is secured with strong passwords and appropriate user permissions.

8. Update Your Next.js Application

  • After making these changes, update the MongoDB URI in your .env.local file in your Next.js project and test the connection again.

After that you should be able to successfully connect your Next.js application to MongoDB hosted on DigitalOcean.


On another note, if you’re finding the setup and maintenance of a self-hosted MongoDB instance challenging, you might want to consider using a Managed MongoDB service, like DigitalOcean’s Managed Databases for MongoDB. Managed database services simplify much of the work involved in running a database securely and efficiently.

Benefits of Using a Managed MongoDB Cluster:

  1. Automated Backups and Recovery: Managed services typically include automated backup solutions, ensuring that your data is safely backed up and can be quickly restored in case of data loss.

  2. Scaling Made Easy: As your application grows, a managed service can seamlessly handle scaling the database. You can increase storage, memory, and compute resources with minimal downtime.

  3. Maintenance and Updates: The service provider takes care of routine maintenance, updates, and patches, ensuring that your database is always running the latest, most secure version.

  4. Performance Monitoring: Managed services often come with built-in monitoring and alerting tools, giving you insights into the performance of your database and helping you to identify and troubleshoot issues quickly.

  5. Security: Managed databases are configured with best practices in mind, offering robust security features like network isolation, encryption at rest and in transit, and more.

  6. Reduced Operational Overhead: By offloading database management tasks, your team can focus more on development and less on the operational aspects of running a database.

  7. Expert Support: You also get access to expert support from the service provider, which can be invaluable, especially if your team lacks deep database administration expertise.

Opting for a managed MongoDB solution can provide peace of mind and allow you to focus on building and improving your application, while the database’s reliability and performance are taken care of by the service provider.

Best,

Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.