You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何将本地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.

Method 1: 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 -p flag will prompt you for your local MySQL password.
  • For MyISAM tables (less common now), use --lock-tables instead to ensure data consistency.

Step 2: Get the backup to the remote target

  • Self-hosted remote MySQL: Use scp or sftp to 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:
    1. Keep the backup on your local machine and import it directly over the network (skip to Step 3).
    2. 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):
    mysql -u [RDS_USERNAME] -p -h [RDS_ENDPOINT] [RDS_DB_NAME] < local_db_backup.sql
    
    Pro tip: Make sure your local IP is added to the RDS security group's inbound rules to allow port 3306 access!
Method 2: MySQL Workbench (Visual, User-Friendly)

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

  1. Go to Database > Migration Wizard.
  2. Select your local connection as the source, and your remote/RDS connection as the target.
  3. 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.
  4. The wizard will handle schema creation and data transfer automatically—no need to manually run dump/import commands.
Method 3: AWS Database Migration Service (DMS) (Best for Large Datasets or Ongoing Replication)

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

  1. Create endpoints: One for your local MySQL source, one for your RDS target.
  2. Create a replication instance: Choose an instance size based on your dataset size (DMS will recommend one).
  3. 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.
  4. Start the task and monitor progress in the DMS Console. DMS handles data validation and can retry failed transfers automatically.
Critical Pre-Migration Checks

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 SELECT and LOCK TABLES permissions; remote/RDS user needs CREATE, INSERT, ALTER, and DROP permissions.
  • 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

火山引擎 最新活动