mysql installation using docker composer
MySQL installation using docker compose

In this post, we will install MySQL using docker-compose. MySQL is a Relational database used to store valuable information. Docker is a tool used to containerize your application such that it becomes operating system agnostic

Prerequisite

Create the docker-compose YAML file

touch mysql-docker-compose.yml

Put the below contents in the mysql-docker-compose.yml file

version: "3.7"
services:
  mysql:
    image: mysql:5.7
    container_name: mysql-5.7
    restart: always                       # always restart
    environment:
      MYSQL_DATABASE: 'test'              # name of database
      MYSQL_USER: 'sample'                # sample is the name of user
      MYSQL_PASSWORD: 'password'          # password for sample user
      MYSQL_ROOT_PASSWORD: 'password'     # password for root user
    ports:
      - '3306:3306'                       # host port 3306 is mapper to docker port 3306
    expose:
      - '3306'
    volumes:
      - mysql-db:/var/lib/mysql
volumes:
        mysql-db:

The above Docker compose file will help you to install the MySQL Database instance. The important points about that instance will be

  • The Docker image being used is mysql-5.7
    • This means we are installing the 5.7 version of MySQL
  • A root user will be created in the MySQL Database
    • The username for the root user will be the root
    • The password for the root user will be the password
  • Another sample user will be created
    • username = sample
    • password = password
  • The database name will be tested.

You can change these values in the environment section of the mysql-docker-compose.yml file based on your needs.

Start the MySQL Docker service

docker-compose -f mysql-docker-compose.yml up

This will start the service in the foreground mode and all logs will be printed on the console. If you close the console then the MySQL database will also shut down.

You want to start the MySQL in background mode by using the -d option

docker-compose -f mysql-docker-compose.yml up -d

Connect to MySQL Database

You can use the below configuration to connect to the MySQL instance that was installed using Docker

  • UserName = root
  • Password = password
  • Database Name = sample
  • Host = localhost or 127.0.0.1
  • Port = 3306

References

Next: Postgres Installation using Docker Compose

Leave a Reply

Your email address will not be published. Required fields are marked *