MySQL is an open source database management system (DBMS) used by many web applications and websites. In this article, we’ll show you how to fix the error message “MySQL command not found”.
What Is The MySQL Command Not Found Error?
If you are getting the error “mysql: command not found” when trying to run the mysql
command, it means that the mysql
command is not in your system’s path or you need SQL homework help. This can happen if the mysql
command is not installed on your system, or if the mysql
command is installed but the directory containing the mysql
binary is not included in your system’s path.
To fix this error, you can try the following steps:
Also read: How to optimize WordPress Database Performance
Step 1: Check if MySQL is Installed on your System
Check if MySQL is installed on your system: To check if MySQL is installed on your system, you can run the following command:
which mysql
If MySQL is installed, this command will print the path to the mysql
binary. If the command prints nothing, it means that MySQL is not installed on your system which is why you are getting “mysql command not found” error.
Get exclusive access to all things tech-savvy, and be the first to receive
the latest updates directly in your inbox.
Step 2: Install MySQL if it is not installed
If MySQL is not installed then you need to install to resolve “mysql command not found” error, installing MySQL is slightly different depending upon the OS, we’ve mentioned installation details for Ubuntu and Redhat.
Install MySQL on Ubuntu
To install MySQL on Ubuntu, you can use the apt
package manager. The apt
package manager is included by default on Ubuntu and is used to install, update, and manage software packages on the system.
To install MySQL using apt
, follow these steps:
- Update the package index: Before installing MySQL, you should update the package index to make sure that you have access to the latest versions of the available software packages. To update the package index, run the following command:
sudo apt update
This command may take some time to complete, depending on the number of packages that need to be updated.
- Install MySQL: After updating the package index, you can install MySQL using the
apt
command. To do this, run the following command:
sudo apt install mysql-server
This will install the MySQL server and all of the required dependencies on your system.
- Start the MySQL service: After installing MySQL, you will need to start the MySQL service to begin using it. To start the MySQL service, run the following command:
sudo systemctl start mysql
This will start the MySQL service and enable it to start automatically whenever the system is booted.
- Set the root password for MySQL: After starting the MySQL service, you will need to set a password for the MySQL root user. By default, the root user has no password set, so you will need to set one before you can log in to the MySQL server.
To set the root password for MySQL, run the following command:
sudo mysql_secure_installation
This will start the MySQL root password setup wizard, which will guide you through the process of setting a password for the root user.
- Log in to MySQL: After setting the root password, you can log in to the MySQL server using the
mysql
command. To log in, run the following command:
mysql -u root -p
This will prompt you for the password for the root user. Enter the password you set in the previous step to log in to the MySQL server.
After logging in, you will be at the MySQL command prompt, where you can run MySQL commands to manage your databases and perform other tasks.
Keep in mind that these instructions are specific to Ubuntu and may not work on other Linux distributions. If you are using a different Linux distribution, you should consult the documentation for that distribution to learn how to install and configure MySQL.
Install MySQL on Redhat based system
To install MySQL on Red Hat, you can use the yum
package manager. The yum
package manager is included by default on Red Hat and is used to install, update, and manage software packages on the system.
To install MySQL using yum
, follow these steps:
- Update the package index: Before installing MySQL, you should update the package index to make sure that you have access to the latest versions of the available software packages. To update the package index, run the following command:
sudo yum update
This command may take some time to complete, depending on the number of packages that need to be updated.
- Install MySQL: After updating the package index, you can install MySQL using the
yum
command. To do this, run the following command:
sudo yum install mysql-server
This will install the MySQL server and all of the required dependencies on your system.
- Start the MySQL service: After installing MySQL, you will need to start the MySQL service to begin using it. To start the MySQL service, run the following command:
sudo systemctl start mysqld
This will start the MySQL service and enable it to start automatically whenever the system is booted.
- Set the root password for MySQL: After starting the MySQL service, you will need to set a password for the MySQL root user. By default, the root user has no password set, so you will need to set one before you can log in to the MySQL server.
To set the root password for MySQL, run the following command:
mysql_secure_installation
This will start the MySQL root password setup wizard, which will guide you through the process of setting a password for the root user.
- Log in to MySQL: After setting the root password, you can log in to the MySQL server using the
mysql
command. To log in, run the following command:
mysql -u root -p
This will prompt you for the password for the root user. Enter the password you set in the previous step to log in to the MySQL server.
After logging in, you will be at the MySQL command prompt, where you can run MySQL commands to manage your databases and perform other tasks.
Keep in mind that these instructions are specific to Red Hat and may not work on other Linux distributions. If you are using a different Linux distribution, you should consult the documentation for that distribution to learn how to install and configure MySQL.
Step 3: Fix MySQL Command not found by adding to system path
Add the mysql
binary to your system’s path: If MySQL is installed but the mysql
command is still not found, it means that the directory containing the mysql
binary is not included in your system’s path. You can add the mysql
binary to your system’s path by modifying the PATH
environment variable.
To do this, you can run the following command, replacing <mysql_bin_dir>
with the directory containing the mysql
binary:
export PATH=$PATH:<mysql_bin_dir>
For example, if the mysql
binary is located in the /usr/local/mysql/bin
directory, you can run the following command:
export PATH=$PATH:/usr/local/mysql/bin
After running this command, you should be able to run the mysql
command without getting the “mysql: command not found” error.
Keep in mind that the PATH
environment variable is reset every time you open a new terminal, so you will need to run this export
command every time you open a new terminal in order to be able to run the mysql
command.
Conclusion
We’ve discussed what is “mysql command not found” error and discussed various ways to resolve this error in different operation systems.
If you have any further questions feel free to ask in comment section below.
Related Content