How to run MySQL in Docker using the M1 silicon chip, with no funny business.

Lucy Ironmonger
7 min readApr 7, 2021

--

The M1 silicon chip MacBooks are out, and they are outrageously good.

If however you’re having issues running Docker, this article may help.

Ersel Aker from Manchester Codes talked me through a way to get MySQL running within Docker on my M1 MacBook. This workaround doesn’t require any funny business — no operating a remote intel machine with SSH keys, or installing a virtual Linux machine on your new shiny mac. Just straight-up badassery and some simple code.

If this post helps, you can: give me a follow on Medium (Lucy Ironmonger), up to 50 claps 👏, and catch me on twitter :)

Step 1 : Download Docker’s Tech Preview

Download this from Docker. It is a Tech Preview of Docker Desktop for the M1 chip, not your normal Docker programme (although it looks the same).

You can read in more detail about some of the known issues here.

Step 2 : Run the Docker Container that hosts your MySQL database

In the example below, I’m running a music library database inside my container.

Open up Terminal.

Note that the bits which are bolded you will replace with your own info (write these down). So pop in your ports (source and destination), database name, and mysql password.

The italicised is what we’ve added for the M1 chip.

You want to write this:

docker run -d -p 3306:3306 name music_library_mysql -e MYSQL_ROOT_PASSWORD=SOME_PASSWORD mysql/mysql-server:latest-aarch64

-d : runs the container in detached mode

-p : publishes the container’s ports to the host

-e : sets the environment variables

— name : assigns a custom name to the container (that’s 2 dashes)

mysql : downloads a MySQL Server Docker Image

And the last bit — /mysql-server:latest-aarch64 changes the architecture so it will be compatible, and readable for your M1. (This is the 64 bit extension of the ARM architecture).

To check it’s running, you can run:

docker ps -a

ps : list containers

a : all containers (check out some other options here)

Or you can check the app, which should give you a steady green light:

Step 3 : Time to go inside the Docker Container

With Docker and the container still running, head back to your open Terminal, and type:

lsof -i :3306

lsof : gives a list of open files

-i : specifically, the internet files

Change the bold to the port you’re

What this should do is give back some data to confirm it’s definitely running:

Sweet, now do:

docker ps -a

This should print out the containers. Make sure your container is definitely running and isn’t exited (see step 2) and then copy the CONTAINER ID from it. E.g. for the below, you would nab 01c34177afd5.

Then back in Terminal, you would use it in this way:

docker exec -it 01c34177afd5 bash

This should then give you:

We’re getting somewhere!

Sidenote: If you’re like me and a bit trigger happy in Terminal, you might get ahead of yourself and do something like this:

To remove the container and start again you’d do:

docker rm 01c34177afd5499a4fb8f2b02bbf0aaa5dc675195f10535df7f44950b6727243

Then you’re good to re-do the command.

Sidenote over — we’re here:

To this you’d want to type:

mysql -u root -p

-u root : access the user called root

-p : you’d assume this means password but no! You can read more on it here.

This will then prompt you for a password in Terminal. Ours was successful with:

SOME_PASSWORD

Remember, this was the code we’d put in right at the start in Step 2. So whatever password you’ve used when you did the initial command:

docker run -d -p 3306:3306 — name music_library_mysql -e MYSQL_ROOT_PASSWORD=SOME_PASSWORD mysql/mysql-server:latest-aarch64

Use that password.

Ok we’re in, Rodney!

Now type:

SELECT host, user FROM mysql.user;

The semicolon is important!

Now what?

As you can see, there are 5 hosts, and 5 users.

Step 4 : Create a new user with all the privileges

You’ll see Terminal is now showing like this:

To this we type:

CREATE USER ‘lucy’@’%’ IDENTIFIED BY ‘password123’;

lucy in this case was the username I wanted to use.

password123 was the password I wanted to use.

Hit enter and it should give you back, without fanfare:

Seems like it hasn’t worked, at least that’s what I thought, but it has.

Next, input into Terminal:

GRANT ALL ON *.* TO ‘lucy’@’%’;

Cool!

So what this has done is create a new user, in my case, lucy with password password123, with all the privileges.

Step 5: Change the info on the MySQL Workbench Database Connection

Head back to MySQL Workbench to get the info changed over:

username: lucy

password: password123

connection name: music_library_mysql

(Obviously use your info!)

Click ‘Test Connection’ and you should now find things are working! Legend!

Step 6: Sort out your .env file

The last part is an easy one to forget. In your coding project you hopefully have a .env file where you keep your private database info. In mine I also have a .env.test file — both have exactly the same data in for now.

On that, you will need to change any information that has been updated. For example my .env file has:

DB_PASSWORD=password123

DB_NAME=music_library_mysql

DB_USER=lucy

DB_HOST=localhost

DB_PORT=3306

All passwords and logins used in this blogpost have been changed since, FWIW.

Please remember you should never share your .env information, or upload it to GitHub. Put .env in your .gitignore file, and make sure the .gitignore is pushed up to your repo first. It’s a sad day when you have to unpick the mess of a .env file being accidentally uploaded!

Run your tests and you should find that your project is up and running again. Woohoo!

— —

Still having issues?

So I discovered after a few days of running this solution, that when my container stopped running, after restarting the container and then running npm start in Terminal I would get this error message, despite not changing any information on the .env file. Connection refused, huh!

Initially I ran through the above steps again by checking the user privileges, but it didn’t solve it. Ersel and I looked then into this and found a quick fix:

  • Start your container in Docker.
  • Go to your .env (and .env.test if you have one) and copy out all the info in those files to a separate place eg text editor.
  • Then delete all the info in the .env files and save the files.
  • Open your Terminal and run npm start — it will really not like this as it will say all your environment variables are now undefined. (This is good!)
  • Then, close Terminal.
  • Paste back in the information to your .env files and save them again.
  • Re-open Terminal, run npm start — it should now work :)

The environment variables are linked to the scope of the session of Terminal you’re in, which can get confused if the container turns off or the Terminal has been open too long. The above steps are like a refresh for it.

Once again a big thanks to tutor supreme Ersel Aker from Manchester Codes in helping unpick this.

Wishing you all lots of fun with your M1s and your coding journeys.

Go get ‘em!

@lucyironmonger

Hit follow and give up to 50 claps if you’re feeling generous!

Manchester Codes is an online school, offering a Fast Track Software Engineering 6-month Course to fit around full-time work. They also offer diversity grants, 0% finance, and have an amazing team of tutors to help students progress.

--

--

Lucy Ironmonger
Lucy Ironmonger

Written by Lucy Ironmonger

Software Developer documenting the small wins. Come say hi!

Responses (1)