Hosting Operations
Read-only, sensitive outputFind Long-Running MySQL Queries
MySQL is slow and you need to identify queries that have been running the longest.
Command
mysql -e "select id,user,host,db,command,time,state,left(info,80) as info from information_schema.processlist where command <> 'Sleep' order by time desc limit 10;"
Before you run this
System impact: Read-only. Output may expose users, paths, tokens, keys, IPs, process arguments, or log details.
When not to use it: Do not assume every long-running query is unsafe; verify owner and purpose first.
Expected output
A sorted list of active MySQL sessions with runtime and query prefix.
System impact
Read-only, sensitive output. Nothing changes. MySQL prints active non-sleeping sessions sorted by age.
Recovery / rollback: no state is changed.
When to use it
Use during latency spikes, stuck reports, and migration incidents.
When not to use it
Do not assume every long-running query is unsafe; verify owner and purpose first.
Watch this command run
Command transcript
This sanitized transcript shows the commands and output shape without exposing host details.
$ mysql -e "show processlist;"
Id User Host db Command Time State Info
17 app 192.0.2.10 shop Query 91 Sending data select * from orders
18 app 192.0.2.10 shop Sleep 20 NULL
22 report 192.0.2.10 analytics Query 680 Copying to tmp table select customer_id
$ mysql -e "select id,user,host,db,command,time,state,left(info,80) as info from information_schema.processlist where command <> 'Sleep' order by time desc limit 10;"
id user host db command time state info
22 report 192.0.2.10 analytics Query 680 Copying to tmp table select customer_id, sum(total) from orders
17 app 192.0.2.10 shop Query 91 Sending data select * from orders join order_items
$ mysql -e "select command, count(*) from information_schema.processlist group by command;"
command count(*)
Query 2
Sleep 1
View commands shown
These are the commands shown in the sanitized transcript.
Commands shown
mysql -e "show processlist;"mysql -e "select id,user,host,db,command,time,state,left(info,80) as info from information_schema.processlist where command <> 'Sleep' order by time desc limit 10;"mysql -e "select command, count(*) from information_schema.processlist group by command;"
next steps
Related commands
Find Long-Running PostgreSQL Queries
One query can make the whole app look broken.
psql -X -c "select pid, now() - query_start as age, state, left(query, 80) as query from pg_stat_activity where query_start is not null order by age desc limit 10;"
Check PostgreSQL Lock Waits
The outage was a queue, not a crash.
psql -X -c "select pid, wait_event_type, wait_event, state, left(query, 80) as query from pg_stat_activity where wait_event_type is not null order by pid;"
Show MySQL Database Sizes
The storage alert needed a database name.
mysql -e "select table_schema, round(sum(data_length + index_length)/1024/1024, 1) as mb from information_schema.tables group by table_schema order by mb desc;"
Show Active PostgreSQL Connections
The database was not down. It was full.
psql -X -A -F '|' -c "select pid,usename,datname,state,client_addr from pg_stat_activity order by state, pid;"
Show Active MySQL Sessions
The app was waiting behind busy sessions.
mysql -e "show full processlist;"
Study mapping
Use this as independent command practice: read the notes, predict the output, then compare it with the example before using a real shell.
Useful for
- LPIC-1 style command-line practice
- LFCS style performance tasks
- Linux+ style troubleshooting review
Independent study support only. No affiliation, endorsement, exam dumps, or real exam questions.