I connect my laravel app hosted to fly io to mysql db hosted on GCP and it time out in connect pls im new to fly and GCP
Hi @soloski123 !
Welcome to Fly.io!
First thing, can you try connecting to your MYSQL db hosted in GCP in your local environment? Here’s a guide you can read on that.
Once you can connect locally, I’m sure you’ll be able to connect from Fly as well. For configuring your Laravel Fly App to connect with the MYSQL db in GCP, you can read our documentation here.
Basically, make sure to revise your fly.toml file with correct configuration from your db:
[env] APP_ENV = "production" DB_CONNECTION = "mysql" DB_HOST = "<GCP_MYSQL_HOST_INFO>" DB_DATABASE= "<GCP_MYSQL_DATABASE>"
Then set your DB_USERNAME and DB_PASSWORD using fly secrets:
fly secrets set DB_USERNAME=<MYSQL_USER> DB_PASSWORD=<MYSQL_PASSWORD>
and make sure to deploy your changes with
One additional thing to consider (which would explain timeouts) is that it’s likely your database is currently not available to the public (ie allowing connections to it from any IP). As that’s not a good idea.
As such, even if you are using the correct hostname, user and secret password (set as described above), your Fly app would not be able to connect unless you allow access to it. However the problem with running an app on Fly is you don’t know what outgoing IP it will use to connect to your GCP database. And so you can’t whitelist [Fly IP] to allow only it to connect. You would need some kind of proxy. For example Google’s own: About the Cloud SQL Auth proxy | Cloud SQL for MySQL | Google Cloud
I was able to add my IP and the IP of the fly app to the cloud SQL instance whitelisting IP sections
I am able to connect to the MySQL instance from my local machine.
But still can’t connect to fly
@soloski123 Hi, It sounds like you have run into the problem I mentioned. You don’t know what outgoing IP Fly will use to connect to your database (the IP Fly shows you in its UI/CLI is the incoming one - the one you set e.g for your DNS to point to it). And so the connection is refused.