MySQL password operation

Source: Internet
Author: User
Tags mysql command line

Modify MySQL password
In most cases, modifying MySQL requires the root permission in MySQL. Therefore, you cannot change the password unless you request the Administrator.

Method 1
Use phpMyAdmin, which is the simplest. Modify the user table of the MySQL database, but do not forget to use the password function.

Method 2
Use mysqladmin
Mysqladmin-u root-P password mypasswd
After entering this command, you need to enter the original root password, and then the root password will be changed to mypasswd.
Change the root in the command to your username, and you can change your password.
Of course, if your mysqladmin cannot connect to MySQL server, or you cannot execute mysqladmin, this method is invalid, and mysqladmin cannot clear the password.

The following methods are used at the MySQL prompt and must have the root permission of MySQL:

Method 3
Mysql> insert into mysql. User (host, user, password)
Values ('%', 'Jeffrey ', password ('biscuit '));
Mysql> flush privileges
Specifically, this is adding a user with the username Jeffrey and password biscuit.
This example is provided in MySQL Chinese reference manual.
Be sure to use the password function, and then use flush privileges.

Method 4
Similar to method Sany, but the replace statement is used.
Mysql> replace into mysql. User (host, user, password)
Values ('%', 'Jeffrey ', password ('biscuit '));
Mysql> flush privileges

Method 5
Use the SET Password statement,
Mysql> set password for Jeffrey @ "%" = PASSWORD ('biscuit ');
You must use the password () function, but do not need to use flush privileges.

Method 6
Use the grant... identified by statement
Mysql> grant usage on *. * To Jeffrey @ "%" identified by 'biscuit ';
Here, the password () function is unnecessary and does not need to be flush privileges.

Note: Password () [not] implements password encryption in the same way as UNIX password encryption.
How to fix MySQL password loss
If MySQL is running, first disable: killall-term mysqld
Start MYSQL: Bin/safe_mysqld -- skip-grant-tables &
You can access MySQL without a password.
Then
> Use MySQL
> Update user SET Password = PASSWORD ("new_pass") where user = "root ";
> Flush privileges;
Restart MySQL and start MySql in a normal way.

Clear MySQL password
Windows:
1. log on to the system as a system administrator.
2. Stop MySQL services.
3. Go to the Command window and enter the MySQL installation directory. For example, if my installation directory is c: \ mysql, go to c: \ mysql \ bin.
4. Skip the permission check and start MySQL,
C: \ mysql \ bin> mysqld-NT -- skip-grant-tables
5. open a new window, go to the C: \ mysql \ bin directory, and set the new root password.
C: \ mysql \ bin> mysqladmin-u root flush-Privileges password "newpassword"
C: \ mysql \ bin> mysqladmin-u root-P Shutdown
Replace newpassword with the root password you want to use. The second command will prompt you to enter a new password and repeat the password entered by the first command.
6. Stop MySQL server and start MySql in Normal Mode
7. You can use a new password to link to MySQL.

UNIX & Linux:
1. log on to the system using root or a user running mysqld;
2. Use the kill command to end the mysqld process;
3. Use the -- skip-grant-tables parameter to start MySQL Server
Shell> mysqld_safe -- skip-grant-tables &
4. Set a new password for root @ localhost
Shell> mysqladmin-u root flush-Privileges password "newpassword"
5. Restart MySQL Server

MySQL password change
To modify MySQL, run the following command on the MySQL command line:
Mysql-u root MySQL
Mysql> Update user SET Password = PASSWORD ("new password") where user = 'name ';
Mysql> flush privileges;
Mysql> quit

How to restore the password of the MySQL database

Because the MySQL password is stored in the User table in the MySQL database, you only need to copy the user table in MySQL under Windows 2003 to overwrite it.

In C: \ mysql \ data \ mysql \ (Linux, there are three user table related files in the/var/lib/MySQL/) directory. FRM, user. MYD, user. myi

User. frm // user Table Style File

User. MYD // User table data file

User. myi // User table index file

For the sake of insurance, all three are copied. However, if the table structure has not been changed on the MySQL to be restored, just copy user. MYD.

Then

