If you want to log in to MySQL as another user, you need to use the "mysql -u -p command". The syntax for logging in as a different user is as follows.
>mysql -u yourUsername -p After pressing enter key Enter password −
To understand the above syntax, let us create a user in MySQL. The syntax is as follows −
CREATE USER 'yourUserName'@'localhost' IDENTIFIED BY 'yourPassword';
Now I want to create a user named "John" with the password "john123456". The query is as follows -
mysql> CREATE USER 'John'@'localhost' IDENTIFIED BY 'john123456'; Query OK, 0 rows affected (0.15 sec)
Now check if the user has been created in the MySQL.user table. Query as follows to list all users in MySQL.user table −
mysql> select user from MySQl.user;
Following is the output -
+------------------+ | user | +------------------+ | Manish | | User2 | | mysql.infoschema | | mysql.session | | mysql.sys | | root | | Adam Smith | | John | | User1 | | am | +------------------+ 10 rows in set (0.00 sec)
Now you need to grant all permissions to user John. The query is as follows:
mysql> GRANT ALL PRIVILEGES ON * . * TO 'John'@'localhost'; Query OK, 0 rows affected (0.19 sec)
Now to log in as another user, first open the command prompt by pressing the Windows R shortcut key and type CMD. Now you need to press the "OK" button. The screenshot is as follows -
#After pressing the “OK” button, you will see the command prompt. The screenshot is as follows -
# then reach the location of the trash can. Please follow these steps:
Step 1-
Step 2-
Now use the above syntax discussed at the beginning. Enter the username "John" and password "john123456" we created above to log in. The command is as follows -
# After pressing the Enter key, you need to enter your password. The query is as follows -
The above is the detailed content of How to log in to MySQL as a different user?. For more information, please follow other related articles on the PHP Chinese website!