Introduction
MySQL is an open-source database management system that works on Windows, Linux, and macOS. The 'Command Not Found' error can appear regardless of the operating system you are using. It indicates that your system was not able to start the MySQL service because it was not able to find the executable file.
This article shows you how to fix the MySQL ‘Command Not Found’ error in Windows, Linux, and macOS.
Prerequisites
- A Windows, Linux or macOS based system with an administrator account
- A functioning local installation of MySQL
Causes of the 'Command Not Found' Error
Whenever you enter the command to start the MySQL interface, the system searches its directories and tries to execute it.
The 'Command Not Found' error indicates that your system was not able to locate the program you want to run. Two of the most common reasons for this error are:
1. Corrupt MySQL installation: In some instances, the MySQL installation process might be corrupted. If that is the case, a fresh MySQL installation is necessary. If you need assistance with installing MySQL, we have resources that can help you install MySQL on CentOS7, Ubuntu 18.04, or install MySQL on Windows with ease.
2. The PATH environment variable has not been set: If you have confirmed that MySQL is active and running, the next step is to determine the correct path. The PATH variable lists all the directories that your system looks through and tries to find a command you have entered in the terminal. If you run a command, and it’s not found in any of the folders in your defined PATH, then it displays the 'Command Not Found' error.
The following sections outline how to set the PATH environment variable in Windows, Linux, and macOS.
Fix MySQL 'Command Not Found' Error in Windows
Start the Windows cmd prompt and enter the command to launch MySQL:
mysql -u root -p
If the system is not able to execute the command, it presents the following error. Follow the steps below to fix the issue.
- Navigate to the Windows Start Menu and select This PC.
- Click the Properties tab.
- Now select Advanced System Settings.
- Click the Environment Variables button.
- Single-click the Path system variable, and then click the Edit button.
- Select New to add the correct path for your MySQL folder.
- Retrieve the full path of your MySQL installation. In this example, MySQL is on the C: partition in the Program Files folder.
- Find the bin folder within MySQL and copy the path.
- Paste the full path to the Edit Environment Variable window and click OK to save your changes and exit the screen.
- Restart the cmd interface and enter the initial command once again:
mysql -u root -p
Enter your password (if you predefined one).
You have successfully logged in to your MySQL bash shell and can start working on your databases.
Fix MySQL 'Command Not Found' Error in Linux (Ubuntu 18.04, CentOS 7)
This section of the tutorial shows what actions to take to fix the 'Command Not Found' error on CentOS 7 or Ubuntu 18.04. The commands are identical for both Linux distributions.
Start the MySQL shell by typing the following command in your terminal window:
mysql -u root -p
You have received the 'Command Not Found' error. To fix it, edit the .bash_profile file.
- List the directories the system searches when executing a command. To do so enter the following in your terminal window:
echo $PATH
The output provides a current list of all the folders it searches.
- To add the path to the MySQL directory, open the .bash_profile file. Use your preferred text editor (e.g. nano) to edit the file:
nano .bash_profile
- Add a line at the end of the file that specifies the location of your MySQL folder:
export PATH=$PATH:/home/location/of/mysql/bin/folder
Let’s look at an example of an edited .bash_profile file.
- Save and exit the file.
Note: Make sure to restart your Linux server. Otherwise, the change does not take effect.
- Run the standard command to launch MySQL:
mysql -u root -p
You have successfully accessed MySQL (MariaDB) on CentOS 7:
Or Ubuntu 18.04:
Fix MySQL 'Command Not Found' Error on macOS
The same principles discussed for Windows and Linux apply for macOS. The general error appears across different operating systems due to the PATH variable. The system is unable to find executable files in the directories it searches.
- Edit the .bash_profile file on macOS by entering the following command:
open -t .bash_profile
- Add the path for your MySQL bin folder:
export PATH=${PATH}:/usr/local/mysql/bin
- Save and exit the file.
- Enter the command to access the MySQL monitor:
mysql -u root -p
You have successfully gained access to your MySQL shell.
Conclusion
These instructions are universal and can be used to fix the same MySQL error, even if it is affecting a different set of applications. You can now use MySQL effectively and freely interact with your databases.
Check out our article on “Access Denied For User Root@Localhost” error which usually appears for new installations of MySQL when you try to connect to MySQL with the root user.