Copy/duplicate database without using mysqldump
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Copying or duplicating a MySQL database without using mysqldump can be essential in scenarios where mysqldump is not preferred due to performance or resource constraints. Several alternative methods exist for achieving this requirement, each with its own advantages and use cases. This article will explore these methods with technical explanations and examples.
Methods for Copying/Duplicating a MySQL Database
1. Binary Log Files
Binary log files in MySQL record all changes to the database. These can be used to replay changes to duplicate the database on another server.
Steps:
- Enable Binary Logging: Make sure that binary logging is enabled on your MySQL server by checking the
my.cnffile:
- Create a Snapshot: Use a logical method such as
SELECT INTO OUTFILEto create a consistent snapshot of your database. - Apply Binary Logs: Apply these logs to the snapshot on the new database server to roll forward changes after the snapshot was taken.
Note: This method is best suited for databases where high availability and minimal downtime are critical.
2. Filesystem-Level Copy
If your MySQL server is stopped, you can copy the database files directly from the filesystem.
Steps:
- Stop MySQL Server: Ensure that the MySQL server is stopped to avoid issues with open files.
- Copy Files: Use
cporrsyncto transfer files in the MySQL data directory. Generally located under/var/lib/mysql/.
- Restart Server: Start the MySQL server.
Considerations: This method can result in data inconsistency if files are copied while MySQL is running.
3. Replication
MySQL replication can be set up to create a read-only copy of your database that can be manually promoted to a writable database if needed.
Steps:
- Configure Master Server:
- Enable binary logging.
- Set a unique server ID.
- Create a User: Create a replication user and grant replication permissions.
- Set up Slave Server:
- Copy the database snapshot to the slave.
- Configure the slave with the master's hostname, log file, and position.
- Start Slave Process:
Advantage: Offers real-time duplication of data with minimal configuration changes.
4. MySQL Workbench Export/Import
MySQL Workbench offers an interface to manually export and import databases.
Steps:
- Open MySQL Workbench.
- Export: Use the export wizard to export the database to a self-contained file.
- Import: Use the import wizard to import the file into another instance or server.
Pros and Cons: While this method is user-friendly, it may not be suitable for large databases due to time and resource consumption.
Key Points Summary
| Method | Description | Key Considerations |
| Binary Log Files | Replays log changes on another server | Requires logs and may require initial snapshot |
| Filesystem-Level Copy | Directly copies database files from filesystem | Server must be stopped during operation |
| Replication | Sets up a real-time replica of the database | Requires network setup and master-slave configuration |
| Workbench Export/Import | Utilizes MySQL Workbench for manual export/import | May not suit large databases due to resource usage |
Additional Considerations
- Data Consistency: Ensure file-level operations are atomic to avoid corruption.
- Storage Compatibility: Make sure both the source and destination databases have compatible storage engines.
- Permissions: Correct permissions must be set on destination for successful read/write operations.
- Backup Integrity: Always verify backups and test restore procedures as part of routine database maintenance.
These methods demonstrate viable alternatives to mysqldump for duplicating a MySQL database. Each method has its specific scenarios of applicability, and the choice between them should consider the technical requirements of your environment.
Related reading
- Correct usage of ENV in distributed Node system
- Correlation Id in Apache kafka
- Couchbase mobile replication through REST-API
- CouchBase Replication Load Balancing - How to reduce the frequency of client replication attempts on failure
- Copying one table to another in DynamoDB
- Copying third party databases into central mySQL and keeping mySQL up to date
- CouchDB Filtered Replication
- CouchDB filtered replication - remove a document

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.