DEV Community

Sharafat
Sharafat

Posted on

Running MySQl in Linux (with/ without podman container with phpmyadmin)

My SQL

MySQL is a RDBMS software which use SQL like syntax to manage databases. Nowadays most of the major linux distributions come with maria db preinstalled, which is an open source drop in replace ment for MySQL. I’ll be writing about some ways to install MySQL in linux based operating systems,

XAMPP

Xampp is a popular tool which is an open source cross-platform web server solution stack package developed by Apache friends. It can be installed via the official website’s installer. Here a .run file will be downloaded which can be installed by executing from a terminal. But it is not recommended to install in this way.
The most recommended way is to search for a similar package in distros native package manager. For example, in Arch Linux the package is available through AUR (Arch User Repository). Here’s the git-clone URL,

To install it, we can use a AUR wrapper like yay. To do so, use the following command to query and install the latest version of xampp.

yay xampp
Enter fullscreen mode Exit fullscreen mode

After installing open the app, head over to the second tab and start database and web server. Web UI will be available under localhost.

Podman Container

One another good way to install MySQL is to use a podman or docker container. I personally prefer podman so I will be writing about it. Installing a container running only MySQL is pretty much easy. We just have to grub the image and run it in a container. It’s volume will be created automatically. Or if we also want to include a phpmyadmin web app to manage our image then we actually have to use a pod to contain two different containers.

MySQL image

To setting up MySQL image, we can pull it from dockerhub. The command will be like,

podman pull mysql
Enter fullscreen mode Exit fullscreen mode

then, we can start and run our image with the following command,

podman run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=tree --name mysql-db mysql:latest
Enter fullscreen mode Exit fullscreen mode

Here our root password is defined as tree by the environement variable MYSQL_ROOT_PASSWORD.
And if we try to do list running process we can execute,

podman ps
Enter fullscreen mode Exit fullscreen mode

It will see our image up and running. Now let’s actually enter to our server!

podman exec -it mysql-db mysql -u root -p
Enter fullscreen mode Exit fullscreen mode

Let’s run a command to verify,

show databases;
Enter fullscreen mode Exit fullscreen mode

It’ll list all databases.
Now with localhost:3306 you can access this database from mysql workbench or other clients.

Phpmyadmin image

Phpmyadmin is a web UI for managing MySQL databases. Let’s pull it first,

podman pull phpmyadmin
Enter fullscreen mode Exit fullscreen mode

Now if run this image we won’t be able to access another image (MySQL) because there’s no connection in between them. So we will be using podman pod. Let’s create a podman pod,

podman pod create --name mysql -p 8080:8080 3306:3306
Enter fullscreen mode Exit fullscreen mode

If we have previously created an image as per this guide and that is up and running, try the follwing command to stop and delete,

podman stop mysql-db && podman rm mysql-db
Enter fullscreen mode Exit fullscreen mode

Now let’s start our mysql server under this pod,

podman run -d -e MYSQL_ROOT_PASSWORD=tree --pod mysql --name mysql-db mysql:latest
Enter fullscreen mode Exit fullscreen mode

And finally let’s open our phpmyadmin with this pod,

podman run --name phpmyadmin -e PMA_ARBITRARY=1 -d --pod mysql phpmyadmin

Enter fullscreen mode Exit fullscreen mode

It will be availabe under port 8080, as like we defined earlier. So let’s head over to,

Here, our,

Server = localhost:3306
Username = root
Password = tree
This can be also done graphically with the help of `podman desktop`.
Enter fullscreen mode Exit fullscreen mode

Docker

  • Pull the image from the docker hub
docker pull mysql
Enter fullscreen mode Exit fullscreen mode

or, using podman?

podman pull docker.io/library/mysql
Enter fullscreen mode Exit fullscreen mode
  • Now, let’s create our first container from the mysql image. Here is the command we will use:
docker run --name test-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=tree -d mysql
Enter fullscreen mode Exit fullscreen mode

run: creates a new container or starts an existing one

--name CONTAINER_NAME: gives the container a name. The name should be readable and short. In our case, the name is test-mysql.

-e ENV_VARIABLE=value: the -e tag creates an environment variable that will be accessible within the container. It is crucial to set MYSQL_ROOT_PASSWORD so that we can run SQL commands later from the container. Make sure to store your strong password somewhere safe (not your brain).

-d: short for detached, the -d tag makes the container run in the background. If you remove this tag, the command will keep printing logs until the container stops.

image_name: the final argument is the image name the container will be built from. In this case, our image is mysql.

-p HOST_PORT:CONTAINER_PORT: the -p tag maps a port from the host machine to the container. In this case, we are mapping port 3306 from the host to the container. This is the default port for MySQL.

If the command returns a long string of gibberish (the container ID), it means the container has started. You can check its status with docker ps:

  • To access the terminal inside your container, you can use the following command:
docker exec -it container_name bash
Enter fullscreen mode Exit fullscreen mode
  • And then to login to mysql:
mysql -u root -p
Enter fullscreen mode Exit fullscreen mode

Troubleshooting

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay