MySQL | Change User Password

In MySQL, the user account password can be changed using 3 different statements:

  1. UPDATE statement
  2. SET PASSWORD statement
  3. ALTER USER statement.

But before changing the password of an account, two very important things should be kept in mind:

  • The user account details for which you want to change the password.
  • The application is being used by the user whose password you are going to change because if the password is changed without changing the connection string of the application then the application will not be able to connect to the database server.

Let us now learn about how to change the user password in SQL using the three above mentioned SQL statements:



  1. Changing MySQL User Password Using The SET PASSWORD Statement: To change the user password using the SET PASSWORD statement the first requirement is that the account needs to have at least UPDATE privilege.

    The user account should be in “user@host” format whose password you want to update.

    To change the password of a user account “gfguser1” to “newpass” using the SET PASSWORD statement, the following code statement should be executed:

    Syntax:

  2. Changing MySQL User Password Using The ALTER USER statement: The second way to change the password for a user account is to use the ALTER USER statement.

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

    To change the password for the user account “gfguser1” to “newpass” using the Alter User statement, syntax is as shown below:
    Syntax:

  3. Changing MySQL User Password Using UPDATE Statement: The third way to change the password of a user account is by using the 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.

    To change the password for the user account “gfguser1” to “newpass” that connects from the localhost server, syntax is as shown below:

    Syntax:



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.