Setting up a local mySQL instance with Docker on Mac
I whitewashed my Macbook a while ago and spend it a fresh and clean installation. It almost took me 3 month to recognize that I was missing a local mySQL instance for development. Dreadful that it took so long to recognize, need to find more time for coding.
However, a quick guide to get a mySQL instance, accessible from my local machine, up and running with Docker.
-
Install Docker Desktop (https://docs.docker.com/docker-for-mac/)
-
Start Docker Desktop and login with your Docker ID (or created a new one)
-
optional: I reduced the Resources to just use one CPU and 1GB of memory. This can be done in the Docker Desktop settings. I don't run any heavy DB tasks on my local machine and prefer to keep resources free for other stuff.
-
Check out which version of MySQL server you would like to run: https://hub.docker.com//mysql. I went with 5.7 as this is the same version we use in production.
-
Open up terminal session and enter the following command (adjust the version behind : to match your favored version):
docker pull mysql/mysql-server:5.7
-
Check if it installed correctly executing
docker images
-
Now let's run this machine.
docker run -d --publish=3307:3306 --name=mysql-instance -e MYSQL_ROOT_PASSWORD=root mysql/mysql-server:5.7
Once again, you need to adjust the command to match your version.
I also changed the port from 3306 to 3307, just in case it might already be in use. If you prefer, just go with the standard 3306:3306. By adjusting the port, you could run as many instances as you want. As you might have already guessed I set the password for our root user, just to root.
- Finally we need to grant our root user the right to connect externally (actually from our local machine). You should not go the following way with a production system, but I am just lazy and would like to have the root user being able to connect. A more secure way would be to create a new user with limited permissions.
Connect to MySQL from the terminal using following command (adjust mysql-instance, if you chose a different name in Step 7):
docker exec -it mysql-instance mysql -u root -p
Now let's allow root user to connect from any host:
update mysql.user set host='%' where host='localhost' and user='root';
and flush privileges:
flush privileges;
Now we're done and can use our instance.