Get command history in MySQL
This article highlights various ways to get a history of MySQL commands we have executed in Windows and Linux.
MySQL Command History
We will use Windows and Linux (Ubuntu 20.04) operating systems to understand different ways to get MySQL command history.
For Windows operating systems, we will use MySQL Workbench and the Windows command line (also known as cmd and command prompt) to access the history of MySQL commands in the form of files and tables.
For Linux operating systems, we will use the Linux shell (terminal) to access the MySQL command history.
Get command history in MySQL using Windows command line
Here, we will explore three ways to get command history in MySQL.
Get the history of all MySQL commands in a tabular format
- Open a Windows command line and go to the MySQL bin folder which is located by default at C:\Program Files\MySQL\MySQL Server 8.0\bin.
- Log in to MySQL using your login credentials.
-
Run the following two queries to turn on the MySQL query log and prepare the history of MySQL commands in a tabular form.
SET GLOBAL log_output = 'table'; SET GLOBAL general_log = 'on';
-
Execute the following query to get the history of executed MySQL commands as output.
In the query above, we useSELECT a.*, CONVERT(a.argument USING utf8) FROM mysql.general_log a;
convert()
the getHex method to convert the hexadecimal value of the parameter field to text. You can also get the parameter attribute value as a hexadecimal value using MySQL 5.7 or later.
Since we have limited space on the command line, we select specific fields as follows:
SELECT a.event_time,a.user_host, a.thread_id, a.server_id,
CONVERT(a.argument USING utf8) AS arugment
FROM mysql.general_log a LIMIT 10;
Output:
Get the history of all MySQL commands in a file
This method is very similar to the previous one. We just need to execute the following query after logging into MySQL.
SET GLOBAL log_output = 'file';
SET GLOBAL general_log_file = "/yourPath/logfile.log";
SET GLOBAL general_log = 'on';
Now, find the file at the specified path and open it to see all the details. Typically, the filename.log file looks like the following.
Output:
Get the history of the last MySQL commands executed in the specified session
We can also find the last executed MySQL command by using the following query for a specific session only. We do this here for root and we only select two fields due to limited space on the command line.
SELECT a.event_time,CONVERT(a.argument USING utf8) AS arugment
FROM mysql.general_log a WHERE user_host LIKE 'root%'
ORDER BY a.event_time DESC LIMIT 1;
Output:
Getting MySQL command history using MySQL Workbench on Windows
We have three methods here as well to retrieve the history of MySQL commands. These methods are the same as we discussed using Windows command line. But this time, we will do it on MySQL Workbench.
Get MySQL command history in table format
We execute the following query to get the history of MySQL commands in tabular form. Here, the convert() method converts the blob value of the parameter field to text.
If you are using MySQL 5.7 or higher, you can also get the value of a parameter property as a blob in Workbench.
SET GLOBAL log_output = 'table';
SET GLOBAL general_log = 'on';
SELECT a.*, CONVERT(a.argument USING utf8) FROM mysql.general_log a;
Output:
Instead of writing a query, we can use the History Output option (highlighted in the screenshot below) to get a history of commands.
Get MySQL command history in one or multiple files
We can get all the history in a file in a specified path.
SET GLOBAL log_output = 'file';
SET GLOBAL general_log_file = "/yourPath/logfile.log";
SET GLOBAL general_log = 'on';
We can also go to the C:\Users\DELL\AppData\Roaming\MySQL\Workbench\sql_history location to see all the individual files with MySQL command history.
Get MySQL command history of last executed query
SELECT a.*,CONVERT(a.argument USING utf8) AS arugment
FROM mysql.general_log a WHERE user_host LIKE 'root%'
ORDER BY a.event_time DESC LIMIT 1;
Output:
Get MySQL command history using Ubuntu terminal
Run the following command while logged in as superuser.
cat ~/.mysql_history
Output:
You can run the following command to eliminate the spaces and make it more readable.
sed "s/\\\040/ /g" < ~/.mysql_history
Output:
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Display tables and database structure in MySQL
Publish Date:2025/04/23 Views:97 Category:MySQL
-
Today, we will learn about queries in MySQL that can display the table and database structure. We will use the mysqldump utility, DESCRIBE the , SHOW TABLES and SHOW CREATE TABLE the statements. We are using MySQL version 8.0.28 while writi
Select first row from MySQL table
Publish Date:2025/04/23 Views:112 Category:MySQL
-
Today, we will explore three scenarios and their solutions where we want to select the first row from a MySQL table. In the first scenario, we will learn to get the first row from a MySQL table where there are multiple instances of a partic
Insert timestamp into MySQL table
Publish Date:2025/04/23 Views:77 Category:MySQL
-
Today, we will learn how to TIMESTAMP insert date and time into a type column of a MySQL table according to the table definition. Create a MySQL table First, we will create the tables that we will use in this tutorial. Sample code: CREATE T
The difference between two tables in MySQL
Publish Date:2025/04/23 Views:102 Category:MySQL
-
In this article, we will learn how to find the difference between two tables in MySQL. The difference between two tables in MySQL We often need to compare two tables to find records in one table that have no matching records in the other ta
MySQL sorts data alphabetically
Publish Date:2025/04/23 Views:129 Category:MySQL
-
In this article, we will learn about various ways to sort data alphabetically in MySQL. Sort MySQL data alphabetically When you use the SELECT command to query data from a table, the rows in the result set are in arbitrary order. To order t
Display the current database in MySQL
Publish Date:2025/04/23 Views:199 Category:MySQL
-
This article focuses on the various queries that can be used to display the current database in MySQL. We will learn by using the Windows command line and MySQL Workbench. Display the current database in MySQL We can use the following query
Check if a database exists in MySQL
Publish Date:2025/04/23 Views:179 Category:MySQL
-
In this article, we will introduce many ways to check if a database exists in MySQL. Check if the database exists in MySQL The system schema is the schema that MySQL uses. It includes tables that contain data needed by a running MySQL serve
Get the sum of multiple columns in MySQL
Publish Date:2025/04/23 Views:125 Category:MySQL
-
In this article, we will learn how to sum multiple columns in MySQL. Sum multiple columns in MySQL You can use aggregate functions SUM() to calculate the total value in a collection. SUM() The function calculation does not consider NULL val
MySQL ForEach Loop
Publish Date:2025/04/23 Views:164 Category:MySQL
-
This article describes how to simulate a foreach loop in MySQL using INSERT, SELECT, WHERE, and JOIN in one statement. MySQL foreach loop To understand foreach loop simulation, let us create three tables with the following names and attribu