How to Install MySQL 8 in Docker on AlmaLinux 8 with Existing MariaDB


Overview

On AlmaLinux 8 systems, MariaDB is commonly installed by default, especially when using hosting control panels such as Webmin or cPanel. In scenarios such as application testing, version compatibility checks, or database migration, it is often necessary to run MySQL 8 in parallel with an existing MariaDB installation.

Replacing MariaDB with MySQL is not recommended on production systems due to package conflicts and dependency risks.
A safer and fully isolated approach is to deploy MySQL 8 using Docker containers.

This document describes how to install Docker on AlmaLinux 8 and run MySQL 8 in a container while keeping MariaDB fully operational on the host system.


Use Cases


Prerequisites


Step 1: Verify Existing MariaDB Installation

Before proceeding, confirm that MariaDB is active and listening on its default port.

Check MariaDB service status

systemctl status mariadb

This confirms that the MariaDB service is running and not reporting startup errors.


Check MariaDB version

mariadb --version

This command displays the installed MariaDB client version.


Verify listening port

sudo ss -tulpn | grep 3306

MariaDB should be listening on TCP port 3306.
MySQL 8 will be configured to use a different port to avoid conflicts.


Step 2: Update System Packages

Ensure the system is fully up to date.

sudo dnf update -y

Install required dependencies used by Docker:

sudo dnf install -y yum-utils device-mapper-persistent-data lvm2

Step 3: Install Docker on AlmaLinux 8

Add the official Docker repository

sudo dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

AlmaLinux is binary-compatible with CentOS, so the CentOS Docker repository is used.


Install Docker Engine and CLI tools

sudo dnf install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

This installs Docker Engine, client utilities, container runtime, and Compose support.


Enable and start Docker

sudo systemctl enable docker
sudo systemctl start docker

Verify Docker installation

docker run hello-world

Successful execution confirms Docker is functioning correctly.


Step 4: Create Persistent Storage for MySQL 8

Docker volumes ensure database data persists across container restarts or recreation.

docker volume create mysql8_data

Verify volume creation:

docker volume ls

Step 5: Pull the MySQL 8 Docker Image

Download the official MySQL 8 image from Docker Hub.

docker pull mysql:8.0

Verify the image is available locally:

docker images | grep mysql

Step 6: Run MySQL 8 Container

MariaDB occupies port 3306, therefore MySQL 8 will be exposed on 3307.

docker run -d \
--name mysql8-container \
--restart unless-stopped \
-p 3307:3306 \
-v mysql8_data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=StrongRootPassword123! \
-e MYSQL_DATABASE=default_db \
-e MYSQL_USER=app_user \
-e MYSQL_PASSWORD=StrongUserPassword123! \
mysql:8.0 \
--character-set-server=utf8mb4 \
--collation-server=utf8mb4_unicode_ci

Parameter explanation


Step 7: Verify MySQL 8 Container Status

List running containers

docker ps

The output should show mysql8-container in a running state.


Review MySQL startup logs

docker logs mysql8-container

Successful initialization ends with:

MySQL init process done. Ready for start up.

Step 8: Connect to MySQL 8

Connect from inside the container

docker exec -it mysql8-container mysql -u root -p

Run validation queries:

SHOW DATABASES;
SELECT VERSION();
EXIT;

Connect from the host system

Install the MySQL client if not present:

sudo dnf install -y mysql

Connect using the mapped port:

mysql -h 127.0.0.1 -P 3307 -u root -p

Step 9: Firewall Configuration

If firewalld is enabled, allow access to port 3307.

sudo firewall-cmd --state

Add firewall rule:

sudo firewall-cmd --permanent --add-port=3307/tcp
sudo firewall-cmd --reload

Verify open ports:

sudo firewall-cmd --list-ports

Step 10: Webmin Integration (Optional)

Webmin can manage both databases independently.

Add MySQL 8 under:

Webmin → Un-used Modules → MySQL Database Server


Step 11: Backup and Restore MySQL 8

Backup all databases

docker exec mysql8-container mysqldump -u root -pStrongRootPassword123! --all-databases > mysql8_backup.sql

Restore backup

docker exec -i mysql8-container mysql -u root -pStrongRootPassword123! < mysql8_backup.sql

Step 12: Container Management Commands

docker start mysql8-container
docker stop mysql8-container
docker restart mysql8-container
docker stats mysql8-container
docker inspect mysql8-container

Security Considerations

docker pull mysql:8.0

Conclusion

By deploying MySQL 8 inside Docker, it is possible to run MySQL and MariaDB simultaneously on AlmaLinux 8 without conflicts.

This approach provides:

This configuration is suitable for development, testing, and controlled production scenarios where MySQL 8 is required.