Skip to content

Percona Toolkit — the MySQL operator essentials

The Percona Toolkit commands an MSP actually uses in production — query digest, online schema change, replication checksums, archive, kill, and how to wield them safely.

Percona Toolkit is a set of mature command-line utilities that fill the gaps the MySQL distribution leaves open — finding the queries actually hurting the database, changing a multi-gigabyte table without taking it offline, verifying replicas have not silently drifted from the primary. This article is the operator-grade subset we install and rely on, the commands an on-call engineer reaches for, and the safety rails that keep them from making the outage worse.

How to verify

Confirm the toolkit is present, the user has the grants it needs, and the version is recent enough to know about your MySQL release.

pt-query-digest --version
pt-online-schema-change --version
pt-table-checksum --version
mysql -e "SHOW GRANTS FOR 'percona'@'localhost';"
pt-summary | head -40

If the toolkit is older than your server (e.g. Percona Toolkit 3.0 against MySQL 8.4), the pt-online-schema-change triggers will misbehave. Upgrade the toolkit before running it against production.

What’s happening

Percona Toolkit is a Perl-based collection — each tool is one script with its own flags, logging, and exit codes. They share a few conventions: they use the DSN syntax for connection details (h=host,P=3306,u=percona), they almost all accept --dry-run to print what they would do without doing it, and they emit useful exit codes that fit into shell pipelines.

The handful we run constantly:

  • pt-query-digest — feed it a slow query log or tcpdump capture and it groups SQL by fingerprint, ranks by total time, and shows the worst offenders with full sample queries. The first stop when a database is “slow.”
  • pt-online-schema-change (pt-osc) — applies ALTER TABLE without an exclusive lock, by creating a shadow table, copying rows in chunks, and swapping the names at the end. Triggers keep the shadow in sync during the copy.
  • pt-table-checksum — runs row-level checksums on the primary and replicas, surfacing any silent drift in async replication. The companion pt-table-sync repairs it.
  • pt-archiver — moves rows out of a hot table into an archive table or a file, in small batches, without long-running transactions.
  • pt-kill — kills connections matching a pattern (long-running, idle in transaction, from a specific user). The break-glass for runaway queries.
  • pt-summary / pt-mysql-summary — produce a one-shot snapshot of the host and the MySQL configuration; the first thing we capture before changing anything.

The procedure

  1. Install on Debian-derived hosts from the Percona repository.

    wget https://repo.percona.com/apt/percona-release_latest.generic_all.deb
    sudo dpkg -i percona-release_latest.generic_all.deb
    sudo apt update
    sudo apt install -y percona-toolkit
  2. Create a dedicated user with the grants the toolkit needs. The full set is broad because some tools touch every database; restrict to what you actually run.

    CREATE USER 'percona'@'localhost' IDENTIFIED BY 'TOOLKIT_PASS';
    GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, INDEX,
          LOCK TABLES, TRIGGER, EVENT, CREATE TEMPORARY TABLES,
          PROCESS, REPLICATION CLIENT, REPLICATION SLAVE,
          RELOAD, SUPER ON *.* TO 'percona'@'localhost';
  3. Find the worst queries on a “slow” host. Enable the slow log first, then digest.

    mysql -e "SET GLOBAL slow_query_log = 'ON'; SET GLOBAL long_query_time = 0.5;"
    sleep 600
    mysql -e "SET GLOBAL slow_query_log = 'OFF';"
    pt-query-digest /var/log/mysql/mysql-slow.log | head -80
  4. Apply a schema change without locking. --dry-run first, every time.

    pt-online-schema-change \
      --alter "ADD COLUMN email VARCHAR(255) NULL" \
      --dry-run \
      D=app,t=users,h=127.0.0.1,u=percona,p=TOOLKIT_PASS
    pt-online-schema-change \
      --alter "ADD COLUMN email VARCHAR(255) NULL" \
      --execute --chunk-time=0.5 --max-load 'Threads_running=25' \
      D=app,t=users,h=127.0.0.1,u=percona,p=TOOLKIT_PASS

    --max-load is the throttle — when the named status counter exceeds the threshold, the chunk copy pauses. --chunk-time=0.5 aims for half-second chunks; if a chunk runs longer, the next one is smaller.

  5. Verify replicas are byte-for-byte consistent with the primary.

    pt-table-checksum \
      --user=percona --password=TOOLKIT_PASS \
      --replicate=percona.checksums \
      --create-replicate-table \
      --no-check-binlog-format \
      h=127.0.0.1

    The exit code is non-zero if any chunk shows a difference; check percona.checksums on the replica for the table and chunk in question, then pt-table-sync --print --replicate=percona.checksums h=replica-host to see the rows that diverge.

  6. Kill long-running queries that match a pattern.

    pt-kill --busy-time 60 --match-command Query --victims all --print
    pt-kill --busy-time 60 --match-command Query --victims all --kill

    Run --print first to see what would die; run --kill only when you have confirmed it is what you mean to do.

Common pitfalls

  • pt-osc without --no-drop-old-table — the default drops the original table after rename. If a foreign key references it and the toolkit cannot figure out how to update the constraint, you can lose data. Read the --alter-foreign-keys-method docs before running on a referenced table.
  • pt-osc triggers and existing triggers — pt-osc will refuse if the table already has triggers (older versions). For tables with existing triggers, the operation requires a different tool (gh-ost) or a maintenance window.
  • pt-table-checksum on a Galera cluster — Galera does not run binlog format STATEMENT for the checksum query the way the tool expects; pass --no-check-binlog-format and use the cluster mode of the tool.
  • pt-kill --kill without --print — easy way to take the application offline by killing the wrong connection set. Always --print first.
  • Running on a primary with replicas without --check-replication-filters — replication filters can hide tables from replicas; pt-table-checksum will report drift that is not really drift.

Percona Toolkit is what turns a “slow database” ticket into a list of specific queries with explain plans, an ALTER TABLE from a six-hour outage into a managed background copy, and a silently-divergent replica into a checksumed claim. In the engagements we run, the toolkit is part of the standard MySQL operator’s kit — installed by config management, the dedicated user is provisioned by the same module, and pt-query-digest reports feed our weekly review of the database fleet. For the broader pattern of running MySQL and MariaDB this carefully, see /en/services/managed-operations/.