Is it possible to write a query that will return only the value of a variable returned by MySQL's SHOW STATUS? Ideally I want something like:
SELECT `Value` FROM (SHOW STATUS LIKE 'Com_delete')
or something similar.
Is this possible?
We check the status with the systemctl status mysql command. We use the mysqladmin tool to check if MySQL server is running. The -u option specifies the user which pings the server. The -p option is a password for the user.
The syntax for assigning a value to a SQL variable within a SELECT query is @ var_name := value , where var_name is the variable name and value is a value that you're retrieving. The variable may be used in subsequent queries wherever an expression is allowed, such as in a WHERE clause or in an INSERT statement.
@@ is used for system variables. Using different suffix with @@ , you can get either session or global value of the system variable.
You're correct that STATUS is a keyword, likely owing to SHOW STATUS . If renaming the column isn't much of a chore for you, changing the name is the best choice.
If you are using MySQL 5.1 or above, you should be able to get that data from INFORMATION_SCHEMA
like this for global status:
select VARIABLE_VALUE
from information_schema.GLOBAL_STATUS
where VARIABLE_NAME = 'Com_delete';
Or if you want the session status instead:
select VARIABLE_VALUE
from information_schema.SESSION_STATUS
where VARIABLE_NAME = 'Com_delete';
The default for SHOW STATUS
is SESSION
status, so the latter query would work as a replacement for that.
Complementing Ike Walker answer, INFORMATION_SCHEMA is deprecated on MySQL 5.7.6, as the Performance Schema tables are intended to replace the INFORMATION_SCHEMA tables.
So, by querying the INFORMATION_SCHEMA you may get an error like this:
The 'INFORMATION_SCHEMA.SESSION_STATUS' feature is disabled; see the documentation for 'show_compatibility_56'
According to this, you can set show_compatibility_56
to ON
to enable backwards compatibility and keep using 5.6 syntax, or enable the PERFORMANCE_SCHEMA and query the new tables:
performance_schema.global_variables
performance_schema.session_variables
performance_schema.global_status
performance_schema.session_status
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With