Run MySQL as Docker Container on Debian

Event though Debian is considered as one of the most stable Linux distributions out there, part of it’s stability comes from the fact that a large portion of it’s package base consists of well-tested but outdated packages. This should be a blessing most of the times but if you are a developer like me, some times you may need a latest or specific version of a package. In this case, I needed the latest version of the mysql-server package. Although official apt repository is available from MySQL to install the latest version of it on Debian, to be able to run something in containers has it’s own advantages including having the ability to run multiple versions of the same package simultaneously and can be easily cleaned up after using.

While one could use my earlier post on creating LXC containers on Debian and run MySQL inside it, it’s a lot easier to use the official MySQL docker image from Docker Hub. If you are yet to install Docker, you can follow this official documentation from Docker to install the community edition of it on Debian. Post installation of Docker, follow the below steps to run MySQL as a Docker container. To be able to connect to the docker instance of MySQL from the MySQL client or MySQL workbench available in the Debian repository, I will be using a few additional tweaks.

  1. Download the required version of MySQL docker image from the Docker Hub. I am using MySQL version 8 which is the latest version of MySQL at the time of writing the post. Replace 8 with the required tag in your case. A full list of supported tags for MySQL docker image can be found at this link.
    docker pull mysql:8
  2. Run the docker image.
    docker run --name test-mysql -e MYSQL_ROOT_PASSWORD=test -d -p 3306:3306 mysql:8 --default_authentication_plugin=mysql_native_password

    A few things to note:
    – The name of the docker instance in this case is “test-mysql”. It can be any arbitrary name.
    – “test” value has been passed as the MYSQL_ROOT_PASSWORD environmental variable while starting the container. This value will be used as the MySQL root password by the container. In absence of this value, it will generate a random root password every time you start the container and print in the log.
    – “-d” option is to run the container in the background as a detached instance.
    – The 3306 port, which is the default port of MySQL is mapped with the 3306 port of the host. This is helpful if you want to connect to the MySQL docker instance on localhost.
    – The “default_authentication_plugin” has been set as “mysql_native_password”. This is required if you want to connect to the MySQL docker instance from the MySQL client or the Workbench available in the Debian repository, which are old and does not support the new default authentication methods introduced in MySQL 8.

  3. The container should take a few seconds to initialize itself. After which, you can use the following command to connect to it.
    mysql --protocol=tcp -u root -ptest

    If you don’t have MySQL client installed in your Debian host, use the following command to install it.

    sudo apt install -y mysql-client
  4. If you want to connect to MySQL from within the container itself, use the following command
    docker exec -it test-mysql mysql -u root -ptest

That’s it. This should give you enough information to start and run MySQL as a Docker container on your Debian host. For additional information and configuration options, visit the following listed pages

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s