Skip to content

Deploying a MySQL Database

MySQL is a widely used open-source relational database management system known for its reliability, performance, and ease of use. It is ideal for applications that require structured data storage, ACID compliance, and robust transaction support.

This guide explains how to deploy a MySQL database to Klutch.sh using a Dockerfile, including installation, sample configuration, and persistent volume setup.

Prerequisites

  • Docker installed (Download)
  • Git and GitHub account
  • Klutch.sh account

Getting Started: Sample Dockerfile

Create a Dockerfile in your project root:

FROM mysql:8.4
# Set environment variables (can be overridden in Klutch.sh UI)
ENV MYSQL_DATABASE=mydb
ENV MYSQL_USER=myuser
ENV MYSQL_PASSWORD=mypassword
ENV MYSQL_ROOT_PASSWORD=rootpassword
# Expose default MySQL port
EXPOSE 3306
# Optional: Add custom initialization scripts
# COPY ./init.sql /docker-entrypoint-initdb.d/

Sample Usage

You can connect to your MySQL database from any app using the following connection string:

mysql://myuser:mypassword@<APP_HOST>:3306/mydb

Replace <APP_HOST> with the internal Klutch.sh service hostname or external IP as needed.


Deploying with Persistent Volumes

To ensure your database data persists across deployments and restarts, attach a persistent volume in Klutch.sh:

  1. Push your code (with Dockerfile) to a GitHub repository.
  2. Log in to Klutch.sh.
  3. Create a new project.
  4. Create a new app:
    • Select your MySQL GitHub repository and branch
    • Choose the Dockerfile option when prompted
    • Set service details and environment variables as needed (MYSQL_DATABASE, MYSQL_USER, MYSQL_PASSWORD, MYSQL_ROOT_PASSWORD)
    • Attach a persistent volume:
      • In the “Volumes” section, add a new volume (e.g., mysqldata)
      • Mount path: /var/lib/mysql
    • Configure region, compute, and other settings as needed
  5. Click “Create” to deploy. Klutch.sh will build your Docker image, attach the volume, and start your MySQL database.

Resources


Deploying MySQL to Klutch.sh with a Dockerfile and persistent volumes ensures your data is safe and your database is production-ready.