How to Install MySQL Using Docker and Docker Compose: A Step-by-Step Tutorial

How to Install MySQL Using Docker and Docker Compose: A Step-by-Step Tutorial

This tutorial will guide you through the process of installing MySQL using Docker and Docker Compose. Using Docker simplifies the setup and management of MySQL, making it easier to deploy and maintain.

Docker Compose further streamlines the process by allowing you to define and manage multi-container Docker applications.

Prerequisites

  • Docker installed on your machine.
  • Docker Compose installed on your machine.
  • Basic knowledge of Docker and Docker Compose.

Step 1: Set Up Docker

If you haven't already installed Docker, follow the instructions on the Docker official website.

Step 2: Set Up Docker Compose

If you haven't installed Docker Compose, follow the instructions on the Docker Compose installation page.

Step 3: Create a Docker Compose File

Create a directory for your project and navigate into it:


mkdir mysql-docker
cd mysql-docker

version: '3.8'

services:
  db:
    image: mysql:8.0
    container_name: mysql_container
    environment:
      MYSQL_ROOT_PASSWORD: rootpassword
      MYSQL_DATABASE: mydatabase
      MYSQL_USER: myuser
      MYSQL_PASSWORD: mypassword
    ports:
      - "3306:3306"
    volumes:
      - db_data:/var/lib/mysql

volumes:
  db_data:

Explanation:

  • version: Specifies the version of Docker Compose.
  • services: Defines the services that make up the application.
    • db: The name of the MySQL service.
      • image: The MySQL image to use. Here, we're using MySQL version 8.0.
      • container_name: The name of the container.
      • environment: Environment variables to configure MySQL.
        • MYSQL_ROOT_PASS
        • WORD: The root password for MySQL.
        • MYSQL_DATABASE: The name of the default database to create.
        • MYSQL_USER: The username for a new user.
        • MYSQL_PASSWORD: The password for the new user.
      • ports: The port mapping. This maps port 3306 on the host to port 3306 on the container.
      • volumes: Defines a named volume to persist data.
  • volumes: Defines the named volume db_data used to persist the database files.

Step 4: Start the MySQL Service

To start the MySQL service, run the following command in the directory where the docker-compose.yml file is located:


docker-compose up -d

The -d flag runs the services in the background (detached mode).

Step 5: Verify the Installation

To check if the MySQL container is running, use the following command:

docker ps

You should see an entry for mysql_container.

Step 6: Connect to the MySQL Database

You can connect to the MySQL database using any MySQL client. For example, you can use the MySQL command-line client from within the container:

docker exec -it mysql_container mysql -umyuser -pmypassword mydatabase

Replace myuser, mypassword, and mydatabase with the values you defined in the docker-compose.yml file.

Step 7: Stopping and Removing Containers

To stop the MySQL container, run:
docker-compose down

This command stops and removes the container but preserves the data in the named volume db_data.

Conclusion

You've successfully installed and set up MySQL using Docker and Docker Compose. This setup makes it easy to deploy, manage, and scale your MySQL instances.

For more advanced configurations and options, refer to the official Docker and MySQL documentation.







Read more




Open-source Apps

9,500+

Medical Apps

500+

Lists

450+

Dev. Resources

900+

/