Report this

What is the reason for this report?

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

Posted on June 21, 2024

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.



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.

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

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.