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 -yFor 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:latestThis 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-containerBreaking down the command
--name mysql-containerassigns a descriptive name to the container.-e MYSQL_ROOT_PASSWORD=my-secret-pwdsets the root password for MySQL.-druns the container in detached mode.-p 3306:3306maps 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/bashBy 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 -pBy 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:latestReplace /path/on/host with the desired path on your host system where you want to store MySQL data.
No comments:
Post a Comment