Pre-General Availability Draft: 2017-07-17
Required credentials for clients that connect to the MySQL server can include a password. This section describes how to assign passwords for MySQL accounts.
MySQL stores passwords in the user table in the
mysql system database. Operations that assign
or modify passwords are permitted only to users with the
CREATE USER privilege, or,
alternatively, privileges for the mysql
database (INSERT privilege to
create new accounts, UPDATE
privilege to modify existing accounts). If the
read_only system variable is
enabled, use of account-modification statements such as
CREATE USER or
SET PASSWORD additionally requires
CONNECTION_ADMIN or the
SUPER privilege.
The discussion here summarizes syntax only for the most common password-assignment statements. For complete details on other possibilities, see Section 13.7.1.3, “CREATE USER Syntax”, Section 13.7.1.1, “ALTER USER Syntax”, Section 13.7.1.6, “GRANT Syntax”, and Section 13.7.1.10, “SET PASSWORD Syntax”.
MySQL hashes passwords stored in the mysql.user
table to obfuscate them. For most statements described here, MySQL
automatically hashes the password specified. An exception is
SET PASSWORD ... =
PASSWORD(', for
which you use the auth_string')PASSWORD()
function explicitly to hash the password. There are also syntaxes
for CREATE USER,
ALTER USER,
GRANT, and SET
PASSWORD that permit hashed values to be specified
literally; for details, see the descriptions of those statements.
MySQL uses plugins to perform client authentication; see Section 6.3.10, “Pluggable Authentication”. The authentication plugin associated with an account determines the algorithm used to hash passwords for that account.
To assign a password when you create a new account, use
CREATE USER and include an
IDENTIFIED BY clause:
mysql> CREATE USER 'jeffrey'@'localhost'
-> IDENTIFIED BY 'mypass';
For this CREATE USER syntax, MySQL
automatically hashes the password before storing it in the
mysql.user table.
CREATE USER also supports syntax
for specifying the account authentication plugin. See
Section 13.7.1.3, “CREATE USER Syntax”.
To assign or change a password for an existing account, use one of the following methods:
Use the
ALTER USERstatement with anIDENTIFIED BYclause:mysql> ALTER USER 'jeffrey'@'localhost' -> IDENTIFIED BY 'mypass';If you are not connected as an anonymous user, you can change your own password without naming your own account literally:
mysql> ALTER USER USER() -> IDENTIFIED BY 'mypass';For these
ALTER USERsyntaxes, MySQL automatically hashes the password before storing it in themysql.usertable.Use
SET PASSWORDwith thePASSWORD()function:mysql> SET PASSWORD FOR -> 'jeffrey'@'localhost' = PASSWORD('mypass');If you are not connected as an anonymous user, you can change your own password by omitting the
FORclause:mysql> SET PASSWORD = PASSWORD('mypass');The
PASSWORD()function hashes the password using the hashing method determined by the value of theold_passwordssystem variable value. IfSET PASSWORDrejects the hashed password value returned byPASSWORD()as not being in the correct format, it may be necessary to changeold_passwordsto change the hashing method. See Section 13.7.1.10, “SET PASSWORD Syntax”.NoteUse of
SET PASSWORD ... = PASSWORD('for password modification is deprecated as of MySQL 5.7.6. Useauth_string')ALTER USERinstead.Use
SET PASSWORDwithout thePASSWORD()function:SET PASSWORDinterprets the string as a cleartext string and hashes it appropriately for the account authentication plugin before storing it in themysql.useraccount row.mysql> SET PASSWORD FOR -> 'jeffrey'@'localhost' = 'mypass';Use a
GRANT USAGEstatement at the global level (ON *.*) to change an account password without affecting the account's current privileges:mysql> GRANT USAGE ON *.* TO 'jeffrey'@'localhost' -> IDENTIFIED BY 'mypass';For this
GRANTsyntax, MySQL automatically hashes the password before storing it in themysql.usertable.NoteUse of
GRANTfor password modification is deprecated as of MySQL 5.7.6. UseALTER USERinstead.To change an account password from the command line, use the mysqladmin command:
shell> mysqladmin -u user_name -h host_name password "new_password"The account for which this command sets the password is the one with a
mysql.usertable row that matchesuser_namein theUsercolumn and the client host from which you connect in theHostcolumn.For password changes made using mysqladmin, MySQL automatically hashes the password before storing it in the
mysql.usertable.