Docker Compose: How to run MySQL database?

Docker Compose: How to run MySQL database?
Photo by Glenn Case on Unsplash

In this article we will focus only on showing how to quickly launch the MySQL database in a container.

Using volumes, we do not lose changes that we make in the database. Changes will still be visible after closing and restarting the container.

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:5.7
    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:

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 docker-compose.yml.

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 docker-compose.

 

If you enjoyed this post please add the comment below or share this post on your Facebook, Twitter, LinkedIn or another social media webpage.
Thanks in advanced!

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu