Home Database Mysql Tutorial Forgot mysql password linux

Forgot mysql password linux

May 11, 2023 pm 07:23 PM

As a Linux server administrator, you must have encountered the situation of forgetting the MySQL database password more than once. When you need to manage or modify the MySQL database, you suddenly find that you cannot log in, or the password you set previously no longer applies. At this time, don’t panic, this article will introduce you to several methods to retrieve the MySQL database password from the Linux system.

Method 1: Use troubleshooting mode to reset password

MySQL provides a special troubleshooting mode (Troubleshooting Mode), which can reset the MySQL root user when the password is forgotten. password.

The steps are as follows:

  1. Close the MySQL service:
$ systemctl stop mysqld
Copy after login
  1. Start the MySQL service and enter debugging mode:
$ mysqld_safe --skip-grant-tables &
Copy after login
  1. Connect to the MySQL service:
$ mysql -u root
Copy after login
  1. Execute the following command to reset the root user password:
> use mysql;
> update user set password=PASSWORD("new_password") where User='root';
> flush privileges;
> quit
Copy after login
  1. Close the MySQL service, Then restart:
$ systemctl stop mysqld
$ systemctl start mysqld
Copy after login

After that, try to log in to MySQL using the new password.

Method 2: Use the Linux system account to reset the password

If you used the system user when creating the MySQL user and you have remembered the user's password, then you can use this user to log in and change the MySQL user's password.

The steps are as follows:

  1. Use the system account to log in to the Linux system;
  2. Open the command line terminal and enter the following command:
$ mysql -u user_name -p
Copy after login

Among them, user_name is the MySQL user name for which you need to reset the password.

  1. Enter the password of the MySQL user and press Enter;
  2. Use the following statement to change the password (replace new_password with your own password):
> SET PASSWORD FOR 'user_name'@'localhost' = PASSWORD('new_password');
Copy after login
  1. Enter the following command to make the settings take effect:
> FLUSH PRIVILEGES;
Copy after login
  1. Exit the MySQL terminal:
> quit
Copy after login

Method 3: Change the password through the configuration file

If you know the password of the MySQL root user, then you can directly modify the configuration file and then restart the MySQL service.

The steps are as follows:

  1. Use vi to edit the MySQL configuration file, which is usually in /etc/my.cnf or /etc/mysql/my.cnf:
$ sudo vi /etc/my.cnf
Copy after login
  1. Find a paragraph in the file that looks similar to the following:
[mysqld]
Copy after login
  1. Add the following to the end of the paragraph:
skip-grant-tables
Copy after login
  1. Save and exit the vi editor;
  2. Restart the MySQL service:
$ sudo systemctl restart mysqld
Copy after login
Copy after login
  1. Use the following statement to update the password (replace new_password with your own password ):
$ mysql -u root
> use mysql;
> update user set password=PASSWORD("new_password") where User='root';
> flush privileges;
> quit
Copy after login
  1. Delete the skip-grant-tables line added in the /etc/my.cnf file;
  2. Restart the MySQL service:
$ sudo systemctl restart mysqld
Copy after login
Copy after login

Summary

Whether the MySQL database is inaccessible due to forgotten passwords, incorrect operations, or other reasons, don't panic, the above three methods can help you solve the problem. If you encounter a similar situation, please try to follow the above steps to retrieve the MySQL database password.

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

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Reduce the use of MySQL memory in Docker Reduce the use of MySQL memory in Docker Mar 04, 2025 pm 03:52 PM

Reduce the use of MySQL memory in Docker

How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

How do you alter a table in MySQL using the ALTER TABLE statement?

How to solve the problem of mysql cannot open shared library How to solve the problem of mysql cannot open shared library Mar 04, 2025 pm 04:01 PM

How to solve the problem of mysql cannot open shared library

What is SQLite? Comprehensive overview What is SQLite? Comprehensive overview Mar 04, 2025 pm 03:55 PM

What is SQLite? Comprehensive overview

Run MySQl in Linux (with/without podman container with phpmyadmin) Run MySQl in Linux (with/without podman container with phpmyadmin) Mar 04, 2025 pm 03:54 PM

Run MySQl in Linux (with/without podman container with phpmyadmin)

Running multiple MySQL versions on MacOS: A step-by-step guide Running multiple MySQL versions on MacOS: A step-by-step guide Mar 04, 2025 pm 03:49 PM

Running multiple MySQL versions on MacOS: A step-by-step guide

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)?

How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)? How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)? Mar 18, 2025 pm 12:00 PM

How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)?

See all articles