On first deploy laravel app: SQLSTATE[HY000] [2002] Connection refused

Hello everyone,

I have set up a MySQL database in fly.io according to Use a MySQL Database · Fly Docs and can also access it locally with flyctl proxy 13306:3306 -a mysql-probe.

app = "mysql-probe"
kill_signal = "SIGINT"
kill_timeout = 5
primary_region = "ams"

app = """--datadir /data/mysql \
  --default-authentication-plugin mysql_native_password \
  --performance-schema=OFF \
  --innodb-buffer-pool-size 64M"""


  MYSQL_DATABASE = "probe"
  MYSQL_USER = "db"

  cpu_kind = "shared"
  cpus = 1
  memory_mb = 256

  image = "mysql:8.0.32"

In the second step, I try to connect my existing Laravel app to this DB and launch it. I have executed fly launch in the directory of my app. After adding the fly.toml according to Laravel and MySQL · Fly Docs it looks like this:

app = "vivo-probe"
primary_region = "ams"
console_command = "php /var/www/html/artisan tinker"

    NODE_VERSION = "18"
    PHP_VERSION = "8.2"

  APP_ENV = "production"
  APP_URL = "https://vivo-probe.fly.dev"
  LOG_CHANNEL = "stderr"
  LOG_LEVEL = "info"
  LOG_STDERR_FORMATTER = "Monolog\\Formatter\\JsonFormatter"
  SESSION_DRIVER = "cookie"
  DB_CONNECTION = "mysql"
  DB_HOST = "mysql-probe.internal"
  DB_DATABASE= "probe"

  internal_port = 8080
  force_https = true
  auto_stop_machines = true
  auto_start_machines = true
  min_machines_running = 0
  processes = ["app"]

  cpu_kind = "shared"
  cpus = 1
  memory_mb = 256

However, the attempt to fly deploy leads to an error:
SQLSTATE[HY000] [2002] Connection refused

Am I doing something wrong? I have tried to follow the instructions completely. I thought it was due to the missing tables and created them via SQL import. But there seems to be no connection at all.

How do I get on here? I have already searched here in the community and also via google.

Many thanks in advance for your help!


Perhaps you didn’t mention this for brevity but just in case …

Have you set the DB_USERNAME and DB_PASSWORD for that database in your Laravel app, as secrets? I see the other DB environment variables in the fly.toml.

Those would both need to be set as secrets (which in turn would make them available as environment variables) in order for the app to be able to connect. If you have, it’s not that :thinking:


yes, I set them:

I even reset the values via the GUI just in case. Since I can connect to the DB with these credentials in MySQL Workbench, they are therefore valid.

Hey there!
Sorry you’re having issues with deploying.
Just to be certain: are the Laravel and mysql applications in the same organization?
And if they are: can you try to run your migrations from the Laravel app, like this:

// In your Laravel root folder
fly ssh console
php artisan migrate

Here’s a few things to test out

:one: Use fly logs for the mysql app to ensure it’s started (seems likely since you can use fly proxy to connect to it, but maybe double check you’re connecting on port 13306 locally when proxying instead of accidentally connecting to a local mysql, which I tend to do :stuck_out_tongue: )
- The setup there is for a really small mysql instance, be sure it’s not crashing)

:two: Did you set a mysql user/password for the MySQL instance via the secrets/env? Specifically I haven’t seen a MYSQL_USER env var in your example (perhaps you set it as a secret?)

:three: Usually there’s more to a mysql error than just “connection refused” but perhaps not! “Connection refused” usually means the mysql instance isn’t reachable (like, not running) but could be a network thing there.

  1. Is the Laravel app in Fly set to connect to 13306 accidentally after some testing?
  2. Maybe SSH in (fly console ssh) , start a php artisan tinker session and output config('database') to see what settings it’s seeing there for the mysql connection.


Yes, both applications run at personal organization.

fly ssh console

throws me:

Error: app vivo-probe has no started VMs.
It may be unhealthy or not have been deployed yet.
Try the following command to verify:

fly status
// fly status:
  Name     = vivo-probe
  Owner    = personal
  Hostname = vivo-probe.fly.dev  
  Image    = -
  Platform = machines

Hi fideloper-fly,

That’s the last 2 lines of the log (exactly the same, I can see via GUI as Monitoring):

2024-01-17T11:09:20Z app[2865072a723648] ams [info]2024-01-17T11:09:20.571811Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
2024-01-17T11:09:20Z app[2865072a723648] ams [info]2024-01-17T11:09:20.572023Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.32'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.   

I have a database probe at local mysql, but it is filled with data. Via proxy-connect and MySQL workbench I could create and drop tables. Also my credentials locally aren’t the same.

According to the documentation I set DB_USERNAME as secret instead. Just now added MYSQL_USER as secret, with the right username. No effect.

  1. Where can Iset the port in my laravel app?
  2. fly console ssh doesn’t work, fly ssh console throws an error (see above reply).

Ahh, DB_USERNAME is a Laravel environment variable (it wouldn’t affect users in the MySQL VM).

However the MySQL official containers (being used here) needs a MYSQL_USER env var (docs).

This is used when the mysql image is first created, so setting it after the fact (after the mysql VM is already created) won’t work.

:point_right: I see MYSQL_USER = "db" in your fly,toml file for the MysQL app, so perhaps I just missed that, I’m not sure (was that an edit?). If that was always there, then ignore me on that point for sure :+1:

If you don’t want to destroy / recreate the database app (and the volume!), ad you can connect with user root, that should be just fine - either keep using root or create a new user via sql query.

If you haven’t set set a port in your Laravel app, don’t worry about it - it’s likely not the issue. That setting is via the DB_PORT env var tho.

Your laravel app isn’t running due to inactivity - if you send it a web request, it’ll turn back on, and then fly ssh console should work again.

Hope that helps!

I have found my mistake!

I have set a Route::redirect() in my app, which dynamically refers to the last data record of a table. Problem: the routes are all initialized when the application is bootstrapped, i.e. an attempt is made to access a table or DB that probably doesn’t even exist yet.

I have now intercepted the whole thing with try-catch, now this error no longer occurs.

Thanks for your advice anyway!

However, I now get a different error later in the procedure:
failed to solve: failed to compute cache key: "/app/public" not found

I have to say: The directory public is called www for various reasons. I have also already adjusted the Dockerfile below as follows:

COPY --from=node_modules_go_brrr /app/public /var/www/html/public-npm
RUN rsync -ar /var/www/html/public-npm/ /var/www/html/www/ \ #instead of /var/www/html/public/
    && rm -rf /var/www/html/public-npm \
    && chown -R www-data:www-data /var/www/html/www #instead of /var/www/html/public

Does anyone have any ideas?

1 Like

Habe auch hier den Fehler gefunden:
Auch /app/public muss in meinem Fall /app/www heißen.

Deployment abgeschlossen.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.