Cloud Native 9 min read

Understanding Linux Storage, LVM, and Docker Volumes for Persistent MySQL Data

This guide explains the fundamentals of Linux host file systems, LVM logical volumes, and Docker storage drivers, showing step‑by‑step how to format, mount, and persist MySQL data using Docker volumes and bind mounts on AlmaLinux servers.

IT Xianyu
IT Xianyu
IT Xianyu
Understanding Linux Storage, LVM, and Docker Volumes for Persistent MySQL Data

1. Why Understand Storage and File Systems?

Imagine a cluttered storage room where you can’t find anything; the same happens on Linux if you don’t grasp file systems, mounts, and storage management, leading to data loss, chaos, and poor performance.

In our scenario three AlmaLinux hosts (master, node1, node2) run MySQL master‑slave containers. The host file system, Docker volume management, and container storage drivers must be clearly separated, like a landlord, warehouse manager, and tenant.

2. Component Relationship Overview

Host File System (Ext4 / XFS) – the foundation and room structure, determines performance and stability.

LVM (Logical Volume Manager) – a flexible, dynamically expandable storage cabinet.

Docker Storage Driver (overlay2, etc.) – isolates read/write layers for each container, like breathable wallpaper.

Docker Volume – a dedicated storage box requested from the host for persistent data.

MySQL Data Directory inside the Container – the actual place where database files reside, usually mapped to a Docker volume or host directory.

The relationship can be visualized as: landlord (host FS) provides rooms, warehouse manager (LVM) allocates space, tenant (Docker container) isolates storage with drivers, and the tenant’s data (database files) lives in its dedicated box (volume).

3. Host File System and Mounting

3.1 View Current File System Type

Execution scenario: Run on any AlmaLinux host (master/node1/node2).
df -T

The -T flag shows the type, e.g., ext4 or xfs .

Sample output:

Filesystem     Type 1K-blocks    Used Available Use% Mounted on
/dev/sda2      xfs 20511356 5091536 15419820  25% /

If you see bash: df: command not found , install coreutils:

sudo yum install -y coreutils

3.2 Mount a New Disk or Partition

Assume a new disk /dev/sdb is added.

Format as XFS (or Ext4) sudo mkfs.xfs /dev/sdb # XFS # or sudo mkfs.ext4 /dev/sdb # Ext4 If mkfs.xfs: command not found , install XFS tools: sudo yum install -y xfsprogs

Create a mount point sudo mkdir -p /data/mysql # mount point for MySQL data

Mount the disk sudo mount /dev/sdb /data/mysql

Enable automatic mounting at boot Edit /etc/fstab and add: /dev/sdb /data/mysql xfs defaults 0 0 Then run: sudo mount -a

4. LVM: Adding Elasticity to Storage

LVM allows flexible expansion, snapshots, and thin provisioning—ideal for databases.

4.1 Install LVM Tools

Execution scenario: Host terminal.
sudo yum install -y lvm2

If pvcreate: command not found , the lvm2 package is missing.

4.2 Basic LVM Operations (example using /dev/sdc to create a 50 GB logical volume for MySQL)

Initialize physical volume sudo pvcreate /dev/sdc

Create a volume group sudo vgcreate vg_mysql /dev/sdc

Create a logical volume sudo lvcreate -L 50G -n lv_mysql vg_mysql

Format the logical volume as XFS sudo mkfs.xfs /dev/vg_mysql/lv_mysql

Mount it to /data/mysql sudo mkdir -p /data/mysql echo '/dev/vg_mysql/lv_mysql /data/mysql xfs defaults 0 0' | sudo tee -a /etc/fstab sudo mount -a Now /data/mysql has 50 GB of flexible storage.

5. Docker Volumes: Persistent Storage for Containers

5.1 Why Not Use the Container’s Internal Directory?

Containers are short‑lived; deleting a container removes its internal data. Use a volume or bind mount instead.

5.2 Create and Use a Docker Volume

Execution scenario: Host terminal (all three servers can run the same commands).
# Create a named volume
docker volume create mysql-data

# Run a container with the volume mounted
docker run -d \
  --name mysql-master \
  -v mysql-data:/var/lib/mysql \
  mysql:8.0

The -v mysql-data:/var/lib/mysql flag maps the volume to MySQL’s data directory.

5.3 List and Clean Up Volumes

# List all volumes
docker volume ls

# Inspect a specific volume
docker volume inspect mysql-data

# Remove unused volumes
docker volume prune

If you encounter permission errors, prepend sudo or add your user to the docker group.

6. Combining LVM and Docker Volumes (Advanced)

To use a logical volume directly as the backing store for a Docker volume, first mount the LV (already done in step 4) and then bind‑mount it into the container:

docker run -d \
  --name mysql-node1 \
  -v /data/mysql:/var/lib/mysql \
  mysql:8.0

This setup writes container data straight to the LVM‑managed volume, giving you both elasticity and container isolation.

All commands indicate whether they run on the host or inside the container and include troubleshooting tips for “command not found” errors.

Keep experimenting and you’ll master Linux storage and container persistence!

DockerLinuxMySQLStorageLVMcontainersVolumes
IT Xianyu
Written by

IT Xianyu

We share common IT technologies (Java, Web, SQL, etc.) and practical applications of emerging software development techniques. New articles are posted daily. Follow IT Xianyu to stay ahead in tech. The IT Xianyu series is being regularly updated.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.