By andytubeee
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!
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:
mongodb://username:password@host:port/database. However, the port number you’ve used (22) is typically for SSH connections, not MongoDB.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
27017 by default).sudo netstat -plntu to check the ports that are in use.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)./etc/mongod.conf) and update the bindIp under net settings to 0.0.0.0 (for all IPs) or to a specific IP address.27017).root has the necessary permissions on the diversable database.mongo shell. This will help you isolate the problem to either the database or the application code.root. Consider setting up a VPN or an SSH tunnel for more secure access if this is for production use..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.
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.
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.
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.
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.
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.
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.
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
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.