Hi!
I actually have a rough draft of a blog article I wrote on how to get MySQL running on Fly. You may as well see it first!
The gist of it is this: You make a second app that runs MySQL, so you’ll end up with 2 apps (one app to run Laravel, another to run MySQL).
The process:
- Create a Fly app for MySQL
- Configure the app
- Add app secrets (to set MySQL passwords)
- Create and configure a volume (to persist our database data)
- Update
fly.toml
- Deploy the app
Create the App
Here's what that looks like:
# Make a directory for the mysql app
mkdir my-mysql
cd my-mysql
# Run `fly launch` to create an app
fly launch
> Set an app name such as "my-mysql"
> Do not launch now if asked
Now we'll have a Fly application created named my-mysql
. We haven't launched anything yet!
Configure the App
Let's create a volume straight-away. If you don't create a volume, you'll lose all of your data on any deployment (or if we move your VM to another host if there are issues).
# This assumes we're in the same directory "my-mysql"
# alongside the fly.toml file
# Create a volume within our app "my-mysql"
# BE SURE TO SET THE REGION YOU WANT
fly volumes create mysqldata --region dfw --size 10
We also need to set some secrets required by the MySQL container:
# This assumes we're in the same directory "my-mysql"
# alongside the fly.toml file
# Set secrets:
# MYSQL_PASSWORD - password set for user $MYSQL_USER
# MYSQL_ROOT_PASSWORD - password set for user "root"
fly secrets set MYSQL_PASSWORD=password MYSQL_ROOT_PASSWORD=password
Finally, edit the fly.toml
file generated to look something like this:
app = "my-mysql"
kill_signal = "SIGINT"
kill_timeout = 5
[mounts]
source="mysqldata"
destination="/data"
[env]
MYSQL_DATABASE = "my_db"
MYSQL_USER = "my_user"
[build]
image = "mysql:8"
[experimental]
cmd = ["--default-authentication-plugin", "mysql_native_password", "--datadir", "/data/mysql"]
There's a few important things to note:
- We deleted the
[[services]]
block and everything under it. We don't need it!
- We added the
[build]
section to define a Docker image. We don't need to create a Dockerfile
of our own.
- The
[env]
section contains two not-so-secret environment variables that MySQL will need to initialize itself.
- We added the
[experimental]
section, which lets us set the command to run in the VM (just like Docker's CMD
).
- For MySQL 8, you'll want to use the
mysql_native_password
password plugin
- More importantly, we set MySQL's data directory to a subdirectory of our mounted volume
Mounting a disk in Linux often results in a lost+found
directory being created. However, MySQL won't initialize into a data directory unless it's completely empty. Therefore, we use a subdirectory of the mounted location: /data/mysql
.
Deploy the App
Now we can deploy the application!
But wait, there's one more thing. MySQL 8+ has higher baseline resource demands than MySQL 5.7.
If you're using MySQL 8, it's best to add some additional RAM to the VM:
# Give the vm 2GB of ram
fly scale memory 2048
And then we can deploy it:
fly deploy
You should now have an app running MySQL running!
Please let me know if you run into any issues!