How to Fix “MySQL Command Not Found” Error?

If you encounter the "mysql: command not found" error while attempting to execute the mysql command, it indicates that the mysql command is not included in your system's path. This issue may arise due to the absence of the mysql command installation on your system or if the directory containing the mysql binary is not added to your system's path. 

To address this error, please follow these steps:

Step 1: Verify MySQL Installation on Your System

To check if MySQL is installed on your system, use the following command:

which mysql

This command will display the path to the mysql binary if MySQL is installed. If nothing is printed, it indicates that MySQL is not installed on your system, leading to the "mysql command not found" error.

Step 2: Install MySQL if it is not Installed

If MySQL is not installed, resolving the "mysql command not found" error requires installation. The installation process varies based on the operating system. Below are the installation details for Ubuntu and Redhat.

Install MySQL on Ubuntu

To install MySQL on Ubuntu, utilize the apt package manager, which is included by default. This manager is used for installing, updating, and managing software packages on the system. Follow these steps:

  1. Update the package index: Before installing MySQL, ensure your package index is up to date by running the following command:

sudo apt update

This command may take some time, depending on the number of packages needing updates.

  1. Install MySQL: After updating the package index, proceed to install MySQL using the apt command. Execute the following command:

sudo apt install mysql-server

This command installs the MySQL server along with all the necessary dependencies on your system.

  1. Start the MySQL Service: Upon completing the MySQL installation, initiate the MySQL service to enable its usage. Run the following command:

sudo systemctl start mysql

This command starts the MySQL service and configures it to launch automatically with system boot.

  1. Set the Root Password for MySQL: After initiating the MySQL service, it's essential to establish a password for the MySQL root user. Since the root user typically has no default password, setting one is necessary for accessing the MySQL server.

To establish the root password, run the following command:

sudo mysql_secure_installation

This command launches the MySQL root password setup wizard, guiding you through the process of defining a password for the root user.

  1. Log in to MySQL: Once the root password is set, log in to the MySQL server using the mysql command. Run the following command:

mysql -u root -p

You'll be prompted to enter the password you set in the previous step to access the MySQL server.

After successfully logging in, you'll find yourself at the MySQL command prompt, allowing you to execute MySQL commands for database management and other tasks.

Note: These instructions are Ubuntu-specific and may not be applicable to other Linux distributions. For different distributions, refer to the respective documentation for MySQL installation and configuration guidance.

Install MySQL on Red Hat-based System:

To install MySQL on Red Hat, utilize the yum package manager, which comes pre-installed. Yum is employed for installing, updating, and managing software packages on the system. 

Follow these steps:

  1. Update the Package Index: Before proceeding with MySQL installation, ensure your package index is current. Execute the following command:

sudo yum update

  • The command's duration depends on the number of packages requiring updates.

  1. Install MySQL: Once the package index is updated, install MySQL using the yum command. Run the following command:

sudo yum install mysql-server

  • This command installs the MySQL server along with all necessary dependencies on your system.

  1. Start the MySQL Service: Once MySQL is installed, initiate the MySQL service to enable its usage. Execute the following command:

sudo systemctl start mysqld

This command starts the MySQL service and configures it to launch automatically with system boot.

  1. Set the Root Password for MySQL: After starting the MySQL service, it's crucial to establish a password for the MySQL root user since the root user typically has no default password. To set the root password, use the following command:

mysql_secure_installation

This command initiates the MySQL root password setup wizard, guiding you through the process of defining a password for the root user.

  1. Log in to MySQL: Upon setting the root password, log in to the MySQL server using the mysql command. Execute the following command:

mysql -u root -p

You'll be prompted to enter the password you set in the previous step to access the MySQL server successfully.

Upon successful login, you'll find yourself at the MySQL command prompt. From here, you can execute MySQL commands to oversee your databases and carry out various tasks.

Please note that these instructions are tailored to Red Hat, and their applicability may differ on other Linux distributions. If you are utilizing a different Linux distribution, refer to the specific documentation for guidance on MySQL installation and configuration.

Step 3: Resolve MySQL Command Not Found by Adding to System Path

If you've installed MySQL, yet the mysql command remains unfound, it suggests that the directory containing the mysql binary is not part of your system's path. To rectify this, add the mysql binary to your system's path by modifying the PATH environment variable.

Execute the following command, replacing with the directory containing the mysql binary:

export PATH=$PATH:

For instance, if the mysql binary resides in the /usr/local/mysql/bin directory, use this command:

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

After running this command, you should be able to execute the mysql command without encountering the "mysql: command not found" error.

Remember, the PATH environment variable resets each time you open a new terminal. Consequently, you'll need to run this export command whenever you open a new terminal to ensure the mysql command functions correctly.

Conclusion

In conclusion, we've addressed the "mysql command not found" error, exploring diverse solutions tailored to different operating systems. 

If you have additional questions, please don't hesitate to ask in the comment section below. Your queries are welcomed and will be promptly addressed.


Was this article helpful?

mood_bad Dislike 0
mood Like 1
visibility Views: 351