This guide provides a step-wise tutorial on how to quickly install MySQL 8 on Debian 12. MySQL is a fast, stable and true multi-user, multi-threaded SQL database server with its main goals being speed, robustness and ease of use. To see a comprehensive description of the features offered by MySQL 8, navigate to MySQL 8 Reference Manual.
Table of Contents
Installing MySQL 8 on Debian 12
Debian 12 doesn’t ship with MySQL 8 on its default repositories.
apt show mysql-server
Package: mysql-server
State: not a real package (virtual)
N: Can't select candidate version from package mysql-server as it has no candidate
N: Can't select versions from package 'mysql-server' as it is purely virtual
N: No packages found
Install MySQL APT Repository on Debian 12
The recommended way to install MySQL 8 is via the MySQL APT repository. This makes the installation of MySQL 8 a seamless task.
You would always install MySQL APT repo by downloading the repository installer from repo.mysql.com;
wget https://repo.mysql.com//mysql-apt-config_0.8.24-1_all.deb
You can then install the binary to setup MySQL APT repository.
HOWEVER, Please note that as of this writing, the repository is not supported for Debian 12 bookworm.
Thus, let us abort the mission here and try another approach?
But why not try to use MariaDB instead, in the meantime?
Install MariaDB 10 on Debian 12
Anyways, what if you really want MySQL 8 itself, how can you install it on Debian 12 at this moment when the MySQL 8 repos are not available yet?
Install MySQL as Docker Container on Debian 12
Well, if you really really need to use MySQL instead of MariadDB on Debian 12 at the moment, then this is the way to go, run it as a Docker container on Debian 12.
Install Docker CE on Debian 12
Thus, follow the guide below install Docker engine on Debian 12;
How to Install Docker CE on Debian 12
Running MySQL 8 as Docker Container on Debian 12
Once the Docker engine is installed and running, then proceed to setup and run MySQL 8 as Docker container.
There available official Docker images of current latest stable release versions of MySQL 8 on the Docker Hub.
So, you can download the official latest MySQL 8 Docker image using the command below;
docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
e2c03c89dcad: Pull complete
68eb43837bf8: Pull complete
796892ddf5ac: Pull complete
6bca45eb31e1: Pull complete
ebb53bc0dcca: Pull complete
2e2c6bdc7a40: Pull complete
6f27b5c76970: Pull complete
438533a24810: Pull complete
e5bdf19985e0: Pull complete
667fa148337b: Pull complete
5baa702110e4: Pull complete
Digest: sha256:232936eb036d444045da2b87a90d48241c60b68b376caf509051cb6cffea6fdc
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
List the images;
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 041315a16183 6 days ago 565MB
In its basic setup, you can execute the command below to create and run MySQL 8 Docker container;
docker run --name mysql8 -e MYSQL_ROOT_PASSWORD="PASSWORD" -d mysql
This commands create a Docker contained called mysql8 using the MySQL image (mysql, tag is latest by default) and run it in a detached mode, that is in the background as specified by option -d
.
The variable MYSQL_ROOT_PASSWORD defines the password to be set for the MySQL root user.
List running containers
You can verify that the MySQL Docker container is now running;
docker ps
Sample output;
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c2cfc05265f1 mysql:latest "docker-entrypoint.s…" 3 seconds ago Up 3 seconds 3306/tcp, 33060/tcp mysql8
As you can see, our MySQL Docker container is running.
Logging in to MySQL 8 Docker container
You can now connect to MySQL 8 Docker container interactive shell using the command below.
docker exec -it mysql8 bash
Once you are logged you will be dropped into the container shell.
From within the MySQL 8 container shell, you can then login to the MySQL database;
mysql -u root -p
Use the password that you had provided during the deployment as the value of MYSQL_ROOT_PASSWORD.
bash-4.4# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.33 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
Running MySQL commands within MySQL Docker Container
While logged in to the MySQL contanier, you should now be able to execute the usual MySQL commands.
SHOW VARIABLES LIKE "%version%";
mysql> SHOW VARIABLES LIKE "%version%";
+--------------------------+------------------------------+
| Variable_name | Value |
+--------------------------+------------------------------+
| admin_tls_version | TLSv1.2,TLSv1.3 |
| immediate_server_version | 999999 |
| innodb_version | 8.0.33 |
| original_server_version | 999999 |
| protocol_version | 10 |
| replica_type_conversions | |
| slave_type_conversions | |
| tls_version | TLSv1.2,TLSv1.3 |
| version | 8.0.33 |
| version_comment | MySQL Community Server - GPL |
| version_compile_machine | x86_64 |
| version_compile_os | Linux |
| version_compile_zlib | 1.2.13 |
+--------------------------+------------------------------+
13 rows in set (0.01 sec)
There you go.
You can log out from the database server;
\q
and exit the container;
exit
Storing MySQL Docker Container Credentials in Secrets file
If you do not want to simply expose your credentials on the command line by using the variables, you can store them in the secrets file. You can then configure the container to load these credentials from the secrets file by passing the path to the file as the value of the MYSQL_ROOT_PASSWORD_FILE.
Some of the variables whose values can be stored in the secret file are MYSQL_ROOT_PASSWORD
, MYSQL_ROOT_HOST
, MYSQL_DATABASE
, MYSQL_USER
, and MYSQL_PASSWORD
For example, let me store my root password secret in the file /opt/mysql.d/.secrets
;
echo 'MYSQL_ROOT_PASSWORD=ChangeME' > /opt/mysql.d/.secrets
Then, you can use the environment variable MYSQL_ROOT_PASSWORD_FILE to specify the path to secrets file.
docker run --name mysql8 \
-v /opt/mysql.d/.secrets:/run/.secrets \
-e MYSQL_ROOT_PASSWORD_FILE=/run/.secrets \
-d mysql:latest
You can also use –env-file to tell Docker to load the environment variables from the file and pass them to the container.
docker run --name mysql8 --env-file /opt/mysql.d/.secrets -d mysql:latest
Accessing MySQL Docker Container Database from the Host
As much as your MySQL Docker container is running fine above, you can only access it within the Docker network.
Confirm this by checking if any MySQL port is listening on the host;
ss -altnp | grep 3306
There is none! this is because the container will run the MySQL server, exposing the default MySQL port (3306) internally within the container itself. So, how do you now connect to your database? You will need to expose the database port to some port on the host.
Well, first of all, let’s stop and remove the container created above (or simply deploy another one with a different name);
docker rm -f mysql8
Then, let’s create create and run the container with the port exposed.
docker run --name mysql8 -p 3306:3306 -e MYSQL_ROOT_PASSWORD="ChangeME" -d mysql
This command, with option, -p 3306:3306
, maps port 3306 from the container to port 3306 on the host.
ss -altnp | grep 3306
LISTEN 0 4096 0.0.0.0:3306 0.0.0.0:* users:(("docker-proxy",pid=6239,fd=4))
LISTEN 0 4096 [::]:3306 [::]:* users:(("docker-proxy",pid=6245,fd=4))
This allows external applications or tools running on the host machine to communicate with the MySQL server running inside the container via port 3306.
How to Use Custom MySQL Configuration file in Docker Container
To achieve flexibility and ease of control over the MySQL server container, you can use a custom MySQL configuration file, that is residing on a specific directory on your host. You can the be able to mount this configuration in the container, to customize various settings according to your specific requirements.
For example, if i have my customized config under, /opt/mysql.d/my.cnf
. Then you can always mount this in the container and use it as default configuration file;
docker run --name mysql8 \
-v /opt/mysql.d/my.cnf:/etc/mysql/my.cnf \
-p 3306:3306 -e MYSQL_ROOT_PASSWORD="ChangeME" \
-d mysql
MySQL Docker container Persistent Data
Persistent data storage is important in Docker environments because it allows data to be stored and accessed even after the container that created it has been stopped or deleted.
There are two mechanism for storing persistent data;
- Use of Docker Volumes
- Use of Bind Mounts
Volumes
are created and managed by Docker. You can create a volume explicitly using the docker volume create
. They provide better isolation, as they are stored within the Docker ecosystem and are not tied to specific paths on the host machine.
docker volume create mysql-data
List volumes
docker volume ls
Use volumes with the container;
docker run --name mysql8 \
-v /opt/mysql.d/my.cnf:/etc/mysql/my.cnf \
-p 3306:3306 -e MYSQL_ROOT_PASSWORD="ChangeME" \
-v mysql-data:/var/lib/mysql \
-d mysql
Confirm;
docker inspect -f '{{ .Mounts }}' mysql8
Bind Mounts
on the other hand provide a way to mount a directory from the host machine into a container.
docker run --name mycontainer -v /host/path:/var/lib/mysql -d myimage
Read more here.
Restart the MySQL Containers
You can restart the MySQL Docker container;
docker restart <container-name>
check more on docker --help
page.
Viewing Container Logs
You can view the container logs using;
docker logs --tail N <contaner-name>
e.g
docker logs --tail 10 mysql8
Or read logs in realtime;
docker logs -f mysql8
Or read the logs from the host system. Just get the container ID in long;
docker ps --no-trunc
The check the logs under /var/lib/docker/container/*/*.log
.
tail -f /var/lib/docker/container/contianer-ID/container-ID-json.log
e.g
tail -f /var/lib/docker/containers/49ccee3a6e871d56b7e189c1723c87d36723980f7d7b0a538815c175393f1421/49ccee3a6e871d56b7e189c1723c87d36723980f7d7b0a538815c175393f1421-json.log
That marks the end of our guide on installing MySQL 8 on Debian 12 as a Docker container.