Home >Database >Mysql Tutorial >What should I do if I forget mysql password?

What should I do if I forget mysql password?

WBOY
WBOYOriginal
2023-05-18 10:40:076186browse

MySQL is an open source relational database management system that is widely used in various applications on the Internet. But what do you do if you forget your MySQL password? This article will introduce you to several common solutions.

  1. Using the MySQL service management tool

The MySQL installation package provides a service management tool named mysqladmin. Enter the following command in the command line window to reset the MySQL root user password:

mysqladmin -u root password 'newpassword'

In the above command, 'newpassword' is the new password you want to set. If your MySQL service has been started and you have administrator rights, you can reset your password using this method.

  1. Using MySQL’s forgotten password function

MySQL provides a forgotten password function that allows users to reset their passwords. Open the my.ini file (or my.cnf file) in the MySQL installation directory, and add the following line in the [mysqld] section:

skip-grant-tables

After saving the file, restart the MySQL service. At this point MySQL no longer authenticates users and all users can log in to the database.

Next, you can enter the following statement on the command line:

mysql -u root -p

Enter reset password and new password in the input prompt, then exit MySQL, and comment out 'skip in the configuration file -grant-tables'. Restart the MySQL service and log in with the new password.

  1. View the configuration file to get the password

By default, the MySQL root user password is stored in the configuration file. You can get the password from the file to reset it. password. Under Windows, the MySQL configuration file is usually located in C:Program FilesMySQLMySQL Server X.Xmy.ini. Under Linux, the MySQL configuration file is located at /etc/mysql/my.cnf.

In the configuration file, look for the following line:

innodb_flush_log_at_trx_commit=1

Below the line, you will find a code snippet similar to the following:

[mysqld]
user=mysql
pid-file=C:/xampp/mysql/mysql.pid
socket=C:/xampp/mysql/mysql.sock
port=3306
basedir="C:/xampp/mysql"
datadir="C:/xampp/mysql/data"
#password=C:/xampp/mysql/bin/mysqld.exe
#bind-address=localhost
#skip-networking
#enable-named-pipe
#max_allowed_packet = 128M

Where password=C: /xampp/mysql/bin/mysqld.exe is the root user password of MySQL. You only need to remove the comment symbol from this line, save the file, and restart the MySQL service to reset the password using this password.

  1. Reinstall MySQL

If you cannot successfully resolve the forgotten password problem, the final solution is to reinstall MySQL. Before reinstalling, you need to back up your data and download the latest version of MySQL from the MySQL official website. After the installation is complete, just use the previously backed up data to restore and configure.

Summary

Whether you have forgotten your MySQL password or are experiencing other MySQL-related issues, there are multiple solutions available. If you are unable to resolve the issue, it is recommended that you seek help from the official MySQL community. During the problem-solving process, be sure to carefully read the relevant documentation and operating instructions, and follow best practices and security principles.

The above is the detailed content of What should I do if I forget mysql password?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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