Fixing The Error “MySQL Command not Found”

MySQL Command Not Found

In this article, you’ll learn how to fix the MySQL Command not Found error by editing your computer PATH environment variable. Read on it now.

What causes the error “MySQL Command Not Found”

bash: mysql: command not found

(Please note that this article will only cover the solution on Windows, as the author has no knowledge about Mac).

The “MySQL Command not Found” error occurs when your computer is unable to find the MySQL executable file (or the .exe file) under the path set in your computer’s PATH environment variable.

The PATH environment variable is a directory set that leads to where executable files are located and can be commanded. Usually, a new directory is created in the PATH when installing some applications.

As shown here:

The solution to this error

Taking in the reason of the error, the quick way is to add or edit the path supposedly leading to the executable file. Now you might be asking, “Where would I find the executable file location?”.
Well, generally, that could be two places, though usually, it’s the former:

  • The default directory usually sitting in the C:\ Drive "Program Files" folder
  • A custom directory set in the program installation executable

And assuming it’s the former, the directory would usually sit in your “Program Files” folder in your C:\ Drive under that application’s name, in this case, “MySQL” or “MySQL Server” or “MySQL Server {version}”. However, if you are using XAMPP’s SQL Server, it would be prepackaged inside XAMPP’s directory folder instead under the “mysql” folder.

Additionally, regardless of where the directory is placed, the executable file will always be in the “bin” folder, assuming you have not done anything to the folder.

So taking in all this, your directory will likely be either:

  • C:\Program Files\MySQL\MySQL Server {version}\bin
  • C:\xampp\mysql\bin

So now all left is to change or add to the PATH environment variable, and there are two ways to do so: Using Windows GUI or the Command Prompt.

Using Windows GUI

To do this, we will have to arrive at the two screenshots above first. To access the Environment Variable Windows GUI:

  1. Open up your Start Menu and click “Properties”
  2. Click “Advance System Settings”
  3. Click “Environment Variables”

Or, you can click Win+S and look for “Environment Variables” which will lead you directly to the GUI.
After that, double-click on the PATH and check if the listed directories contain the MySQL bin directory or not. If it’s the latter, create one representing where the executable sits. If it’s the former, edit it to lead to the correct directory.

Using a Command Prompt

If you don’t know, a command prompt allows you to perform a list of actions from a single command line (i.e. a “macro”). You’ll generally only use the Command Prompt (CMD) and PowerShell.
While PowerShell is generally better as a command prompt, with utility in its scripting language, we’ll only be covering the solution in the CMD for simplicity.
You don’t even need to check the enviroment variables GUI. Simply type in the CMD:

echo %PATH%

If you can don’t find the directory in this list, input this line:

setx path "%PATH%[directory]"

So in this case, we get something like:

setx path "%PATH%C:\xampp\bin"
setx path "%PATH%C:\Program Files\MySQL\MySQL Server 8.0\bin"

Summary

To fix the MySQL command not found error, you must first check whether the PATH Environment Variable leads to the executable file them either add or edit a directory. Only tangentially related, but if you intend to use command prompts, it would be useful to use the Bash as it’s now available for Windows 10 and 11.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *