You may run into a situation where you need to have two different versions of MySQL to test different applications. Something similar happened to me.
We'll now go through some steps to be able to run both MySQL 5.6 and 5.7 on the same computer with minimal effort.
Things you need:
- Docker. I use Mac OS, so I downloaded the latest stable .dmg from the Docker page.
I'm assuming you already have one version of MySQL installed. In my case I had the 5.7 version and needed the 5.6, so that's what we're going to cover.
If you don't know which version of MySQL are you running, you can quickly check with any of these methods:
- Run the query
SELECT VERSION();on your MySQL instance
mysql --versionfrom the Terminal
Run MySQL 5.6 on Docker
First thing we need is to create a Docker container with MySQL 5.6 installed. To do so run the following command:
sudo docker run --name mysql-56-container -p 127.0.0.1:3310:3306 -e MYSQL_ROOT_PASSWORD=mysuperpassword -d mysql:5.6
This command will download an image with MySQL 5.6 and spin off the Docker container. MySQL will be installed on port 3306, but the host machine will use port 3310 to forward to 3306. Note that you need to provide a password for the root user. In this case is just mysuperpassword.
At this point you can connect to the database from the Terminal using this command and your previously defined password:
mysql -u root -p --port 3310 --host 127.0.0.1
There is a problem though. There are no databases on this instance yet.
Create a copy of your current database
The easiest way to copy your database data is using
mysqldump. To do so, run the following command:
mysqldump -u root -p --opt [database name] > [database name].sql
This will create a [database name].sql file that you'll import into the 5.6 MySQL instance.
Create empty database on your new instance
Connect to your new MySQL instance with the command previoulsy mentioned and create and empty database with the proper name you need:
create database [database name]
Import your .sql file
Now you need to import your database data to your MySQL 5.6 instance. Run:
mysql -u root -p --host 127.0.0.1 --port 3310 [new database name] < /path/to/[database name].sql
And that would do it. Your MySQL 5.6 instance should now have a copy of your database data from the MySQL 5.7 instance.
I hope this helps you since it has helped me. I needed to run an older version of MySQL because some queries on an app were giving me different results on my development environment than on production. The cause was the MySQL version difference between them.
And remember, is a really good idea to have your development environment as close as the one on production.