Home > Database > Mysql Tutorial > How to log in to MySQL as a different user?

How to log in to MySQL as a different user?

王林
Release: 2023-09-08 14:29:04
forward
1021 people have browsed it

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 −
Copy after login

To understand the above syntax, let us create a user in MySQL. The syntax is as follows −

CREATE USER 'yourUserName'@'localhost' IDENTIFIED BY 'yourPassword';
Copy after login

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)
Copy after login

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;
Copy after login

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)
Copy after login

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)
Copy after login

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 -

How to log in to MySQL as a different user?

#After pressing the “OK” button, you will see the command prompt. The screenshot is as follows -

How to log in to MySQL as a different user?

# then reach the location of the trash can. Please follow these steps:

Step 1-

How to log in to MySQL as a different user?

Step 2-

How to log in to MySQL as a different user?

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 -

How to log in to MySQL as a different user?

# After pressing the Enter key, you need to enter your password. The query is as follows -

How to log in to MySQL as a different user?

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template