If Mysql is not accessed for a period of time, you may forget the access password. What should I do? reinstall mysql? The cost is too high. Here we will introduce two methods to restore the password.
Method 1: Because the Mysql password is stored in the user table in the mysql database, we only need to copy and overwrite the user table with the known password.
In Linux, there are three user table files in the/var/lib/mysql/directory: user. frm, user. MYD, and user. MYI. They represent:
User. frm // user Table Style File
User. MYD // user table data file
User. MYI // user table index file
For the sake of insurance, we will first back up the files related to the three user tables ,,
Back up three related files in the user table
Then we copy the three files in the user table in the mysql database with the password. Then we can use ls to view the six user table files.
In this case, you only need to restart the mysql service to access the Mysql database with a known password.
Restart command: service mysql restart or
/Etc/init. d/mysql restart
Now we can access the Mysql database with a known password.
Content navigation
Method 2:
The first method is to restore the password of another Mysql database without such a known password. How can we recover the database? We use a method described in the Mysql Chinese Reference Manual, which involves several steps:
1: Send the kill command to the mysqld server to disable the mysqld server (not kill-9). The files that store the process ID are usually located in the directory of the MYSQL database.
Killall-TERM mysqld // you must be a UNIX root user or your running
SERVER.
2: Use the '-- skip-grant-tables' parameter to start mysqld. (See/usr/bin/safe_mysqld -- skip-grant-tables in LINUX and c: mysqlbinmysqld -- skip-grant-tables in windows;
Result of steps 1 and 2
3: Then you can log on to the mysqld server without a password. At this time, the Mysql password is the same as the one you just installed and is empty. Now we can use mysqladmin to change the mysql password in figure 6.
4: load the permission table. You can use mysqladmin flush-privileges to refresh the permission table.
Use mysqladmin to load the permission table
5: log on with the new password.
Mysql is a powerful database that requires more practice in practice. This article describes how to install Mysql and the most basic commands for accessing and changing passwords, finally, we will introduce two methods to restore the Mysql password. Hope to help you.
- Delete MySQL database data recovery in Linux EXT3
- Add an ODBC driver for MySQL in Ubuntu
- LAMP (Linux + Apache + Mysql + PHP) Environment Construction