Transfer MySQL Databases to New Server
In this article
- Why You Need a Database Transfer
- Before You Start: Preparation Checklist
- Method 1: Using mysqldump (Recommended for Most Users)
- Method 2: Copying the Data Directory (For Large Databases)
- Common Problems and How to Avoid Them
- Comparing Transfer Methods
- Safety and Verification Steps
- Frequently Asked Questions
To transfer MySQL databases from an old server to a new one, use mysqldump to create a logical backup, copy the resulting SQL file, and import it on the new server with the mysql client. This method works for most setups, including different MySQL versions and operating systems, and preserves table structures, data, and indexes.
Why You Need a Database Transfer
Server migrations happen for many reasons: moving to a larger VPS, switching hosting providers, consolidating infrastructure, or upgrading hardware. A database is often the most critical part of an application—losing it means losing user accounts, orders, posts, or configuration. A clean transfer minimizes downtime and avoids data corruption, which is why the process deserves careful planning rather than a quick copy-paste of files.
Before You Start: Preparation Checklist
Successful transfers start before you run any command. Take these steps first:
- Identify all databases you need to move. Run
SHOW DATABASES;on the old server to list them, and note which ones are actually in use (skip system schemas likeinformation_schemaandperformance_schema). - Check MySQL versions on both servers. Major version differences (e.g., 5.7 to 8.0) can affect compatibility; check the official upgrade notes for any known issues.
- Note database sizes with
SELECT table_schema, ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS size_mb FROM information_schema.tables GROUP BY table_schema;. This tells you how long the transfer might take and whether you need compression. - Plan for downtime if your application writes data constantly. A logical dump captures a point-in-time snapshot; any writes after the dump starts won't be included unless you stop writes or use replication.
- Create a backup of the dump file itself on a third location (like your local machine) before importing, in case the new server has issues.
Method 1: Using mysqldump (Recommended for Most Users)
mysqldump is the standard MySQL utility for logical backups. It generates SQL statements that recreate the database structure and insert the data. It works across different MySQL versions and operating systems, making it the most portable option.
On the old server, run:
mysqldump -u username -p --databases db1 db2 > backup.sqlIf you want all databases, use --all-databases. For a single database, just specify its name. Add --single-transaction for InnoDB tables to get a consistent snapshot without locking tables (this is important for busy servers). For large databases, compress the output to save bandwidth:
mysqldump -u username -p --single-transaction --databases db1 | gzip > backup.sql.gzTransfer the file to the new server using scp, rsync, or a file manager. Then import it:
mysql -u username -p < backup.sqlIf you compressed it, decompress first:
gunzip < backup.sql.gz | mysql -u username -pAfter importing, verify the data by running a quick query like SELECT COUNT(*) FROM your_table; on both servers and comparing results.
Method 2: Copying the Data Directory (For Large Databases)
For very large databases (tens of gigabytes or more), mysqldump can be slow because it regenerates every INSERT statement. A faster alternative is to copy the physical data files, but this requires the MySQL versions to be compatible and the servers to be shut down (or use a consistent snapshot).
Steps:
- Stop the MySQL service on both servers.
- Copy the entire data directory (usually
/var/lib/mysqlon Linux) from the old server to the new one usingrsyncortar. - Ensure file permissions are correct (usually the
mysqluser owns the files). - Start MySQL on the new server and run
mysql_upgradeif the version differs.
This method is faster but riskier: any mismatch in MySQL versions, storage engine versions, or file formats can cause corruption. Only use it when you're confident the servers are nearly identical.
Common Problems and How to Avoid Them
Several issues frequently trip up people during transfers:
- Access denied errors – Ensure the user you're using has
SELECTandLOCK TABLESprivileges on the old server, andCREATE,INSERT, andALTERprivileges on the new one. - Character set mismatches – If your data contains non-ASCII characters (like emojis or accented letters), add
--default-character-set=utf8mb4to both dump and import commands to avoid garbled text. - Foreign key constraint failures – When importing, the order of tables matters.
mysqldumphandles this by default, but if you manually split the dump, you may need to disable foreign key checks withSET FOREIGN_KEY_CHECKS=0;before importing. - Out-of-memory errors – For large dumps, increase
max_allowed_packetin the MySQL config (e.g., to 256M) on both servers. - Timeouts – If the import takes too long, your client may time out. Use
mysql --connect-timeout=600or run the import in ascreenortmuxsession.
Comparing Transfer Methods
Here's a quick comparison to help you decide:
- mysqldump: Best for most cases. Portable, works across versions, easy to debug. Slower for huge databases.
- Physical file copy: Fastest for large data, but fragile. Requires identical or compatible versions and downtime.
- MySQL replication: Best for zero-downtime migrations. Set up the new server as a replica of the old one, let it catch up, then switch. More complex to configure.
- Third-party tools (like Percona XtraBackup or MySQL Workbench) offer GUI or incremental backup options, but add dependencies.
Safety and Verification Steps
After the import, don't assume everything worked. Run these checks:
- Compare row counts for critical tables between old and new servers.
- Run
CHECK TABLE your_table;on a few tables to detect corruption. - Test your application against the new server before pointing traffic to it.
- Keep the old server running for a few days as a fallback, but stop writes to it to avoid confusion.
- Update your application's database connection settings (host, port, credentials) to point to the new server.
If you're moving to a different MySQL version, run mysql_upgrade after the import to update system tables and ensure compatibility.
Frequently Asked Questions
Can I transfer a MySQL database without stopping the old server? Yes, using mysqldump --single-transaction for InnoDB tables gives a consistent snapshot without locking. For MyISAM tables, you'll need to lock them or accept a possible inconsistent snapshot if writes happen during the dump.
What if my new server has a different MySQL version? mysqldump is generally forward-compatible: you can dump from an older version and import into a newer one. Going backward (newer to older) is riskier and may fail if the dump uses syntax the older version doesn't support. Check the official upgrade matrix.
How long does a transfer take? It depends on database size, network speed, and server resources. A 1 GB database on a decent connection might take 5-10 minutes. For multi-hundred-GB databases, plan for hours and consider using compression or the physical copy method.
Do I need to transfer user accounts and privileges? mysqldump --all-databases includes the mysql system database, which contains user accounts. If you only dump specific databases, you'll need to recreate users manually on the new server using CREATE USER and GRANT statements.
What's the safest way to verify the transfer? Run a checksum on a few tables using CHECKSUM TABLE on both servers and compare the results. Also, run your application's health checks or a few manual queries that exercise reads and writes.