Using multiple MySQL versions on the same linux machine using docker

Updated . Posted . Visible to the public.

We had a card that described how to install multiple mysql versions using mysql-sandbox. Nowadays with the wide adoption of docker it might be easier to use a MySQL docker image for this purpose.

Create a new mysql instance

docker run --name projectname_db -e MYSQL_ROOT_PASSWORD=secret -p "33008:3306" -d --restart unless-stopped mysql:5.7

The port 33008 is a freely chosen free port on the host machine that will be used to establish a connection.
You can see available mysql versions on the docker hub Show archive.org snapshot .
The restart policy unless-stopped will also start the database container after a reboot (until you manually stop it).

Connect to DB instance

$ mysql -uroot -psecret -P 33008 -h 127.0.0.1
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]>

Make sure to always use the ip instead of localhost (-h 127.0.0.1) so that a tcp connection is used instead of a socket connection.

Configure rails application to use the database

development:
  adapter: mysql2
  database: projectname_development
  encoding: utf8mb4
  collation: utf8mb4_unicode_ci
  host: 127.0.0.1
  username: root
  password: secret
  port: 33008
  variables:
    sql_mode: TRADITIONAL

Further commands

docker stop projectname_db # stop the container
docker start projectname_db # start the container e.g. after a reboot
docker rm projectname_db # remove the container (will not delete the volume of the container)
docker volume prune # remove the volume (and all other volumes that are not connected to a container)
docker rm -v projectname_db # remove the container including volume in one step

Troubleshooting

If you get the following error while interacting with the database:

ERROR 2061 (HY000): RSA Encryption not supported - caching_sha2_password plugin was built with GnuTLS support

it may help to disable RSA encryption when creating the docker image by using the flag --default-authentication-plugin=mysql_native_password
e.g.

docker run --name projectname_db -e MYSQL_ROOT_PASSWORD=secret -p "33008:3306" -d --restart unless-stopped mysql:5.7 --default-authentication-plugin=mysql_native_password
Daniel Straßner
Last edit
Dennis
License
Source code in this card is licensed under the MIT License.
Posted by Daniel Straßner to makandra dev (2021-08-04 13:31)