Practical MySQL Query Troubleshooting for Slow or Stuck SQL

Practical MySQL Query Troubleshooting for Slow or Stuck SQL

Who This Is For (And What’s Going Wrong)

You’re running MySQL queries, and something feels wrong.
Maybe the prompt doesn’t come back.
Maybe your app feels like it’s moving through mud.

If you:

  • Work with PHP & MySQL
  • Maintain a small business site or app
  • Or just started poking around MySQL from the command line

…this guide will walk you through two super common MySQL issues:

  1. Queries that appear to hang because they’re not actually finished (missing semicolon problem)
  2. Queries that run but are painfully slow (slow query problem)

We’ll keep it practical:
short steps, clear examples, and just enough explanation so you know what you’re changing.


Step 1: Fix Queries That “Hang” Because of Missing Semicolons

First simple problem: you type a query, hit ENTER… and MySQL just gives you a new line.
No error, no result, just a different prompt.

In MySQL, queries must end with a semicolon (;) to actually execute.
Until MySQL sees that semicolon, it assumes you’re still typing the same query.

How to Spot This in the MySQL Prompt

You might see something like this in your terminal:

mysql> SHOW * FROM table_name
    ->

That -> prompt means MySQL is waiting for you to finish the statement.
The query hasn’t run yet.

If you then type ; and press ENTER:

    -> ;

Now MySQL will finally execute the query.

What To Do When Your Query Seems Stuck

If your query looks stuck or the prompt changed to something like -> or just keeps going to a new line:

  1. Check if you missed the semicolon.
  2. Look at your last line.
  3. If there’s no ; at the end, MySQL is still waiting.
  4. Finish the query with a semicolon.
  5. Just type ; and press ENTER.
  6. Example:
    sql
    -> ;
  7. Re-type correctly if you messed up the query.
  8. If the statement is wrong and you just want to cancel, the simplest way is to:
    • Press Ctrl + C in the terminal to exit the MySQL client, then
    • Log back in and type the query again, this time ending with ;.

Common Gotcha

If you copy-paste queries from somewhere else, sometimes the semicolon doesn’t come along, or there’s a line break after it.
Always double-check the last character before you hit ENTER.

Once you get into the habit of ending with ;, this problem disappears.


Step 2: When Queries Are Slow – Use the Slow Query Log

Now the more annoying issue: queries do run, but they’re very slow.

Instead of guessing which query is causing the problem, MySQL has a built-in tool for this: the slow query log.
When enabled, MySQL will write any slow queries into a log file so you can review them later.

You don’t need any fancy tools for this step, just:

  • SSH access to the server
  • Sudo/root permission to edit MySQL config

We’ll walk through how to turn the slow query log on and what each setting means.

Safety note: You’re editing a MySQL config file here.
A small typo can stop MySQL from starting.
Make a quick backup of the config file before you change anything.


Step 3: Edit `mysqld.cnf` to Enable Logging

The main MySQL server config file we care about is typically here:

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

You can use nano or any text editor you like.
But use sudo, because this file is root-owned.

Make a Quick Backup (Recommended)

Before editing, create a copy of the file:

sudo cp /etc/mysql/mysql.conf.d/mysqld.cnf \
        /etc/mysql/mysql.conf.d/mysqld.cnf.bak

If something breaks, you can restore it:

sudo mv /etc/mysql/mysql.conf.d/mysqld.cnf.bak \
        /etc/mysql/mysql.conf.d/mysqld.cnf

Then restart MySQL if needed.

Find the Slow Query Log Section

