Changing User password

After creating a user account, the next step is learning how to change the password. Changing passwords is necessary to ensure the security of the database system.

There are three methods to change the password. One of these three statements can be used:

  • SET PASSWORD statement

  • ALTER USER statement

  • UPDATE statement

Changing MySQL User Password Using the SET PASSWORD Statement

For this statement to work, the account must at least have the UPDATE privilege. The user account should also be in the format user@host

SYNTAX:

SET PASSWORD FOR ‘username’@’host’ = ‘newpassword’;

EXAMPLE:

SET PASSWORD FOR ‘student’@’host’ = ‘DataAnalysisRocks’;

The above statement will set the new password for the user student.

Changing MySQL User Password Using the ALTER USER statement

The ALTER USER statement is used along with the IDENTIFIED BY clause.

SYNTAX:

ALTER USER ‘username’@’host’ IDENTIFIED BY ‘newpassword’;

EXAMPLE:

ALTER USER ‘student’@’host’ IDENTIFIED BY ‘DataAnalysisRocks’;

Changing MySQL User Password Using UPDATE Statement

The UPDATE statement updates the user table of the MySQL database. The FLUSH PRIVILEGES statement needs to be executed after executing the UPDATE statement.

The FLUSH PRIVILEGES statement is used to reload privileges from the grant table in the MySQL database.

SYNTAX:

UPDATE mysql.user

SET authentication_string = PASSWORD(‘newpassword’)

WHERE User=’username’ AND host=’host’;

FLUSH PRIVILEGES;

EXAMPLE:

UPDATE mysql.user

SET authentication_string = PASSWORD(‘DataAnalysisRocks’)

WHERE User=’student’ AND host=’host’;

FLUSH PRIVILEGES;