Deploying a MySQL Database Using Podman

Deploying MySQL Using Podman

In this comprehensive guide, we’ll walk through the process of deploying a MySQL database using Podman, covering installation, configuration, and best practices.

Table of Contents

Introduction

In today’s dynamic landscape of software development and deployment, containerization has become a cornerstone technology. Among the plethora of container management tools available, Podman stands out for its lightweight nature, security features, and ability to run containers without a daemon. Leveraging Podman, developers and system administrators can easily deploy and manage various applications, including databases like MySQL.

Deploying MySQL using Podman: Installing Podman

Before embarking on our MySQL deployment journey, let’s ensure Podman is installed on your system. Podman is widely supported across Linux distributions and can be easily installed via package managers. For instance, on Ubuntu, you can install Podman using the apt package manager with the following command:

				
					$ sudo apt install podman -y
				
			

For users on other distributions, refer to their respective package managers or consult Podman’s official documentation for installation instructions tailored to your platform.

Pulling the MySQL Image

With Podman installed, the next step is to pull the MySQL image from a container registry. Podman seamlessly integrates with Docker registries, allowing users to pull images with familiar commands. Execute the following command to fetch the official MySQL image from Docker Hub:

				
					$ podman pull docker.io/library/mysql:latest
				
			

This command retrieves the latest MySQL image and stores it locally on your system, ready for container deployment.

Creating a MySQL Container

Once the MySQL image is available locally, it’s time to create a container instance using Podman. Before doing so, let’s define the necessary environment variables and settings. For instance, specifying the root password and exposing the MySQL port for external access are common configurations. Here’s an example command to create a MySQL container:

				
					$ podman run --name mysql-container -e MYSQL_ROOT_PASSWORD=my-secret-pwd -d -p 3306:3306 mysql:latest
				
			
				
					$ podman ps
CONTAINER ID  IMAGE                           COMMAND               CREATED        STATUS            PORTS                   NAMES
d98ec496fae7  docker.io/library/mysql:latest  mysqld                4 seconds ago  Up 3 seconds ago  0.0.0.0:3306->3306/tcp  mysql-container
				
			

Breaking down the command

  • --name mysql-container assigns a descriptive name to the container.
  • -e MYSQL_ROOT_PASSWORD=my-secret-pwd sets the root password for MySQL.
  • -d runs the container in detached mode.
  • -p 3306:3306 maps port 3306 from the container to port 3306 on the host, enabling external access to MySQL.

Deploying MySQL Using Podman: Accessing the MySQL Database

With the MySQL container up and running, you can now access it using the following commands:

Method #1

				
					$ podman exec -it mysql-container /bin/bash

				
			

By running this command, you’ll be dropped into a Bash shell within the “mysql-container” container, allowing you to execute commands and perform tasks within the containerized environment. This can be useful for troubleshooting, debugging, or performing administrative tasks within the MySQL container.

				
					bash-4.4# mysql -h localhost -p
Enter password: 
				
			

 You’ll be prompted to enter the root password specified during container creation. Once authenticated, you’ll have full access to the MySQL database. 

				
					Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.3.0 MySQL Community Server - GPL

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

				
			

Method #2

				
					$ podman exec -it mysql-container mysql -u root -p

				
			

By running this command, you’ll be prompted to enter the password for the root user of the MySQL server running inside the “mysql-container” container. After entering the correct password, you’ll have access to the MySQL command-line interface (CLI) within the container, allowing you to interact with the MySQL server, execute queries, and perform administrative tasks.

Persisting Data

By default, data within a container is ephemeral and gets lost when the container is deleted or restarted. To ensure data persistence across container lifecycles, it’s essential to mount a volume from the host system to the MySQL container. This allows MySQL to store data on the host filesystem, ensuring data integrity and longevity. Here’s how you can achieve this:

				
					$ podman run --name mysql-container -e MYSQL_ROOT_PASSWORD=my-secret-pwd -d -p 3306:3306 -v /path/on/host:/var/lib/mysql mysql:latest
				
			

Replace /path/on/host with the desired path on your host system where you want to store MySQL data.

Conclusion

In this guide, we’ve explored how to deploy a MySQL database using Podman, a versatile containerization tool known for its simplicity and efficiency. By following the steps outlined above, you can quickly set up a MySQL instance within a container, empowering you with portability, scalability, and ease of management.

Did you find this article useful? Your feedback is invaluable to us! Please feel free to share your thoughts in the comments section below.

Related Posts

Install MySQL on Fedora 37
Commands
Install MySQL on Fedora 37

In this article, we will review how to install MySQL on Fedora 37, providing step-by-step instructions to ensure a smooth installation process. Table of Contents

Read More »

Leave a Reply

Your email address will not be published. Required fields are marked *