Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Persisting MySQL Data on Local Host for Docker and Kubernetes

Tech 2

Persist MySQL Data to Local Directory with Docker

To store MySQL data on your local host filesystem instead of an ephemeral container volume, simply bind mount a local directory to the container's default data path. See the official MySQL Docker image documentation for additional configuration details.

docker run -d \
  -e MYSQL_ROOT_PASSWORD=my_db_pass \
  --name mysql-service \
  -v /home/local-projects/backend/mysql/data:/var/lib/mysql \
  -p 3306:3306 \
  --restart=always \
  mysql:5.6

Single-Node MySQL Deployment on Kubernetes

Below is a working deployment manifest that uses a local host directory for persistent MySQL data storage:

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  labels:
    app: mysql
  name: mysql
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: mysql
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - env:
        - name: MYSQL_ROOT_PASSWORD
          value: "my_db_pass"
        image: mysql:8.0
        imagePullPolicy: IfNotPresent
        name: mysql
        ports:
        - containerPort: 3306
          hostPort: 3306
          name: mysql-port
          protocol: TCP
        readinessProbe:
          failureThreshold: 5
          initialDelaySeconds: 30
          periodSeconds: 10
          successThreshold: 1
          tcpSocket:
            port: 3306
          timeoutSeconds: 1
        volumeMounts:
        - mountPath: /var/lib/mysql
          name: mysql-local-data
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
      volumes:
      - hostPath:
          path: /opt/k8s/mysql/data
          type: DirectoryOrCreate
        name: mysql-local-data

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.