如何将本地MySQL数据库迁移至远程MySQL实例(含AWS RDS)
Alright, let's break down the most reliable methods for migrating your local MySQL data to a remote instance—whether that's a self-hosted remote MySQL server or an AWS RDS MySQL instance. These are all approaches I've used in production, so they're battle-tested.
mysqldump (Command-Line, Best for Small-to-Medium Datasets) This is the classic go-to for straightforward migrations, especially if you're comfortable with the terminal.
Step 1: Backup your local database
First, create a dump of your local database. For InnoDB tables (most common these days), use --single-transaction to avoid locking tables during the backup:
mysqldump -u [LOCAL_USERNAME] -p --single-transaction [LOCAL_DB_NAME] > local_db_backup.sql
- Replace
[LOCAL_USERNAME]and[LOCAL_DB_NAME]with your actual credentials. - The
-pflag will prompt you for your local MySQL password. - For MyISAM tables (less common now), use
--lock-tablesinstead to ensure data consistency.
Step 2: Get the backup to the remote target
- Self-hosted remote MySQL: Use
scporsftpto transfer the backup file to the remote server:scp local_db_backup.sql [REMOTE_USER]@[REMOTE_HOST]:/path/to/remote/folder/ - AWS RDS: You can't directly transfer files to an RDS instance, so you have two options:
- Keep the backup on your local machine and import it directly over the network (skip to Step 3).
- Upload the backup to an S3 bucket, then use RDS's Import from S3 feature (great for very large files).
Step 3: Import the backup to the remote database
- Self-hosted remote MySQL: SSH into the remote server, then run:
mysql -u [REMOTE_USERNAME] -p -h [REMOTE_HOST] [REMOTE_DB_NAME] < /path/to/remote/folder/local_db_backup.sql - AWS RDS: Run the same command from your local machine, replacing
[REMOTE_HOST]with your RDS instance's endpoint (found in the AWS Console):
Pro tip: Make sure your local IP is added to the RDS security group's inbound rules to allow port 3306 access!mysql -u [RDS_USERNAME] -p -h [RDS_ENDPOINT] [RDS_DB_NAME] < local_db_backup.sql
If you prefer a GUI over the command line, MySQL Workbench's migration tools are perfect.
Step 1: Set up connections
- Open MySQL Workbench and create a connection to your local MySQL instance.
- Create another connection to your remote instance (for RDS, use the endpoint, username, and password from the AWS Console).
Step 2: Use the Migration Wizard
- Go to Database > Migration Wizard.
- Select your local connection as the source, and your remote/RDS connection as the target.
- Follow the prompts to select databases/tables to migrate, check for compatibility issues (critical for RDS, which has some restricted functions), and start the migration.
- The wizard will handle schema creation and data transfer automatically—no need to manually run dump/import commands.
If you're migrating to RDS and have a huge dataset, or need to keep the local and RDS instances in sync post-migration, AWS DMS is the way to go.
Step 1: Prepare your environments
- Ensure your local MySQL instance is accessible to DMS: Use a VPN, VPC peering, or add your local IP (or DMS replication instance's IP) to your local firewall rules.
- Make sure your RDS instance's security group allows inbound traffic from the DMS replication instance.
Step 2: Configure DMS
- Create endpoints: One for your local MySQL source, one for your RDS target.
- Create a replication instance: Choose an instance size based on your dataset size (DMS will recommend one).
- Create a migration task:
- Choose "Migrate existing data and replicate ongoing changes" if you need continuous sync, or "Migrate existing data only" for a one-time migration.
- Map your source tables to the target RDS tables.
- Start the task and monitor progress in the DMS Console. DMS handles data validation and can retry failed transfers automatically.
Don't skip these—they'll save you headaches later:
- Version compatibility: Ensure your remote/RDS MySQL version is at least as new as your local version (RDS supports most recent MySQL versions, but double-check).
- Permissions: Local user needs
SELECTandLOCK TABLESpermissions; remote/RDS user needsCREATE,INSERT,ALTER, andDROPpermissions. - Data consistency: After migration, run
CHECKSUM TABLE [TABLE_NAME];on both local and remote tables to verify all data was transferred correctly. - Performance: For large datasets, schedule migrations during off-peak hours to avoid impacting production traffic.
内容的提问来源于stack exchange,提问作者Nayeem Shaiky




