SIGN IN / UP
    opened image

    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.
     

    So, let's install Docker. 
    As usual, we update the OS packages. 

     

    apt update
    


    Let's 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
    

     


    Check the status:

     

     

     

     

    systemctl status docker
    

     


    If it didn't start, start it:

     

     

     

     

    systemctl start docker
    

     


    And add it to autostart.

     

     

     

     

    systemctl enable docker
    

     


     

     

    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 rights for the docker-compose file. 

     

     

     

    chmod +x /usr/local/bin/docker-compose
    

     


    Check if Docker-Compose is installed:

     

     

     

     

    docker-compose --version
    

     



    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

     


    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, it's 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.

    Let's run our script (to do this, you need to be in the directory where our file is created. In this case, it's /home/mysql):

     

     

     

     

     

     

    docker-compose up -d 
    

     


    Wait for the images to download and deploy.

    Check:

     

     

     

     

    docker-compose ps
    

     


    or 

     

     

     

     

    docker ps
    

     



    To view the logs, use the command 

     

     

     

     

    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, but now it will be linked to a specific database server, namely the MySQL 8 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 startup dependency 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. 


    Similarly, you can install the version of the MariaDB database you need. How to do this, you can read in this article.