Laravel and MySQL
For data store, why not start with a relational database classic: MySQL. You can run your own MySQL Fly App, or set up a PlanetScale MySQL-compatible serverless database.
- Connect to your MySQL Fly App from a Laravel Fly App
- Connect to your MySQL Fly App from a local environment
[env]configuration in your Laravel application’s
fly.tomlfile to connect with your MySQL Fly App’s Fly .internal address:
[env] APP_ENV = "production" DB_CONNECTION = "mysql" DB_HOST = "<MYSQL Fly .internal Address>" DB_DATABASE= "<MYSQL_DATABASE>"
Then, set up your Laravel Fly App’s database username and password through Fly Secrets:
fly secrets set DB_USERNAME=<MYSQL_USER> DB_PASSWORD=<MYSQL_PASSWORD>
Finally deploy your Laravel Fly App changes with:
The MySQL instance you spun up in Fly.io “is closed to the public internet”, and can only be accessed by another application found in your Fly.io organization’s private network. You’ll need a way to tunnel into the network and finally connect to your MySQL instance.
In this guide you’ll tunnel to your MySQL instance through the use of
Open your MySQL application’s
fly.tomland take note of the following:
app = "<mysql-app-name>" [env] MYSQL_DATABASE = "<database-name>" MYSQL_USER = "<database-user>"
fly proxyto tunnel to your MySQL application:
fly proxy 3306 -a <mysql-app-name>
Finally, update your Laravel application’s local .env file with the values from your MySQL
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=<MYSQL_DATABASE> DB_USERNAME=<MYSQL_USER> DB_PASSWORD=<MYSQL_PASSWORD>
Once you’re setup with PlanetScale, connect your Laravel application in Fly.io through the following steps below:
- Get Laravel connection information from PlanetScale instance
- Connect from Laravel application in Fly.io
Once initialized, your database dashboard should have metrics and options like so:
- Click on the Connect button at the top right, this should provide a box of information for connecting with your PlanetScale database.
- First though, make sure to add a password, by clicking on the “New Password” button at the upper right corner. This should show you a new password afterwards.
- Next, select “Laravel” in the list labeled “Connect with”
Take note of the connection string provided and let’s move on!
[env]configuration in Laravel application’s
fly.tomlwith details from the PlanetScale connection string
[env] APP_ENV = "production" DB_CONNECTION = "mysql" DB_HOST = "<DB_HOST>" DB_DATABASE= "<DB_DATABASE>" MYSQL_ATTR_SSL_CA="/etc/ssl/certs/ca-certificates.crt"
Take note that the value for
MYSQL_ATTR_SSL_CAvaries depending on the Docker container used. For the default Docker container used by Fly.io, the above value is the path
Next, set up the database username and password through fly secrets:
fly secrets set DB_USERNAME=<DB_USERNAME> DB_PASSWORD=<DB_PASSWORD>
Finally deploy your changes with: