Speeding Up Your Database: How to Get the Most Out of MySQL Tuner
If you’ve ever watched a PHP page stall because the database can’t keep up, this guide is for you. We’ll walk through installing MySQL Tuner, interpreting its output, and making tweaks that actually shave milliseconds off your queries.
Step 1: Grab MySQL Tuner on the Server Where It Matters
wget http://mysqltuner.pl/mysqltuner.pl chmod +x mysqltuner.pl
Why this matters: Running the script locally means you’re looking at the exact configuration your application uses, not a snapshot from a dev box. A single “mysqld” instance on a shared host could be hiding a memory leak that only shows up under real load.
Step 2: Run It Against a Busy Database
./mysqltuner.pl --user root --pass your_password --host localhost
Skip the --debug flag unless you’re troubleshooting MySQL itself. The script pulls live stats from SHOW STATUS and SHOW VARIABLES, so you’ll see what actually hurts performance—not what a test environment suggests.
Step 3: Read the Recommendations (and Don’t Take Them Blindly)
Typical output might say:
Query cache size = 64 MB Query cache limit = 1 KB Query cache type = ON
If you’re running MySQL 5.7 or newer, note that the query cache is deprecated. That recommendation can be a red flag; sometimes the tuner will still advise increasing it because older codebases rely on cached SELECTs.
Step 4: Tweak Your my.cnf and Test Again
Add or adjust:
[mysqld] innodb_buffer_pool_size=2G query_cache_type=0 # Disable if you’re on 5.7+
Restart MySQL, then re‑run the tuner. Watch the “Buffer pool size” line climb toward your server’s memory minus what PHP or other services need.
Why restart matters: MySQL reads its config only at startup. A hot reload won’t pick up new values, so a fresh boot is essential to see real changes.
Step 5: Pinpoint Hot Queries with the Slow‑Query Log
sed -i 's/^#slow_query_log =/slow_query_log =/' /etc/mysql/my.cnf sed -i 's/#long_query_time =/long_query_time = 0.2/' /etc/mysql/my.cnf systemctl restart mysql
Now the tuner’s “Slow queries” section will be populated. If you see something like:
SELECT * FROM wp_posts WHERE ID = ?
you’re probably missing an index on ID. Adding it can cut response time from seconds to milliseconds.
Step 6: Use Indexes Wisely (and Watch the Cardinality)
CREATE INDEX idx_user_id ON orders(user_id);
After adding, run:
ANALYZE TABLE orders;
This updates MySQL’s statistics so future query plans are accurate. I’ve seen servers that add an index and still suffer because they never ran ANALYZE; the optimizer kept guessing.
Step 7: Keep the Tuner in Your Routine
Run ./mysqltuner.pl every time you deploy a major update or when traffic spikes. It’s a cheap sanity check that reminds you whether your memory settings are still appropriate as your schema grows.
That’s the meat of it. MySQL Tuner is basically a quick‑look cheat sheet, but when paired with real data and a willingness to adjust my.cnf, it turns a sluggish database into something that feels snappy. Give it a spin next time you notice a page hanging, and see how much faster your queries can run.