#./Etc/rc. d/init. d/MySQL stop
#./Etc/rc. d/init. d/MySQL start
# Mysql-u root-P xxxxxx

Okay, you can log in with the MySQL password in Windows 2003.

Mysql> use MySQL
Mysql> Update user SET Password = PASSWORD ('xxxxxx') where user = 'root ';

An error occurs, prompting that the user table has only the read permission.

I analyzed the reason, because the user. * File Permission is assigned in Windows 2003, and in Windows 2003, my LS-l permission is 666.

In Linux, I can see that after copying the file, the permission is changed to 600 (in fact, under normal circumstances, only the file owner here is not mysql, the copied owner is changed to root, so there will be insufficient permissions. At this time, if you change to 666 permissions, It will be okay, of course, this is not good, there is no substance to solve the problem ), check LS-l under/var/lib/MySQL /.

Again
# Chown-r MYSQL: MySQL user .*
# Chmod 600 user .*

// OK, done

Restart MySQL

Reconnect

Mysql> use MySQL
Mysql> Update user SET Password = PASSWORD ('xxxxxx') where user = 'root ';
Mysql> flush privileges;

NOTE: If MySQL is configured by default in Windows, you must execute

Mysql> Delete from user where user = '';
Mysql> Delete from user where host = '% ';
Mysql> flush privileges;

Now, the password recovery process is complete.

This method has some limitations. You must have another user table file.

OthersThere are several methods

Other methods 1(This is a widely used method on the Internet. It is in the MySQL Chinese Reference Manual)

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 an equivalent user on the server you run to perform this operation.

2. Use the '-- skip-grant-tables' parameter to start mysqld. In Linux:

/Usr/bin/safe_mysqld -- skip-grant-tables, c: \ mysql \ bin \ mysqld -- skip-grant-tables in Windows

3. log on to the mysqld server without a password,

> Use MySQL
> Update user SET Password = PASSWORD ("new_pass") where user = "root ";
> Flush privileges;

You can also do this:

'Mysqladmin-H hostname-u user password' new password''

4. Load the permission table:

'Mysqladmin-H hostname flush-Privileges'

Or use SQL commands

'Flush privileges'

5. killall-term mysqld

6. log in with a new password

Other method 2

Directly use the hexadecimal editor to edit the user. MYD File

But here I want to explain that I found a problem when editing here. Some encrypted password strings are stored continuously, and some of the last two digits are cut, the last two digits are stored in other places. I haven't understood this yet. note that the encrypted password string is edited, that is, you still need to have another user table file. The difference between this method and the method described above is that this method directly edits the user table file in Linux, and you do not need to change the file owner and permissions.

 

In WindowsActual OperationAs follows:

1. Disable running MySQL.

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

3. Input
Mysqld-NT -- skip-grant-tables

Press enter. If no prompt is displayed, it is correct.

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

5. Enter MySQL and press Enter. If yes, a MySQL prompt is displayed.>

6. Connect to the permission Database

> Use MySQL;
(> It is an existing prompt. Don't forget the last Semicolon)

6. Change the password:

> Update user SET Password = PASSWORD ("123456") where user = "root"; (do not forget the last semicolon)

7. Refresh permissions (required steps)

> Flush privileges;

8. Exit

> \ Q

9. log out of the system, log on to MySQL, and log on to MySQL with the username "root" and the new password "123456.

It is said that it can be usedDirectly modify the user tableFile method:

Close mysql. In Windows, open MySQL \ data \ mysql. There are three file users. FRM, user. MYD, user. myi finds a MySQL with a password and replaces these three files. If the user table structure has not been changed, no one will change it. Replace the user. MYD.

You can also directly edit user. MYD and find a hexadecimal editor. ultraedit has this function. Close MySQL and enable user. MYD. Change the eight characters after the root user name to 565491d704013245, and the new password is 123456. Or change the hexadecimal numbers corresponding to them (one character on the left corresponds to two numbers) to 00 02 02 02 02 02 02 02 02 02. This is an empty password, the asterisk * is displayed on the right of the editor, which looks like a decimal point. Restart MySQL and enter root and your new password.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.