search
HomeDatabaseMysql TutorialMysql root password forgotten
Mysql root password forgottenMay 14, 2023 am 09:44 AM

MySQL is a popular open source database management system that is widely used in enterprise and personal application development. However, due to various reasons, sometimes it happens that you forget your MySQL root password. When this happens, you don’t have to worry as there are several ways to reset the MySQL root password.

First, you can try to log in using the MySQL default root user and an empty password. This may be because you have not changed or forgotten to change the password. Run the following command in command line mode. If you can log in directly, you can change the MySQL root password by modifying the password:

mysql -u root -p

If you cannot log in with the default root user and empty password, you can consider Use the following method to reset your MySQL root password:

Method 1: Use safe mode to reset the MySQL root password

Step 1: Shut down the MySQL service.

In MySQL server, you can use the following command to shut down the MySQL service:

sudo systemctl stop mysql

Step 2: Start MySQL server safe mode.

In Linux systems, you need to use root privileges to start the MySQL server safe mode. You can run the following command:

sudo mysqld_safe --skip-grant-tables &

Step 3: Log in to the MySQL server, no password is required.

After starting the MySQL server in safe mode, you can log in to the MySQL server without providing a password. Log in by entering the following command:

mysql -u root

Step 4: Change the root user password using the following command:

UPDATE mysql.user SET authentication_string=PASSWORD('new_password') WHERE User='root';

You need to replace "new_password" with the new password you want to set. This will change the password of the MySQL root user.

Step 5: Refresh permissions and exit MySQL.

After updating the password, you need to refresh the MySQL user permissions and exit MySQL. Enter the following command to complete this operation:

FLUSH PRIVILEGES;
EXIT;

Step 6: Restart the MySQL server.

After completing the above steps, you need to turn off safe mode and restart the MySQL server. Do this by entering the following command:

sudo systemctl stop mysql
sudo systemctl start mysql

Now you have successfully reset the MySQL root password and restarted the MySQL server.

Method 2: Reset the MySQL root password using MySQL's init-file option

Step 1: Create a file named resetpass.sql and open it.

Enter the following command in the file:

UPDATE mysql.user SET authentication_string=PASSWORD('new_password') WHERE User='root';
FLUSH PRIVILEGES;

Replace "new_password" with the new password you want to set.

Step 2: Stop the MySQL service.

You can stop the MySQL service by running the following command:

sudo systemctl stop mysql

Step 3: Start the MySQL service using the --init-file option.

At the command line, pass the resetpass.sql file to the MySQL server to initialize the new password as follows:

sudo mysqld --init-file=/path/to/resetpass.sql &

To specify the correct path, change /path/to/resetpass. sql replaced with the actual path to the resetpass.sql file.

Step 4: Check the MySQL log file for abnormalities.

In the previous step, you can now restart the MySQL service. After starting MySQL, you can confirm whether there are any abnormal logs by viewing the MySQL log file:

sudo tail -f /var/log/mysql/error.log

Step 5: Delete the resetpass.sql file.

After starting the MySQL server, you can delete the resetpass.sql file to prevent others from accessing and changing the root password.

Method 3: Use dpkg-reconfigure to reset the MySQL root password

Step 1: Stop the MySQL server.

At the command line, stop the MySQL server by entering the following command:

sudo systemctl stop mysql

Step 2: Start the dpkg-reconfigure tool.

At the command line, enter the following command to start the dpkg-reconfigure tool:

sudo dpkg-reconfigure mysql-server-5.7

Step 3: Follow the wizard prompts and reset the MySQL root password.

In the wizard of the dpkg-reconfigure tool, you need to set a new password for the MySQL root user. Follow the tool prompts to complete setting your new password.

Step 4: Restart the MySQL server.

Now that you have successfully reset the MySQL root password, please restart the MySQL server by entering the following command:

sudo systemctl start mysql

Conclusion

In this article, we have covered three different Method to reset MySQL root password. While each method is different, they can all be used to avoid the problems of forgetting your MySQL root password. No matter which method you choose to reset your password, you need to pay attention to security and ensure that only people with access to the MySQL server can reset the password. By taking necessary precautions and following the correct steps, you can easily reset your MySQL root password.

The above is the detailed content of Mysql root password forgotten. 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
Reduce the use of MySQL memory in DockerReduce the use of MySQL memory in DockerMar 04, 2025 pm 03:52 PM

This article explores optimizing MySQL memory usage in Docker. It discusses monitoring techniques (Docker stats, Performance Schema, external tools) and configuration strategies. These include Docker memory limits, swapping, and cgroups, alongside

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

This article addresses MySQL's "unable to open shared library" error. The issue stems from MySQL's inability to locate necessary shared libraries (.so/.dll files). Solutions involve verifying library installation via the system's package m

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

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

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

This article compares installing MySQL on Linux directly versus using Podman containers, with/without phpMyAdmin. It details installation steps for each method, emphasizing Podman's advantages in isolation, portability, and reproducibility, but also

What is SQLite? Comprehensive overviewWhat is SQLite? Comprehensive overviewMar 04, 2025 pm 03:55 PM

This article provides a comprehensive overview of SQLite, a self-contained, serverless relational database. It details SQLite's advantages (simplicity, portability, ease of use) and disadvantages (concurrency limitations, scalability challenges). C

How do I configure SSL/TLS encryption for MySQL connections?How do I configure SSL/TLS encryption for MySQL connections?Mar 18, 2025 pm 12:01 PM

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

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

This guide demonstrates installing and managing multiple MySQL versions on macOS using Homebrew. It emphasizes using Homebrew to isolate installations, preventing conflicts. The article details installation, starting/stopping services, and best pra

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

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools