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

One thought on “Install MySQL using docker compose”
  1. I have mysql installed locally on my ubuntu and I want mysql docker container to run on port 3307 with volume bind. I want it to be stored in specific folder here it is mysql_bind_folder. I stop mysql service on my ubuntu server and try to create a container using following command.
    docker run –name mysql-wvol -p 3307:3306 -e MYSQL_ROOT_PASSWORD=pwd1234 -d –mount type=bind,source=”$(pwd)”/mysql_bind_folder,target=/var/lib/mysql mysql:5.7-oracle –verbose –log-error-verbosity=3

    the log is showing chown: changing ownership of ‘/var/lib/mysql’: Permission denied
    chatGPT is suggesting
    mkdir -p ~/mysql_container_data
    sudo chown -R 999:999 mysql_bind_folder
    sudo chmod -R 755 mysql_bind_folder

    That also I tried but same message. can you find out why it is not working?

Leave a Reply

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