Make sure you can connect to the database with the mysql command using your credentials from the host running the container. If you can’t, fix that first.
Usually connection issues are due to an incorrect username and password, missing grants in the MySQL server, or missing firewall rules/security policy rules/network ACLs between the client and server. The MySQL server log can help diagnose these issues.
For security, do NOT expose your MySQL server to the public Internet. Make sure only IPs that need access to the database server can access the port.