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


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:

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

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

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

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel