Documentation Home
MySQL 8.0 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 32.8Mb
PDF (A4) - 32.8Mb
PDF (RPM) - 30.8Mb
HTML Download (TGZ) - 7.8Mb
HTML Download (Zip) - 7.9Mb
HTML Download (RPM) - 6.7Mb
Man Pages (TGZ) - 143.5Kb
Man Pages (Zip) - 203.6Kb
Info (Gzip) - 3.0Mb
Info (Zip) - 3.0Mb

Pre-General Availability Draft: 2017-07-24 SET PASSWORD Syntax

SET PASSWORD [FOR user] = password_option

password_option: {
  | 'auth_string'

The SET PASSWORD statement assigns a password to a MySQL user account, specified as either a cleartext (unencrypted) or encrypted value:

  • 'auth_string' represents a cleartext password.

  • 'hash_string' represents an encrypted password.


SET PASSWORD can be used with or without an explicitly named user account:

  • With a FOR user clause, the statement sets the password for the named account, which must exist:

    SET PASSWORD FOR 'jeffrey'@'localhost' = password_option;

    In this case, you must have the UPDATE privilege for the mysql database.

  • With no FOR user clause, the statement sets the password for the current user:

    SET PASSWORD = password_option;

    Any client who connects to the server using a nonanonymous account can change the password for that account. To see which account the server authenticated you as, invoke the CURRENT_USER() function:


When the read_only system variable is enabled, SET PASSWORD requires the SUPER privilege in addition to any other required privileges.

If a FOR user clause is given, the account name uses the format described in Section 6.2.4, “Specifying Account Names”. The user value should be given as 'user_name'@'host_name', where 'user_name' and 'host_name' are exactly as listed in the User and Host columns of the account's mysql.user table row. The host name part of the account name, if omitted, defaults to '%'. For example, to set the password for an account with User and Host column values of 'bob' and '', write the statement like this:

SET PASSWORD FOR 'bob'@'' = PASSWORD('auth_string');

The password can be specified in these ways:

  • Using the PASSWORD() function (deprecated as of MySQL 5.7.6)

    The 'auth_string' function argument is the cleartext (unencrypted) password. PASSWORD() hashes the password and returns the encrypted password string for storage in the mysql.user account row.

    The PASSWORD() function hashes the password using the hashing method determined by the value of the old_passwords system variable value. If SET PASSWORD rejects the hashed password value returned by PASSWORD() as not being in the correct format, it may be necessary to change old_passwords to change the hashing method. For example, if the account uses the mysql_native_password plugin, the old_passwords value must be 0:

    SET old_passwords = 0;
    SET PASSWORD FOR 'jeffrey'@'localhost' = PASSWORD('mypass');

    If the old_passwords value differs from that required by the authentication plugin, the hashed password value returned by PASSWORD() is not acceptable for that plugin, and attempts to set the password produce an error. Permitted old_passwords values are described later in this section.

  • Using a string without PASSWORD()

    SET PASSWORD interprets the string as a cleartext string and hashes it appropriately for the account authentication plugin before storing it in the mysql.user account row.

For more information about setting passwords, see Section 6.3.7, “Assigning Account Passwords”.

The following table shows the permitted values of old_passwords, the password hashing method for each value, and which authentication plugins use passwords hashed with each method.

ValuePassword Hashing MethodAssociated Authentication Plugin
0MySQL 4.1 native hashingmysql_native_password
2SHA-256 hashingsha256_password

Under some circumstances, SET PASSWORD may be recorded in server logs or on the client side in a history file such as ~/.mysql_history, which means that cleartext passwords may be read by anyone having read access to that information. For information about the conditions under which this occurs for the server logs and how to control it, see Section, “Passwords and Logging”. For similar information about client-side logging, see Section, “mysql Logging”.

If you are using MySQL Replication, be aware that, currently, a password used by a replication slave as part of a CHANGE MASTER TO statement is effectively limited to 32 characters in length; if the password is longer, any excess characters are truncated. This is not due to any limit imposed by the MySQL Server generally, but rather is an issue specific to MySQL Replication. (For more information, see Bug #43439.)

User Comments
Sign Up Login You must be logged in to post a comment.