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
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
Check the status:
systemctl status docker
If it did not start, start it:
systemctl start docker
And add it to autostart.
systemctl enable docker
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
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, 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 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.
Similarly, you can install the version of the MariaDB database you need. How to do this can be found in this article.
We also suggest considering other useful articles:
- How to Install phpMyAdmin in Docker
- How to Install Redis in Docker and Docker Compose
- How to Copy Data from Host to Docker and from Docker to Host