How to run MySQL database using Docker-Compose in 3 minutes? – cool example!

How to run MySQL database using Docker-Compose in 3 minutes? - cool example! Docker-Compose: Jak uruchomić bazę danych MySQL w 3 minuty?
Photo by Glenn Case on Unsplash
Share this post and Earn Free Points!

In this article we will focus only on showing how to quickly run MySQL database using Docker-Compose. In short: docker-compose MySQL. Using volumes, we do not lose changes that we make in the database. Changes will still be visible after closing and restarting the container.

Introduction

MySQL

MySQL is a popular open-source relational database management system (RDBMS) that is used to store and manage data. It is designed to be fast, reliable, and easy to use. MySQL is often used for web-based applications, as it supports many different programming languages, including PHP, Python, and Java.

To get started with MySQL, you will need to install the MySQL server on your computer. You can then use a MySQL client, such as the MySQL command-line tool or a graphical interface like MySQL Workbench, to connect to the server and create and manage databases and tables.

Once you have MySQL set up, you can use SQL (Structured Query Language) to create, modify, and query your databases and tables. You can use SQL commands to insert, update, delete, and select data from your tables, as well as to create, alter, and drop tables and databases.

There are many resources available online to help you learn more about MySQL and how to use it effectively. Some good places to start include the MySQL documentation and tutorials, as well as online forums and communities where you can ask questions and get help from other MySQL users.

Docker-Compose

Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define the dependencies of your applications in a single YAML file, and then use a single command to start all of the containers with a single command.

You can find more information about using Docker Compose with MySQL in the Docker documentation and the MySQL documentation.

Advantages Of Docker And Docker-Compose

here are several advantages to using Docker and Docker Compose:

  1. Isolation: Docker containers provide a way to run applications in an isolated environment, which can help reduce conflicts between different applications or different versions of the same application.
  2. Portability: Docker containers are designed to be portable, which means you can easily move them from one system to another. This can make it easier to develop, test, and deploy applications, as you don’t have to worry about setting up the necessary dependencies on every system.
  3. Efficiency: Docker containers are lightweight and efficient, as they only include the necessary components for running the application. This can make it faster to start and stop containers, and it can also save resources on the host system.
  4. Scalability: Docker Compose makes it easy to scale applications by defining and running multiple containers in a single command. This can be useful for applications that need to handle a lot of traffic or that need to run on multiple servers.
  5. Simplicity: Docker Compose provides a simple, declarative syntax for defining and running multi-container applications. This can make it easier to set up and manage complex applications, as you don’t have to worry about the details of starting and stopping individual containers.

Run MySQL Database Using Docker-Compose

Create File: docker-compose.yml

We create the file docker-compose.yml. Inside the file please put the code below. To create the container we will use the image MySQL:7.7.

version: '3'
services:
  mysql-db:
    image: mysql:8.0
    hostname: mysql-db
    container_name: mysql_db
    restart: always
    environment:
      MYSQL_DATABASE: 'bigdataetl'
      MYSQL_USER: 'user_bigdataetl'
      MYSQL_PASSWORD: 'password_bigdataetl'
      MYSQL_ROOT_PASSWORD: 'password_for_root_user'
    ports:
      - '3306:3306'
    volumes:
      - my-db-volume:/var/lib/mysql
volumes:
  my-db-volume:

This Docker Compose file creates a MySQL container using the MySQL 8.0 image. It sets several environment variables to configure the MySQL server, including the root password, the name of a database to create, and the name and password of a user. It also creates a volume to store the data in the MySQL container.

We save the file and then we just need to run the following command being in the directory where we have the file previously created by us YML file.

To start the MySQL container, you can use the docker-compose up command. This will start the MySQL container and any other containers that you have defined in the Docker Compose file. You can then connect to the MySQL server using a MySQL client, such as the MySQL command-line tool or a graphical interface like MySQL Workbench.

docker-compose up -d

After a few moments, the container will be created, up and running. We can check the status of our container.

docker-compose ps

  Name               Command             State                 Ports              
----------------------------------------------------------------------------------
mysql_db   docker-entrypoint.sh mysqld   Up      0.0.0.0:3306->3306/tcp, 33060/tcp

Let’s check the connection. I will use DBeaver. As you can see the connection works. We are able to use our MySQL database using Docker and compose

Summary

That’s all about how to run MySQL database using Docker-Compose. I hope this tutorial helped you to understand usage of MySQL and Docker.

Could You Please Share This Post? 
I appreciate It And Thank YOU! :)
Have A Nice Day!

How useful was this post?

Click on a star to rate it!

Average rating 4.8 / 5. Vote count: 1215

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?