You mentioned in the comments above that your database is hosted by IONOS, and your code is running externally from there.
Unfortunately, IONOS appears to block connections to their database products from external sources. Heroku is most definitely an external source. This fact is clearly documented on their help site:
Access to your IONOS MySQL database from your desktop computer or any other non-IONOS source is not possible:
In principle, you can only access your MySQL database through applications installed on your IONOS web space, e.g. with your own WordPress installation. Other access paths have been blocked for security reasons.
The way they accomplish this restriction is likely by not publishing public DNS records for their databases, which is why your code complains about not being able to resolve an address for it.
What you're asking for is not possible with this vendor without a special arrangement. If this is an absolute requirement, you should look for an alternative database host.