---
title: 'How to install MySQL 8 in Docker | Zomro'
description: 'In this article, we will look at how to install a MySQL 8 server in a Docker container, and see how to connect it to phpMyAdmin in docker-compose'
image: 'https://dx86q6oq7ry0e.cloudfront.net/uploads/media/blog/a48@server-panel.net/2022/11/01/1669736570_1.png'
---

![opened image]()

  * [Zomro](https://zomro.com/)
  * [FAQ](https://zomro.com/blog/faq/)
  * How to install MySQL 8 in Docker 



# How to install MySQL 8 in Docker

23-11-2022

In this article, we will look at how to install a MySQL 8 server in a Docker container and see how to connect it to phpMyAdmin in docker-compose, which we installed in the article How to Install phpMyAdmin in Docker.  


## **How to Install Docker**

As usual, update the OS packages. 
    
    
    apt update
    

  
Install the necessary packages and add a new repository:
    
    
    apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
    
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    
    add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
    

  
Update the packages with the new repository:
    
    
    apt update
    

  
Install Docker.
    
    
    apt-get install docker-ce docker-ce-cli containerd.io
    

  
Check the Docker version:
    
    
    docker --version
    

  
![How to Install MySQL 8 in Docker - 1](https://dx86q6oq7ry0e.cloudfront.net/uploads/media/blog/a48@server-panel.net/2022/11/01/1669736570_1.png)Check the status:
    
    
    systemctl status docker
    

  
![How to Install MySQL 8 in Docker - 2](https://dx86q6oq7ry0e.cloudfront.net/uploads/media/blog/a48@server-panel.net/2022/11/01/1669736593_2.png)If it did not start, start it:
    
    
    systemctl start docker
    

  
And add it to autostart.
    
    
    systemctl enable docker
    

  
![How to Install MySQL 8 in Docker - 3](https://dx86q6oq7ry0e.cloudfront.net/uploads/media/blog/a48@server-panel.net/2022/11/01/1669736594_4.png)

**Installing Docker-Compose**
    
    
    curl -L "https://github.com/docker/compose/releases/download/1.25.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    

  
Set execution permissions for the docker-compose file. 
    
    
    chmod +x /usr/local/bin/docker-compose
    

  
Check if Docker-Compose is installed:
    
    
    docker-compose --version
    

  
![How to Install MySQL 8 in Docker - 4](https://dx86q6oq7ry0e.cloudfront.net/uploads/media/blog/a48@server-panel.net/2022/11/01/1669740139_5.png)  
As we can see, everything is fine. Let's move on to creating a file for Docker-Compose.  
  
To keep track of where and what we have installed in the future, let's create a separate folder for this project in the /home directory and navigate to it.
    
    
    mkdir /home/mysql && cd /home/mysql
    

  
In this guide, we will install a specific version of MySQL 8. For this, we will use the repository from hub.docker.com  
  
To create the docker-compose.yaml file, we will use the repository from the link <https://hub.docker.com/_/mysql>  
  
Create the file **docker-compose.yaml** or **docker-compose.yml** :
    
    
    vim docker-compose.yaml
    

  
And add the following code to it:
    
    
    version: '3.1'
    
    services:
       db_mysql:
          container_name: db_mysql
          image: mysql
          ports:
            - "3311:3306"
          restart: always
          environment:
            MYSQL_USER: admin
            MYSQL_PASSWORD: rNZzq5U37DqJlNe
            MYSQL_ROOT_PASSWORD: 4kDGQDYe4JxDjRd
          volumes:
            - /var/lib/mysqld:/var/lib/mysql

  
![How to Install MySQL 8 in Docker - 5](https://dx86q6oq7ry0e.cloudfront.net/uploads/media/blog/a48@server-panel.net/2022/11/01/1669816237_screenshot_4.png)Where:  
  
**db_mysql** : the name of your container;  
**image: mysql** : the image from which MySQL 8 will be deployed;  
**ports: 3311:3306** \- port 3311 which we will use to connect to MySQL;  
**restart:always** \- indicates that the container will be restarted in case of failure or server reboot;  
**mysql_USER** : creating a new user, in this case, admin;  
**mysql_PASSWORD** : password for the admin user;  
**mysql_ROOT_PASSWORD** : this password will be set for the MySQL root superuser account;  
In **volumes** we specify a shared directory so that the database data is preserved when the container is restarted.  
  
Run our script (you need to be in the directory where our file is created. In this case, it is **/home/mysql**):
    
    
    docker-compose up -d 
    

  
Wait for the images to download and deploy.  
  
Check:
    
    
    docker-compose ps
    

  
or 
    
    
    docker ps
    

  
![How to Install MySQL 8 in Docker - 6](https://dx86q6oq7ry0e.cloudfront.net/uploads/media/blog/a48@server-panel.net/2022/11/01/1669816404_screenshot_5.png)

## **How to View Logs**

## 
    
    
    docker logs -f db_mysql
    

  
You can also install this version of MySQL 8 along with phpMyAdmin and link it to it. Let's see how to do this.  
  
Edit the created file **docker-compose.yaml**
    
    
    vim docker-compose.yaml
    

  
  
Add the following structure below:
    
    
    version: '3.1'
    
    services:
       db_mysql:
          container_name: db_mysql
          image: mysql
          ports:
            - "3311:3306"
          restart: always
          environment:
            MYSQL_USER: admin
            MYSQL_PASSWORD: rNZzq5U37DqJlNe
            MYSQL_ROOT_PASSWORD: 4kDGQDYe4JxDjRd
          volumes:
            - /var/lib/mysqld:/var/lib/mysql
       phpmyadmin:
          container_name: phpmyadmin
          image: phpmyadmin
          restart: always
          ports:
            - "8091:80"
          environment:
            - PMA_HOST=db_mysql
          depends_on:
            - db_mysql

  
In the **phpmyadmin** section, we added the phpmyadmin image, specified port **8091** for it, but now it will be linked to a specific database server, namely the MySQL 8 server we installed/deployed.  
  
This parameter is specified in the environment block where:   
**PMA_HOST=db_mysql** \- points to the MySQL container/block described above in this file.  
Also, the **depends_on** directive indicates the dependency of the start/launch on the **db_mysql** container. This means that the **phpmyadmin** container will not start until the **mysql** container starts.   
  
We can start phpmyadmin and mysql with one command.
    
    
    docker-compose up -d 
    

  
Check:
    
    
    docker-compose ps
    

  
As we can see, the containers are running.   
  
![How to Install MySQL 8 in Docker - 7](https://dx86q6oq7ry0e.cloudfront.net/uploads/media/blog/a48@server-panel.net/2022/11/01/1669816500_screenshot_6.png)  
Similarly, you can install the version of the MariaDB database you need. How to do this can be found in [this](https://zomro.com/rus/blog/faq/290-kak-ustanovit-mariadb-v-docker) article.

We also suggest considering other useful articles:

  * [How to Install phpMyAdmin in Docker](https://zomro.com/blog/faq/289-kak-ustanovit-phpmyadmin-v-docker)
  * [How to Install Redis in Docker and Docker Compose](https://zomro.com/blog/faq/301-kak-ustanovit-redis-v-docker)
  * [How to Copy Data from Host to Docker and from Docker to Host](https://zomro.com/blog/faq/284-kak-skopirovat-dannye-s-hosta-v-docker-i-iz-docker-na-host)



Similar articles:

[How to install MySQL 8 in Docker](https://zomro.com/blog/faq/291-kak-ustanovit-mysql-8-v-docker) [Virus scanning on a Linux server](https://zomro.com/blog/faq/716-virus-scanning-on-linux-maldet) [Adding resource records to a domain in DNS hosting](https://zomro.com/blog/faq/296-dobavlenie-zapisej-v-dns-hosting) [How to Install FileGator Manager Files in Docker](https://zomro.com/blog/faq/416-how-to-install-filegator-manager-files-in-docker) [Installation Pterodactyl panel in CentOS 7](https://zomro.com/blog/faq/463-installation-pterodactyl-panel-in-centos-7)

[ GPU server from €174.80/mon ](/dedicated-servers/type=gpu)
