Print Current Login User Information

MySQLMySQLBeginner
Practice Now

Introduction

In this project, you will learn how to access the MySQL database and obtain information about the current logged-in user and the system user.

👀 Preview

MariaDB [(none)]> SOURCE /home/labex/project/currentUser.sql;
+----------------+----------------+
| CURRENT_USER() | SYSTEM_USER()  |
+----------------+----------------+
| root@localhost | root@localhost |
+----------------+----------------+
1 row in set (0.000 sec)

ðŸŽŊ Tasks

In this project, you will learn:

  • How to access the MySQL database using the sudo command without a password
  • How to use the CURRENT_USER and SYSTEM_USER functions to retrieve user information
  • How to save the SQL script to a designated file location

🏆 Achievements

After completing this project, you will be able to:

  • Understand how to access the MySQL database and execute SQL statements
  • Retrieve information about the current logged-in user and the system user
  • Save SQL scripts to a specific file location for future use

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL mysql(("`MySQL`")) -.-> mysql/BasicKeywordsandStatementsGroup(["`Basic Keywords and Statements`"]) sql(("`SQL`")) -.-> sql/BasicSQLCommandsGroup(["`Basic SQL Commands`"]) sql(("`SQL`")) -.-> sql/DataManipulationandQueryingGroup(["`Data Manipulation and Querying`"]) mysql/BasicKeywordsandStatementsGroup -.-> mysql/source("`External Code Execution`") sql/BasicSQLCommandsGroup -.-> sql/select("`SELECT statements`") sql/DataManipulationandQueryingGroup -.-> sql/in("`IN clause`") mysql/BasicKeywordsandStatementsGroup -.-> mysql/select("`Data Retrieval`") subgraph Lab Skills mysql/source -.-> lab-301374{{"`Print Current Login User Information`"}} sql/select -.-> lab-301374{{"`Print Current Login User Information`"}} sql/in -.-> lab-301374{{"`Print Current Login User Information`"}} mysql/select -.-> lab-301374{{"`Print Current Login User Information`"}} end

Access MySQL and Obtain User Information

In this step, you will learn how to access the MySQL database using the sudo command and obtain information about the current logged-in user and the system user.

  1. Open a terminal and start the MySQL service using the following command:
sudo service mysql start
  1. Access the MySQL database using the sudo command without any password:
sudo mysql

You should now be in the MySQL prompt, ready to execute SQL statements.

Display Current User and System User Information

In this step, you will write SQL statements to display the current logged-in user and the system user information.

  1. Open the currentUser.sql file.
  2. Add the following SQL statement:
SELECT CURRENT_USER(), SYSTEM_USER();

This statement will use the CURRENT_USER function to obtain information about the currently logged-in user and the SYSTEM_USER function to obtain information about the system user.

Save the SQL Script

In this final step, you will save the SQL script to the designated file location.

  1. Run the script in the MySQL prompt using the following command:
MariaDB [(none)]> SOURCE /home/labex/project/currentUser.sql;

This will execute the SQL script and display the current logged-in user and system user information.

The output should look similar to the following:

+----------------+----------------+
| CURRENT_USER() | SYSTEM_USER()  |
+----------------+----------------+
| root@localhost | root@localhost |
+----------------+----------------+
1 row in set (0.000 sec)

This output shows that the current logged-in user and the system user are both root@localhost.

Congratulations! You have completed the project and saved the SQL script to the designated location.

âœĻ Check Solution and Practice

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other MySQL Tutorials you may like