Question

How to query a DO-managed MySQL database from a DO-hosted Nextjs app

I have a managed MySQL database which I like to query directly from a Nextjs app which is hosted as DigitalOcean app. In my local development environment I can query the database directly and get the desired results, however, when I push the app to DigitalOcean apps the query in question fails. Most likely, I have not configured the environment properly. I have configured environment variables for DB_HOST, DB_PORT, DB_USER, DB_PASSWORD, and DB_NAME for which I used the values given in the managed database section. Moreover, I have added the app to the trusted sources for the database. In another attempt I also uploaded the SSL certificate and configured an environment variable for it. This also did not work. In the Nextjs app I use mysql2/promise to create the connection and next’s NextApiRequest and NextApiResponse as handler.


Submit an answer


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!

Sign In or Sign Up to Answer

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.

Bobby Iliev
Site Moderator
Site Moderator badge
June 23, 2024

Hey!

What is the exact error that you get when you try to deploy your app to the App Platform?

I recently answered a similar quesiton regarding mysql2/promise and a managed MySQL cluster:

https://www.digitalocean.com/community/questions/app-platform-service-connecting-to-manage-mysql-receives-self-signed-certificate-in-certificate-chain-error?comment=205074

The issue from the discussion above was fixed by actually storing the CA certificate as a file instead of passing it as a string:

What you could do is to add a command to the app that creates the certificate file upon runtime, such as echo $MYSQL_CA_CERT > ca_cert.cert && <original run command>. App Platform requires the original run time command to start the app upon runtime.

Then you can reference that file in the connection string, eg:

ca: fs.readFileSync('./ca_cert.cert'),

Let me know how it goes!

- Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

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

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.