How to Transfer All MySQL Databases From Old to New Server

Transferring or Migrating a MySQL/MariaDB database between servers usually takes only few easy steps, but data transfer can take some time depending on the volume of data you would like to transfer.

In this article, you will learn how to transfer or migrate all your MySQL/MariaDB databases from old Linux server to a new server, import it successfully and confirm that the data is there.

Important Notes

  • Make sure to have the same version of MySQL installed on both server with same distribution.
  • Make sure to have enough free space on both server to hold the database dump file and the imported database.
  • Don’t ever consider moving the data directory of database to another server. Never mess with internal structure of the database, if you do, you will face problems in future.

Export a MySQL Databases to Dump File

First start by login into your old server and stop the mysql/mariadb service using the systemctl command as shown.

# systemctl stop mariadb
# systemctl stop mysql

Then dump all your MySQL databases to a single file using the mysqldump command.

# mysqldump -u [user] -p --all-databases > all_databases.sql

Once the dump is completed, you are ready to transfer the databases.

If you want to dump a single database, you can use:

# mysqldump -u root -p --opt [database name] > database_name.sql

Transfer MySQL Databases Dump File to New Server

Now use scp command to transfer your databases dump file to the new server under home directory as shown.

# scp all_databases.sql [email protected]:~/       [All Databases]
# scp database_name.sql [email protected]:~/       [Singe Database]

Once you connect, the database will be transferred to the new server.

Import MySQL Databases Dump File to New Server

Once the MySQL dump file has been traferred to the new server, you can use the following command to import all your databases into MySQL.

# mysql -u [user] -p --all-databases < all_databases.sql   [All Databases]
# mysql -u [user] -p newdatabase < database_name.sql      [Singe Database]

Once the import is completed, you can verify the databases on both servers using the following command on the mysql shell.

# mysql -u user -p
# show databases;

Transfer MySQL Databases and Users to New Server

If you want to move all your MySQL databases, users, permissions and data structure old server to new, you can use rsync command to copy the all the content from the mysql/mariadb data directory to new server as shown.

# rsync -avz /var/lib/mysql/* [email protected]:/var/lib/mysql/ 

Once the transfer completes, you can set the ownership of the mysql/mariadb data directory to user and group mysql, then do a directory listing to check that all files have been transferred.

# chown mysql:mysql -R /var/lib/mysql/
# ls  -l /var/lib/mysql/

That’s all! In this article, you learned how to easily migrate all MySQL/MariaDB databases from one server to another. How do you find this method compared to other methods? We would like to hear from you via the comment form below to reach us.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Aaron Kili

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide
The Complete Linux System Administrator Bundle
Become an Ethical Hacker Bonus Bundle

You may also like...

10 Responses

  1. Colin says:

    This only works to go from one version to the same one. I tried it to move the tables from my old install to the new one and all it did was mangle it to the point it wouldn’t work because of structure changes, table changes, storage methods, and so on. Doing user tables is fine, but taking all the admin tables over too just isn’t going to work!

  2. Márton Gajárszky says:

    Agreed with Jordan Sorenson on rhel-7.4.

  3. Basile Starynkevitch says:

    Consider suggesting, in some but not all cases, for large databases, to compress the *.sql dump file.

  4. Sorin Srbu says:

    On CentOS 6 running MySQL 5.5, the mysqldump command doesn’t run, unless the mysqld daemon is running.
    Further, the “-all-databases” doesn’t exist.

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.