Open the file:

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Scroll until you see a section with lines like this (they may be commented out with #):

#slow-query-log = 1
#slow-query-log-file = /var/log/mysql-slow.log
#long_query_time = 2
#log_queries_not_using_indexes

These lines control how the slow query log behaves.
Right now, if they start with #, they’re disabled.

What Each Directive Does (In Plain Language)

  • slow-query-log
    Set this to 1 to enable the slow query log at all.
  • slow-query-log-file
    This is the path to the log file where slow queries are written.
    In this setup, it’s:
    /var/log/mysql-slow.log
  • long_query_time
    This is the threshold (in seconds) for what counts as “slow”.
    With 2, any query taking longer than 2 seconds is logged.
  • log_queries_not_using_indexes
    This tells MySQL to also log queries that don’t use indexes.
    It’s optional but useful to spot inefficient queries, even if they’re not super slow yet.

Enable the Slow Query Log

To enable it, remove the leading # at the beginning of each line, so it looks like this:

slow-query-log = 1
slow-query-log-file = /var/log/mysql-slow.log
long_query_time = 2
log_queries_not_using_indexes

If you’re on MySQL 8+, these lines may not exist in the file by default.
In that case, add them manually to the bottom of the file:

slow-query-log = 1
slow-query-log-file = /var/log/mysql-slow.log
long_query_time = 2
log_queries_not_using_indexes

Save and exit the editor when you’re done.

  • In nano, that’s: Ctrl + O (write), ENTER, then Ctrl + X (exit).

Step 4: Restart MySQL and Generate Some Load

After changing mysqld.cnf, you need to restart MySQL so it picks up the new config.

On many systems you can do:

sudo systemctl restart mysql

Or, if your distro uses a slightly different service name, adjust accordingly.

Once MySQL restarts, the slow query log is now active.
Any query that:

  • Takes longer than 2 seconds, or
  • Runs without using indexes (because we enabled log_queries_not_using_indexes)

…will be written to /var/log/mysql-slow.log.

Now, use your app or run some queries that you know are slow.
Give MySQL a bit of time to collect real query data.

Note: The source here focuses on enabling the log, not reading/analyzing it.
But just knowing where slow queries are recorded is already a huge step forward.


Step 5: Why These Settings Matter for Real-World Troubleshooting

Let’s connect the dots a bit so this isn’t just random config copy-paste.

  • Missing semicolon = query never actually runs.
  • Symptom: prompt hanging, no result, different-looking prompt (->).
  • Fix: add ; at the end, or cancel and retype properly.
  • Slow query log = MySQL tells you which queries are slow or indexless.
  • slow-query-log = 1 → feature on.
  • slow-query-log-file = /var/log/mysql-slow.log → where to look.
  • long_query_time = 2 → anything over 2 seconds gets logged.
  • log_queries_not_using_indexes → log potential future problems, not just current slow ones.

Once you have this configured, you can:

  • Reproduce your slow page or operation
  • Check the slow query log file for entries
  • Use those queries as the starting point for optimization later

Even if you’re not ready to tune indexes or rewrite SQL yet, having the log running now means you’re collecting data for when you are ready.


Quick Safety and Maintenance Notes

Because we’re touching config and logs, a few quick safety reminders:

  • Backup config files before changes.
    That one cp command can save you from a long night of debugging.
  • Watch log file size over time.
    The file /var/log/mysql-slow.log can grow, especially on busy servers.
  • Use reasonable long_query_time.
    A value like 2 seconds is a good starting point for many setups.
  • Permissions matter.
    Editing /etc/mysql/mysql.conf.d/mysqld.cnf usually requires sudo.
    Don’t run your whole terminal session as root if you don’t need to.

These aren’t MySQL-specific magic tricks, just good habits from years of breaking things and putting them back together.


Recap and Next Step

You’ve now got two solid tools for everyday MySQL headaches:

  1. Queries that seem stuck → check for a missing semicolon and watch the prompt (->).
  2. Queries that are slow → enable the slow query log in mysqld.cnf and let MySQL write problem queries to /var/log/mysql-slow.log.

From here, your next step is simple: use your app normally for a while, then check that slow query log and see what MySQL is complaining about.
That gives you a concrete list of queries to improve later.

If this worked for you, keep CrushEdge handy for the next fix.

No Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.