background

MySQL Change Database Encoding

License: GPL v3Ruby

A robust Ruby tool for changing MySQL database encoding and collation with minimal disruption to production systems. Supports online schema changes to keep your database operational during migrations.

Features

Installation

Prerequisites

Setup

git clone https://github.com/durableprogramming/mysql_change_database_encoding.git
cd mysql_change_database_encoding
bundle install

Usage

Basic Migration

Change database encoding and collation:

MYSQL_PASSWORD=your_password MYSQL_DATABASE=your_database \
ruby mysql_change_database_encoding.rb \
--collation utf8mb4_unicode_ci --encoding utf8mb4

Online Schema Change

Use pt-online-schema-change for zero-downtime migration:

MYSQL_PASSWORD=secure_password MYSQL_DATABASE=legacy_database \
ruby mysql_change_database_encoding.rb \
--collation utf8mb4_unicode_ci --encoding utf8mb4 --osc

Environment Variables

Command Options

Common Use Cases

Migrate to UTF8MB4

The most common migration path - moving from utf8 to utf8mb4 for full Unicode support:

MYSQL_PASSWORD=pwd MYSQL_DATABASE=myapp \
ruby mysql_change_database_encoding.rb \
--encoding utf8mb4 --collation utf8mb4_unicode_ci --osc

Update Collation Only

Change collation without modifying encoding:

MYSQL_PASSWORD=pwd MYSQL_DATABASE=myapp \
ruby mysql_change_database_encoding.rb \
--collation utf8mb4_unicode_ci

Legacy Database Migration

Migrate old latin1 databases to modern encoding:

MYSQL_PASSWORD=pwd MYSQL_DATABASE=legacy_app \
ruby mysql_change_database_encoding.rb \
--encoding utf8mb4 --collation utf8mb4_unicode_ci --osc

Why Use This Tool?

Problems It Solves

Benefits

Online Schema Change Integration

When using --osc, the tool leverages pt-online-schema-change to:

  1. Create a new table with desired encoding/collation
  2. Copy data in chunks while tracking changes
  3. Swap tables atomically
  4. Minimize locking and downtime

This is crucial for production databases that cannot afford maintenance windows.

Best Practices

Before Migration

  1. Backup your database: Always create a full backup before encoding changes
  2. Test in staging: Run the migration on a staging database first
  3. Check disk space: Online schema changes require additional space
  4. Monitor replication: Be aware of replication lag during migration

During Migration

  1. Use —dry-run first: Preview changes before execution
  2. Monitor performance: Watch for increased load during migration
  3. Start with small tables: Test on non-critical tables first

After Migration

  1. Verify data integrity: Check that all data migrated correctly
  2. Update application: Ensure application uses new encoding in connections
  3. Update backups: Create new backup with updated encoding

Requirements

Contributing

Contributions are welcome! Please read our contributing guidelines before submitting pull requests.

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/my-feature)
  3. Make your changes
  4. Test thoroughly on a test database
  5. Commit your changes
  6. Push to your fork
  7. Open a pull request

Commercial Support

Commercial support is available from Durable Programming, LLC at durableprogramming.com. We offer:

Contact us at [email protected].

License

GNU General Public License v3.0 - see LICENSE for details.

Copyright (c) 2018-2025 Durable Programming, LLC. All rights reserved.

Support

For bugs, feature requests, or questions, please open an issue on GitHub.

Acknowledgments

GitHub

10 stars | 📅 Created March 2018 | 🔄 Updated 2025

View on GitHub