[Database] Forgot mysql local password

illustrate

I haven't used the local mysql database for a long time and forgot the password.

Steps

1. Enter the bin directory where MySQL is installed, press shift + right mouse button, the words "Open PowerShell window here" will appear, and then click to open PowerShell
insert image description here

2. Enter net stop mysql(or net stop mysql80) to stop the MySQL service (or manually stop in the service. Location: Control Panel\All Control Panel Items\Windows Tools\Services)

insert image description here
insert image description here

3. Enter mysqld --skip-grant-tables Enter
4. Reopen a window, enter the bin directory, enter mysql, and then enter use mysql
5. Modify the database connection password, the command is as follows, the password is set according to your own ideas
update user set password=password("123456") where user="root";
ALTER USER 'root'@'localhost' IDENTIFIED BY '123';
6. Reload the authority, and the password is set The permission information of user root is written into the database from the memory. Enter the command flush privileges;, the English semicolon ";" cannot be omitted. If you do not write ";", enter "\g" after the carriage return, the effect is the same.
7. Exit mysql, enter quit.
8. Open another cmd window, execute it mysqladmin -u root -p shutdown, and then prompt for a password: enter the password you set.
9. Start the database service, enter net start mysql
10. Verify whether the setting is successful. Open cmd separately, enter the bin directory of mysql, enter mysql –u root –p, and press Enter to prompt to enter the password, and then the display is as follows. connection succeeded.
insert image description here

Operation failure resolution

1. In the fourth step of the above operation steps, enter mysql and report an error

The first kind of error

ERROR 1045 (28000): Access denied for user 'ODBC'@'localhost' (using password: NO)

When you do not specify a login user, Windows will use this ODBC user to connect to the MySQL database when connecting to the MySQL database. Regardless of whether the ODBC user exists in your MySQL database, the Windows system will use this ODBC user to try to connect. MySQL database
So, if you don’t want to use mysql -uroot -proot to log in to the MySQL database with two parameters (-u, -p), you just want to enter the MySQL console by directly typing the command mysql, you need Tell, MySQL, you don't want to use the ODBC user selected for you by the Windows system to connect to MySQL, you want to use a user you are familiar with to log in to MySQL, and when logging in, you don't want to enter too many parameters, just use a mysql command, You can log in to the MySQL console.
In a word, you have to solve it, which means you can enter mysql without entering a password.
You can ignore this question, because I don't care.

The solution is as follows

  • 1. Open my.ini, you can search globally, the default location: C:\ProgramData\MySQL\MySQL Server 8.0\my.ini (under the installation path)
  • 2. If you do not have permission to modify, copy it to the desktop, or copy its content to a new text for modification.
  • 3. user=root``password=rootAdd it to my.ini, save it and put it back to the original position.
    insert image description here
  • 4. Enter the service, find the mysql service, click to select properties (if you are mysql80, look for mysql80)
    insert image description here
  • 5. See the path of the executable file of mysql in the properties, and put my.ini in the directory before the bin (in the upper directory of the parent directory bin of the mysqld.exe application)
    insert image description here
  • 6. Enter again at this time mysql, enter mysql, and query again, select user();. Is the current user correct?

insert image description here

The second error

MySQL: 无法将“mysql”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写...
This requires you to reconfigure mysql global variables

The solution is as follows

  • 1. Go to Control Panel\All Control Panel Items\System, open Advanced System Settings
    insert image description here
  • 2. Select Advanced-Environment Variables
    insert image description here
  • 3. Find the path, select it and click Edit
    insert image description here
  • 4. Enter the address of the mysql installation directory with bin, click OK, and close in turn.
    insert image description here

2. Re-operate after the second step from above

Proceed as follows

1. Skip MySQL authentication (login without password), where the address of the my.ini file is modified according to your own situation
mysqld --defaults-file="C:ProgramData\MySQL\MySQL Server 8.0\my.ini" --console --skip-grant-tables --shared-memory

report error

2023-07-07T06:38:28.000247Z 0 [Warning] [MY-010915] [Server] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
2023-07-07T06:38:28.000544Z 0 [System] [MY-013169] [Server] mysql安装目录\MySQL Server 8.0\bin\mysqld.exe (mysqld 8.0.28) initializing of server in progress as process 20396
2023-07-07T06:38:28.003075Z 0 [ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting.
2023-07-07T06:38:28.003087Z 0 [ERROR] [MY-013236] [Server] The designated data directory mysql安装目录\MySQL Server 8.0\Data\ is unusable. You can remove all files that the server added to it.
2023-07-07T06:38:28.016844Z 0 [ERROR] [MY-010119] [Server] Aborting
2023-07-07T06:38:28.018009Z 0 [System] [MY-010910] [Server] mysql安装目录\MySQL Server 8.0\bin\mysqld.exe: Shutdown complete (mysqld 8.0.28)  MySQL Community Server - GPL.

The solution is as follows

  • 1. Empty and delete all the files under the datadir directory in my.ini (violent method)
  • 2. In the bin directory, open cmd and entermysqld --initialize --console
  • 3. Save 'root@localhost:', this is your new password. You can change your password now

2. Continue to operate from the fourth step onwards. Just change the password.

reference link

1. MySQL forgot password, reset root password (pure steps): https://blog.csdn.net/qq_42307920/article/details/127049565
2. Ultimate solution to mysql8.0 ERROR 1045 (28000): Access denied for user ' ODBC'@'localhost' (using password: NO): https://blog.csdn.net/m0_47505062/article/details/122342121
3. initializing of server in progress as process 4656: https://blog.csdn.net /Rbaggio92/article/details/106013390
4. Solution for forgetting the local MySQL database password https://www.cnblogs.com/liusx0303/p/10093050.html
5. Solution: ERROR 1045 (28000): Access denied for user ' ODBC'@'localhost' (using password: NO): https://blog.csdn.net/qq_41550190/article/details/117961327

Guess you like

Origin blog.csdn.net/Daisy74RJ/article/details/131597532