SoFunction
Updated on 2025-03-08

What to do if mysql forgets password (windows linux)

First, let me introduce to you the solution to forget mysql password under Windows.

The actual operation under Windows is as follows

1. Close running MySQL.

2. Open the DOS window and go to the mysql\bin directory.

3. Enter mysqld --skip-grant-tables to enter. If there is no prompt message, that is right.

4. Open another DOS window (because the DOS window just now can no longer be moved), and go to the mysql\bin directory.

5. Enter mysql Enter. If successful, a MySQL prompt will appear >

6. Connect to permission database >use mysql; (> is the prompt that is already there, don't forget the last semicolon)

6. Change password: > update user set password=password("520") where user="root"; (Don't forget the last semicolon)

7. Refresh permissions (required steps)>flush privileges;

8. Exit > \q

9. Log out of the system, enter, turn on MySQL, and log in with the user name root and the new password 123456 you just set.

 Step 1

C:\Documents and Settings\Administrator>cd D:\web\\Mysql\MySQL Se
rver5.5\bin
C:\Documents and Settings\Administrator>d:
D:\web\\Mysql\MySQL Server5.5\bin>mysqld --skip-grant-tables

Step 2

Microsoft Windows [version 5.2.3790]
(C) Copyright 1985-2003 Microsoft Corp.
C:\Documents and Settings\Administrator>cd D:\web\\Mysql\MySQL Se
rver5.5\bin
C:\Documents and Settings\Administrator>d:
D:\web\\Mysql\MySQL Server5.5\bin>mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.10 MySQL Community Server (GPL)
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use mysql;
Database changed
mysql> update user set password=password("520") where user="root";
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> \q
Bye
D:\web\\Mysql\MySQL Server5.5\bin>

Below is a solution for forgetting root password for mysql under Linux

1. First, confirm that the server is in a secure state, that is, no one can connect to the MySQL database arbitrarily.

Because during the resetting MySQL root password, the MySQL database is completely out of password protection, and other users can also log in and modify MySQL information at will. The server's quasi-security state can be achieved by closing MySQL's external ports and stopping Apache and all user processes. The safest state is to operate on the server's console and unplug the network cable.

2. Modify MySQL login settings:

# vi /etc/

Add a sentence to the paragraph of [mysqld]: skip-grant-tables

For example:

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/
skip-grant-tables

Save and exit vi.

3. Restart mysqld
# /etc//mysqld restart
Stopping MySQL: [ OK ]
Starting MySQL: [ OK ]

4. Log in and modify MySQL's root password

# /usr/bin/mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3 to server version: 3.23.56
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> USE mysql ;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> UPDATE user SET Password = password ( 'new-password' ) WHERE User = 'root' ;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 2 Changed: 0 Warnings: 0
mysql> flush privileges ;
Query OK, 0 rows affected (0.01 sec)
mysql> quit
Bye

5. Modify MySQL login settings back

# vi /etc/
Delete the skip-grant-tables that you just added in the [mysqld] segment
Save and exit vi.

6. Restart mysqld

# /etc//mysqld restart
Stopping MySQL: [ OK ]
Starting MySQL: [ OK ]