Docker

What are the Steps to Use MySQL With Docker?

MySQL is a well-known relational database management program that can operate on various platforms, such as Windows, Facebook, Twitter, etc. Docker is a well-known forum that enables developers to create, execute, and distribute applications by utilizing containers. Docker permits users to use MySQL with it, which is an efficient and adaptable way to install and maintain database applications. It also provides security, portability, and isolation for MySQL applications.

This article will explain the step-by-step procedure for using MySQL with Docker.

What are the Steps to Use MySQL With Docker?

To use MySQL with Docker, try out the below-mentioned steps:

Step 1: Pull MySQL Image From Docker Hub

To pull MySQL from Docker Hub to the local system, write out the below-listed command in Windows PowerShell:

docker pull mysql

It can be observed that the MySQL image’s latest version has been downloaded.

Step 2: View the Downloaded Image

Next, list all the available images to ensure that the MySQL image has been pulled successfully:

docker images

The above output shows the latest version of MySQL image.

Step 3: Start MySQL Container

Then, create and execute the MySQL container via the “docker run -d –name <cont-name> -e MYSQL_ROOT_PASSWORD=<password> <image-name>” command:

docker run -d --name mySql-cont -e MYSQL_ROOT_PASSWORD=mysql123 mysql:latest

Here:

  • –name” option sets the container name i.e., “mySql-cont”.
  • -d” flag is utilized to execute the container in background.
  • -e MYSQL_ROOT_PASSWORD” defines the root password to “mysql123”.
  • mysql:latest” is the Docker image to utilize:

The above-executed command has built and started a “mySql-cont” container running the latest version of MySQL in the background.

Step 4: View Running MySQL Container

To ensure that the MySQL container is running successfully, execute the following command:

docker ps

The above output indicates that the MySQL container is running successfully i.e., “mySql-cont”.

Step 5: Access MySQL Container

Now, utilize the “docker exec -it” command along with the container name to open the Bash shell inside the running MySQL container:

docker exec -it mySql-cont bash

The above-stated command has opened a Bash shell and now users can execute the command within the running MySQL container.

Step 6: Connect to MySQL Database

After that, connect to the MySQL database as the root user using the provided command and enter the password interactively:

mysql -uroot -p

It can be observed that the MySQL shell has been started.

Step 7: Execute MySQL Commands

Finally, run the MySQL commands in the MySQL container. For instance, execute the “SHOW DATABASES;” command to view all the existing databases:

SHOW DATABASES;

The above output showed the available databases in the MySQL container.

To select a specific database, execute the “USE <database-name>;” command:

USE mysql;

Moreover, to view the tables in the selected database, utilize the below-provided command:

SHOW TABLES;

In the above output, all the tables in the MySQL container can be seen. We have successfully used MySQL with Docker.

Conclusion

To use MySQL with Docker, first, pull the MySQL image from Docker Hub using the “docker pull mysql” command. Then, create and execute the MySQL container through the “docker run -d –name <cont-name> -e MYSQL_ROOT_PASSWORD=<password> <image-name>” command and view it. After that, access the MySQL container and connect to the MySQL database. Finally, run MySQL commands in it. This article has explained the procedure for using MySQL with Docker.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.