Home >Database >Mysql Tutorial >mysql root password forgotten
MySQL is a very popular database management system that is widely used in the development of web applications. Since it is necessary to set important information such as login password and root password during the installation and use of MySQL, it is often the case that the MySQL root password is forgotten. This article will introduce how to deal with this situation from various aspects.
1. Retrieve MySQL root password
1. Use the mysqladmin command
In a Linux or Mac terminal, you can use the mysqladmin command to retrieve the MySQL root password. The specific steps are as follows:
Step 1: Open the terminal
Step 2: Use the following command to log in to the MySQL database:
mysql -u root -p
Step 3: Enter your password. If you forget it, skip to step 4
Step 4: Use the following command to reset the root password:
mysqladmin -u root password NEWPASSWORD
2. Modify the MySQL configuration file
If you forget the MySQL root password, you can also retrieve it by modifying the configuration file. The following are the specific steps:
Step 1: Find the MySQL configuration file
In Linux systems, the MySQL configuration file is usually /etc/my.cnf or /etc/mysql/my.cnf . On Windows systems, the configuration file is usually located in C:Program FilesMySQLMySQL Server X.Xmy.ini.
Step 2: Stop the MySQL service
In the Linux or Mac terminal, use the following command to stop the MySQL service:
sudo /etc/init.d/mysql stop
In Windows system, open the service list, find the MySQL service, right-click and select "Stop".
Step 3: Edit the MySQL configuration file
Use a text editor to open the MySQL configuration file. Find the following line:
[mysqldump]
user=root
password=OLD_PASSWORD
Change the original password to the new password you want. For example, if you want to change the password to "apple", you would write:
[mysqldump]
user=root
password=apple
Step 4: Start MySQL Service
In the Linux or Mac terminal, use the following command to start the MySQL service:
sudo /etc/init.d/mysql start
In the Windows system, open the service List, find the MySQL service, right-click and select "Start".
2. Prevent the MySQL root password from being forgotten
1. Record the password
When you set and change the MySQL root password for the first time, it is recommended to use a password management tool to record the password so that Prevent forgetting. You can choose a password management tool like 1Password or LastPass to record passwords.
2. Enable MySQL password policy
MySQL supports the use of password policies to force users to use complex passwords, thereby increasing the difficulty of password cracking. You can enable password policy by editing the MySQL configuration file. The following are the specific steps:
Step 1: Find the MySQL configuration file
In Linux systems, the MySQL configuration file is usually /etc/my.cnf or /etc/mysql/my.cnf . On Windows systems, the configuration file is usually located in C:Program FilesMySQLMySQL Server X.Xmy.ini.
Step 2: Edit the MySQL configuration file
Use a text editor to open the MySQL configuration file. Find the following line:
[mysqld]
default_authentication_plugin=mysql_native_password
Will be the default authentication plugin Modify plugin to mysql_native_password and add the following content:
[mysqld]
default_authentication_plugin=mysql_native_password
validate_password_policy=STRONG
validate_password_length=8
validate_password_mixed_case_count=1
default_password_lifetime=365
The above setting will enable the following password policy:
Finally, restart the MySQL service.
Step 3: Restart the MySQL service
In the Linux or Mac terminal, use the following command to restart the MySQL service:
sudo /etc/init.d/mysql restart
In Windows system, open the service list, find the MySQL service, right-click and select "Restart".
Summary:
Forgetting the MySQL root password often causes a lot of inconvenience. This article introduces how to retrieve the MySQL root password according to different situations. At the same time, this article also recommends some preventive measures, such as logging passwords and enabling MySQL password policies. In the future use of MySQL, I hope these measures can help you better manage passwords and avoid the trouble caused by password issues.
The above is the detailed content of mysql root password forgotten. For more information, please follow other related articles on the PHP Chinese website!