Kiến Thức Linux

How to Fix MySQL ‘Command Not Found’ (Linux, Windows, mac OS)

tutorial on fixing the command not found error in MySQL

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.

tutorial on fixing the command not found error in MySQL

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.

Command not found in Windows cmd
  1. Navigate to the Windows Start Menu and select This PC.
  2. Click the Properties tab.
Selecting the properties Tab in This PC menu
  1. Now select Advanced System Settings.
Advanced system settings tab in Mysql
  1. Click the Environment Variables button.
Enviormental Variables listed
  1. Single-click the Path system variable, and then click the Edit button.
How to change the Path enviormental variable
  1. Select New to add the correct path for your MySQL folder.
Adding a new enviormental variable
  1. Retrieve the full path of your MySQL installation. In this example, MySQL is on the C: partition in the Program Files folder.
example of locating the MySQL folder
  1. Find the bin folder within MySQL and copy the path.
Copying the full path to MySQL bin folder
  1. Paste the full path to the Edit Environment Variable window and click OK to save your changes and exit the screen.
Edit the enviormental variable in Mysql
  1. Restart the cmd interface and enter the initial command once again:
mysql -u root -p

Enter your password (if you predefined one).

Successful login to MySQL

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.

  1. 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.

Finding the path using echo command
  1. 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
  1. 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.

editing the bash file
  1. Save and exit the file.

Note: Make sure to restart your Linux server. Otherwise, the change does not take effect.

  1. Run the standard command to launch MySQL:
mysql -u root -p

You have successfully accessed MySQL (MariaDB) on CentOS 7:

Successful access to MariaDB.
Or Ubuntu 18.04:

access granted to MySQL in Ubuntu

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.

  1. Edit the .bash_profile file on macOS by entering the following command:
open -t .bash_profile
  1. Add the path for your MySQL bin folder:
export PATH=${PATH}:/usr/local/mysql/bin
  1. Save and exit the file.
  2. Enter the command to access the MySQL monitor:
mysql -u root -p

You have successfully gained access to your MySQL shell.

MySQL shell on macOS

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.

Đăng ký liền tay Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !

Add Comment

Click here to post a comment

Đăng ký liền tay
Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !