How to Modify MySQL Tables Online Without Downtime Using pt-online-schema-change
Learn a step-by-step method to safely alter large MySQL tables in production without locking, by creating a new table, syncing data via triggers, copying rows, swapping tables, and using Percona’s pt-online-schema-change tool with example commands and installation instructions.
Solution Idea
1. Create a new table with the desired schema.
2. Add triggers on the old table to replicate updates to the new table.
3. Copy existing data from the old table to the new one.
4. After synchronization, rename the tables to swap them.
5. Drop the old table and its triggers.
Implementation
Percona provides the pt-online-schema-change tool, which performs online schema modifications without blocking reads and writes.
Usage example:
pt-online-schema-change --user=root --password=111111 --host=192.168.31.157 --alter "modify name CHAR(50)" D=sakila,t=test --executeThe tool outputs progress information during execution.
Parameter explanation: --user, --password, --host: database connection details. --alter: the ALTER statement to apply, e.g.,
alter table test modify name varchar(60); D=database, t=table: specify target database and table. --execute: actually perform the change; use --dry-run to preview.
Installation
Download the Percona Toolkit from https://www.percona.com/downloads/percona-toolkit/ and install on CentOS 7:
yum install epel-release-7-5.noarch
yum install perl-DBD-MySQL
yum install perl-Digest-MD5
yum install perl-Config-Tiny
yum install perl-Log-Dispatch
yum install perl-Parallel-ForkManager
yum install -y rrdtool perl-rrdtool rrdtool-devel perl-Params-Validate
rpm -ivh percona-toolkit.rpmAfter installation, the tool can be executed directly.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
