Here I am going to talk you through how to use the mysql command in the terminal.

You would think installing MySQL automatically does this but it doesn’t I’ve found if you don’t install MySQL via Brew or Pip, using mysql in command line doesn’t work straight out of the box. Specifically, if you’ve installed it on an older operating system like High Sierra.

You may have tried typing something like this into the Terminal:

mysql --version

You may have gotten something like mysql command not found in response.

Therefore you have to manually connect the mysql command with the MySQL package location which is located in /usr/local/mysql/bin/mysql.

Therefore if you typed into Terminal something like:

/usr/local/mysql/bin/mysql --version

It should execute the right files and the Terminal should return your version of MySQL.

Recently, I had compatibility issues with installing the latest MySQL on High Sierra and wrote a post on how to install one that was compatible.

Writing just MySQL in Terminal doesn’t work because Terminal looks for commands that are stored in the $PATH variable. The $PATH variable is where the Terminal looks up for any command in the /bin folder. In the /bin folder lives executable files and packages.

When you install new packages most of the time they live in the /bin directory and the $PATH variable is updated to know where to look for them. Installing MySQL directly doesn’t seem to do this for you. Therefore we have to manually do this.

We can do that by typing the following line into Terminal:

export PATH=$PATH:/usr/local/mysql/bin

The $PATH is the variable and the colon : assigns the new location for the /bin folder so now when you type in msql in the command line it works as it links up both. Try typing this into the Terminal and see what comes up:

mysql --version

You should get your version of MySQL popping up. Mine returns:

mysql  Ver 8.0.18 for macos10.14 on x86_64 (MySQL Community Server - GPL)

The issue with assigning the new path is that when you close the Terminal all changes are loss just like closing an unsaved file.

To make sure the new configuration is loaded every time you open Terminal you need to update the .bash_profile which is a dotfile.

Dotfiles are hidden files/folders used to store user preferences and customise your system. .bash_profile is one of them and the one we need to update for the change to happen every time you load Terminal.

The line to make this permanent change is:

echo 'export PATH=$PATH:/usr/local/mysql/bin' >> ~/.bash_profile

What we are doing is saving the result of echo to ~/.bash_profile

And voila! Try closing and opening Terminal and trying out some stuff like typing in mysql. It now should start it up and work.

If you found this post useful please support House Ninety Two and
Buy Me a Coffee at ko-fi.com