Introduction
You must now be familiar with MySQL, its features and its functionality. In this blog, we will be exploring a new component of MySQL, which is, manipulating a user account. A user account in MySQL contains many things like the username, password, information of the host and user privileges. MySQL offers many ways for manipulating a user account like-
- Changing a user password.
- Locking a user account.
- Unlocking a user account.
This blog will discuss all these ways one by one.
So, let’s get started.
Changing a User Password
Sometimes, a situation arises where we need to change a user password. For changing a user's password, we must have some information about the user, like
- The user account's username.
-
The name of the application which that user account is using.
The application’s name is required because we must connect to that application for changing the user password. If not connected, then the application will not be able to connect with the database server.
In MySQL, there are three ways of changing a user password. These are-
- UPDATE Command.
- SET PASSWORD Command.
- ALTER USER Command.
Let’s discuss them all one by one.
UPDATE Command
The first way to change a user password is to use the UPDATE command. This command updates the user table of the MySQL database.
Before executing the UPDATE command, we must connect to the user's application (in this case, MySQL). After executing the UPDATE command, we must execute another command, i.e., the FLUSH PRIVILEGES command to reload the user's privileges from the grant table of the MySQL database.
Below is the syntax to use the UPDATE command-
USE application_name;
UPDATE user SET authentication_string = PASSWORD(‘new password’) WHERE user = ‘username’ AND host = ‘hostname’;
FLUSH PRIVILEGES;
where,
application_name is the name of the application the user is using.
new password is the new password that we want to set for a user.
username is the username of the user account.
hostname is the name of the host.
Let’s take a look at an example of the UPDATE command.
Suppose we have a user ‘ninja’ and host as ‘localhost’ using the MySQL application. We want to change the user ninja’s password to ‘iamninja’. We will execute the following query to do so-
mysql> USE mysql;
mysql> UPDATE user SET authentication_string = PASSWORD('iamninja') WHERE user = 'ninja' AND host = 'localhost';
mysql> FLUSH PRIVILEGES;
The above query will change the user ninja’s password.
SET PASSWORD Command
The second way to change a user password is to use the SET PASSWORD command. For using this command, we must have an UPDATE privilege. We don’t need to connect to the application the user is using or execute the FLUSH PRIVILEGES command in this command.
Below is the syntax to use the SET PASSWORD command-
SET PASSWORD FOR username@hostname = PASSWORD(‘new password’);
where,
username is the username of the user account.
hostname is the name of the host.
new password is the new password that we want to set for a user.
Let’s take a look at an example of the SET PASSWORD command.
Suppose we have a user ‘ninja’ and a host as ‘localhost’. We want to change the user ninja’s password to ‘codingninjas’. We will execute the following query to do so-
mysql> SET PASSWORD FOR ninja@localhost = PASSWORD('codingninjas');
MySQL deprecated this syntax in version 5.7.6 and may remove it in future releases. Instead, it employs the following plaintext password:
mysql> SET PASSWORD FOR ninja@localhost = codingninjas;
The above query will change the user ninja’s password.
ALTER USER Command
The third way to change a user password is to use the ALTER USER command along with the IDENTIFIED BY clause.
Below is the syntax to use the ALTER USER command-
ALTER USER username@hostname IDENTIFIED BY ‘new password’;
where,
username is the user account's username.
hostname is the name of the host.
new password is the new password that we want to set for a user.
Let’s take a look at an example of the ALTER USER command.
Suppose we have a user ‘ninja’ and a host as ‘localhost’. We want to change the user ninja’s password to ‘ilovecoding’. We will execute the following query to do so-
mysql> ALTER USER ninja@localhost IDENTIFIED BY 'ilovecoding';
The above query will change the user ninja’s password.
Note: If we want to reset the password for the MySQL root account, we need to force the MySQL database server to stop and restart without using grant table validation.
Next, we will study how to lock a user account.