MySQL is a popular database management system that is open source software and used by many web developers to store and manage data. When you use MySQL, you need to set a password in the operating system to protect your database. But what if you forget your MySQL password? In this article, we will introduce several methods to view MySQL passwords.
Method 1: Check the MySQL password through the configuration file
In the Linux system, the MySQL configuration file is located at /etc/mysql/my.cnf. You can open this file using a text editor such as vim or nano. Find the following line:
user=root password=your_password
Here you can find the password for the MySQL root user. If this password is not the password you want to know, you can try method two.
Method 2: Use the print variable command to view the MySQL password
Open the terminal and enter the following command:
sudo mysql -uroot -p -e "SHOW VARIABLES LIKE 'password'"
This command will prompt you to enter the root user password for accessing MySQL . After entering the password, the system will display the following results:
+---------------+-------------------------------------------+ | Variable_name | Value | +---------------+-------------------------------------------+ | password | ***************************************** | +---------------+-------------------------------------------+
Your MySQL root password is displayed here.
Method 3: View the MySQL log file
Log in to the MySQL server and use the following command to view the MySQL server log file:
sudo tail /var/log/mysql.log
This command will display the nearest MySQL server Activity record. Search the log file for the following line:
Access denied for user 'root'@'localhost' (using password: YES)
This line indicates that the password you provided is incorrect. Depending on the context, you can find the MySQL administrator's default password in the logs. If this password is not what you need, you may need to contact your system administrator.
If none of the above methods work, you can try resetting your MySQL password. However, this requires you to have administrator rights. You can use the following steps to reset your MySQL password:
sudo systemctl stop mysql
sudo mysqld_safe --skip-grant-tables &
mysql -uroot
use mysql;
UPDATE user SET authentication_string=PASSWORD('new_password') WHERE user='root';
FLUSH PRIVILEGES;
quit;
sudo systemctl stop mysql
sudo systemctl start mysql
Now you can log in to MySQL using your new password.
Summary:
In this article, we introduced you to various methods to view MySQL passwords in Linux systems. You can find the MySQL password by looking in the configuration file, using the print variables command, and viewing log files. If all these methods fail, you can try resetting your MySQL password. No matter which method you choose, you should take care to protect your MySQL password and keep it in a safe place.
The above is the detailed content of How to check MySQL password? Brief analysis of three methods. For more information, please follow other related articles on the PHP Chinese website!