Home > Database > Mysql Tutorial > Forgot mysql root password

Forgot mysql root password

王林
Release: 2023-05-12 13:14:39
Original
1088 people have browsed it

Forgot mysql root password

MySQL is a very popular relational database management system. Whether on a personal computer or in an enterprise-level application, using MySQL to manage data has become the norm. However, if you are a newbie using MySQL, sometimes you will encounter some problems, such as forgetting the root password.

In MySQL, root is the default administrator account. It has the highest level of permissions and can access and operate all contents of the database. If you have forgotten your root password, then you need to take some steps to reset it.

The following are several commonly used methods to solve the problem of forgetting the MySQL root password:

Method 1: Use the mysqladmin command

mysqladmin is a command used to manage MySQL servers Run the utility, which can be used to change the password of the root account. The following are the steps to use the mysqladmin command to reset the root password:

1. Use the terminal to open the MySQL command line client:

mysql -u root -p
Copy after login
Copy after login

2. Enter the current root password and log in to the MySQL command line.

3. Enter the following command to change the password:

mysqladmin -u root -p password newpassword
Copy after login

Among them, "newpassword" is the new password you want to set. After executing this command, the root password will be reset.

Method 2: Use the UPDATE statement

If you cannot use the mysqladmin command, you can also try to use the MySQL UPDATE statement to reset the root password. The following are the steps to use the UPDATE statement to change the root password:

1. Use the terminal to open the MySQL command line client:

mysql -u root -p
Copy after login
Copy after login

2. Enter the current root password and log in to the MySQL command line.

3. Execute the following SQL statement on the MySQL command line:

UPDATE mysql.user SET authentication_string=PASSWORD('newpassword') WHERE User='root';
FLUSH PRIVILEGES;
Copy after login
Copy after login

Among them, "newpassword" is the new password you want to set. After executing the statement, the root password will be reset. Set.

Method 3: Modify the configuration file

If the above two methods do not work, then the last method is to modify the MySQL configuration file. The following are the steps to modify the configuration file to reset the root password:

  1. Open the MySQL configuration file my.cnf or my.ini. The specific file name and location may vary based on different operating systems and installation methods.
  2. Add the following line under the [mysqld] section:
skip-grant-tables
Copy after login

This setting will enable MySQL's passwordless authentication mode.

  1. Save the configuration file and close it, then restart the MySQL service.
  2. Use the terminal to open the MySQL command line client:
mysql -u root
Copy after login

You can log in to the MySQL command line without entering a password.

  1. Execute the following statement to change the root password:
UPDATE mysql.user SET authentication_string=PASSWORD('newpassword') WHERE User='root';
FLUSH PRIVILEGES;
Copy after login
Copy after login

Among them, "newpassword" is the new password you want to set.

  1. Delete or comment out the skip-grant-tables line added in my.cnf or my.ini, and restart the MySQL service.

Summary

Forgetting the MySQL root password is a common and annoying problem for users. But luckily, there are several methods you can use to reset your password. For novices, it is recommended to try using the mysqladmin command to change the password first. If this method does not solve the problem, you can try using the UPDATE statement or modifying the configuration file. No matter which method you use, be sure to remember to set a new password to ensure the security of your account.

The above is the detailed content of Forgot mysql root password. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template