Docker

How to Create MySQL Database with Docker?

MySQL is a free and open-source RDBMS that is commonly used in web applications and other types of software. The MySQL database is a collection of related tables, and each table contains rows and columns to store large amounts of data. This data in a MySQL database can be accessed and managed using Structured Query Language (SQL). Developers use Docker to create the MYSQL database with it as it provides portability, isolation, and many other features.

This write-up will illustrate the procedure to create MySQL Database with Docker.

How to Create MySQL Database with Docker?

To create a MySQL database with Docker, follow the below-provided steps:

  • Pull MySQL Image from Docker Hub
  • Build and Run MySQL container
  • Access MySQL container
  • Connect to MySQL database
  • Create a Database using the “CREATE DATABASE <database-name>;” command
  • Create a new table in the database
  • Insert values in the table
  • Select data from the database

Step 1: Pull MySQL Image from Docker Hub

First, pull MySQL from Docker Hub to the local system by executing the provided command in Windows PowerShell:

docker pull mysql

According to the above image, the latest version of MySQL image has been downloaded.

Step 2: Build and Run MySQL Container

Then, create and run the MySQL container using the MySQL image through 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:

  • The “-d” flag is used to run the container in detached mode.
  • The “–name” option defines the name for the container i.e., “mySql-cont”.
  • The “-e MYSQL_ROOT_PASSWORD” specifies the root password to “mysql123”.
  • The “mysql:latest” is the latest MySQL Docker image to use for the container:

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

Step 3: View Running MySQL Container

Write out the below-listed command to verify whether the MySQL container is running or not:

docker ps

In the above output, the “mySql-cont” container is running successfully.

Step 4: Access MySQL Container

After that, use the “docker exec -it” command 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 the Bash shell and now users can execute MySQL commands within the running MySQL container.

Step 5: Connect to MySQL Database

Next, connect to the MySQL database as the root user via the following command and enter the password in an interactive mode:

mysql -uroot -p

The output shows that the MySQL shell has been started.

Step 6: Create a Database in MySQL

To create a database in MySQL, execute the “CREATE DATABASE <database-name>;” command:

CREATE DATABASE TestDB;

The above command has created a new database named “TestDB”.

Step 7: Verify Created Database

Run the given-provided command to verify whether the new database has been created or not:

SHOW DATABASES;

According to the above output, the “TestDB” database has been created successfully.

Step 8: Select Database

Now, select the newly created database using the “USE <database-name>;” command:

USE TestDB;

In this way, the “Database changed” message is displayed in the terminal.

Step 9: Create New Table in Database

To create a new table in the current database, use the “CREATE TABLE table_name (column1 <datatype>, column2 <datatype>, column3 <datatype> );” command:

CREATE TABLE Student (PersonID INT, Name VARCHAR(20), Age VARCHAR(20), City VARCHAR(20));

This command has created a new table named “Student” with some columns.

Step 10: Insert Values in Table

Then, utilize the “INSERT INTO <table-name> VALUE (value1, value2, value3, …);” command to insert some values in the table:

INSERT INTO Student VALUE ("1", "Laiba", "23", "Islamabad");

The above shows that new values have been inserted in the table.

Step 11: Select Data from the Database

To select data from a specific table, execute the “SELECT * FROM <table-name>” command:

SELECT * FROM Student;

The above output has displayed the data from the “Student” table.

Bonus Tip: Delete the Database

To delete a database, utilize the “DROP DATABASE <database-name>” command:

DROP DATABASE TestDB;

This command has deleted a “TestDB” database.

Conclusion

To create a MySQL database with Docker, first, pull the MySQL Image from Docker Hub. Next, build and run the MySQL container using the “docker run -d –name <cont-name> -e MYSQL_ROOT_PASSWORD=<password> <image-name>” command. Then, access the MySQL container and connect to the MySQL database. After that, create a Database using the “CREATE DATABASE <database-name>;” command. Furthermore, users can create a new table in the database, insert values in the table and select data from the database.

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.