MySQL – Different Methods to Know Current User

There are different methods to know the name of the current user in MySQL. There can be many reasons we want to know who is the current user. Here is list of the of the few of the such reasons.

  1. Customized code which we want to run based on the reason.
  2. Checking the permissions based on the user.
  3. Inserting the name of the user in the audit table.
  4. Displaying special message based on the login user.
  5. Executing some special logic/code based on the logged in user.

Method 1: Usage of system functions

SELECT user();
SELECT current_user();

Both the above SELECT statements return the name of the current user, which executes the above statements.

MySQL - Different Methods to Know Current User mysql-current-user

Method 2: Usage of system table

SELECT * from mysql.user;

The above lists out all the users registered in the server. It will also list out various levels of privileges for the users.

To get only the information about the current user, you can use

SELECT * from mysql.user WHERE user() like concat(user,'%');

You can also use synonym functions of user()

SELECT session_user();
SELECT system_user();

Both the above will return the result which is also the result of SELECT user().

If you are using a similar login in your application or business, I would like to know what is the business need for the same. Please leave a comment.

Reference: Pinal Dave (https://blog.sqlauthority.com)

,
Previous Post
MySQL – How to Drop Table If Exists in Database?
Next Post
SQL SERVER – Network Name resource fails to come online in a Windows Server 2008 R2 Failover Cluster

Related Posts

2 Comments. Leave new

  • Please post the results of Method-2. Also, why are there two commands for Method-1 when both of them display the same result? Is there a strong reason?

    Reply
    • My friend – every reason shows the same result…test
      this is a test preformed by siteground

      Reply

Leave a Reply

Menu