ANS Documentation

Improve This Doc
  • Cloud
  • Domains and DNS management
  • Backup and High Availability
  • eCommerce Stacks
  • Security
  • Email
  • Monitoring and usage management
  • Networking
  • Operating systems
    • Linux
      • Basics
      • Apache
      • Control Panels
      • FTP
      • Magento
      • Miscellaneous
      • MySQL
      • NGINX
      • PHP
      • PHP-FPM
      • Setting up Websites
      • NFS
      • VPN
      • Percona
      • SSH
      • SSL
      • Git
      • Redis
      • Elasticsearch
      • MongoDB
      • Ncdu
    • VMware ESXi
    • Windows
  • Webcelerator
  • MyUKFast
  • Home >
  • Operating systems >
  • Linux >
  • MySQL >
  • Migrate Your Databases

Migrate Your Databases¶

In this article, we discuss how to successfully migrate a MySQL / MariaDB database between servers. This can be achieved in a few easy steps and can be a relatively quick process depending on the size of the database(s) are being transferred.

Prerequisites¶

  • Ensure the same version of MySQL is installed on both servers.

  • Ensure there is enough free space for the size of the database on both servers.

    • To find out the size of each database use the following query:

SELECT table_schema AS "Database",
ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS "Size (MB)"
FROM information_schema.TABLES
GROUP BY table_schema;

Create a database dump¶

On the current databases server, run the relative commands via SSH. Depending on how large the database(s) is, it may take a short while to complete.

For a single database :

mysqldump --events --routines --triggers --single-transaction dbname > dbname

For all / multiple databases, the below commands will split the individual databases into separate files in the /root/mysqldumps directory:

mkdir /root/mysqldumps
for i in $(mysql -NBe 'show databases' | grep -v 'performance_schema'), do
  mysqldump --force --events --routines --single-transaction --triggers $i > /root/mysqldumps/$i
done

Transfer the dump¶

When the database dump is complete, its time to transfer the file(s) to the alternate server. This example uses SCP to securely copy the files and directories between remote hosts without using an FTP client. For servers behind the same dedicated firewall, use the internal IP address of the remote server.

For a single database :

scp -v -P <ssh-port> dbname [email protected]:~/dbname

For multiple databases :

scp -vr  -P <ssh-port> /root/mysqldumps [email protected]:mysqldumps

Check that the files transferred as expected on the remote host:

ll /root/mysqldumps/
total 2068
-rw-r--r-- 1 root root    2107 Jul 10 17:25 example1
-rw-r--r-- 1 root root  533999 Jul 10 17:25 example2
-rw-r--r-- 1 root root    2372 Jul 10 17:25 example3
-rw-r--r-- 1 root root 1033415 Jul 10 17:25 information_schema
-rw-r--r-- 1 root root  515484 Jul 10 17:25 mysql
-rw-r--r-- 1 root root    2247 Jul 10 17:25 example4

Before the Import¶

Before the import, first ensure the files do not contain USE or CREATE DATABASE statements as this can cause unexpected outcomes.

Use the following grep command to check, if blank, move on to import your database:

egrep "^USE|^CREATE DATABASE" example1
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `example1` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `example1`;

Theses lines can be deleted manually, or by using the following sed commands:

sed -i '/^USE/d' example1
sed -i '/^CREATE DATABASE/d' example1

Import your database¶

Now the files are transferred, it’s time to to perform the import. If there is an existing database of that name there will be conflicts and you may wish to rename the conflicting database.

For a single database :

MariaDB [(none)]> CREATE DATABASE example1;

To import the database, run the following command:

mysql -u root -p example1 < example1

For multiple databases :

for i in $(ls /root/mysqldumps); do
  mysql -e "CREATE DATABASE $i" && mysql -u root -p $i < /root/mysqldumps/$i;
done

Next Article > Managing Databases and Users

  • Useful Links
  • SMB
  • Enterprise
  • Channel
  • Public Sector
  • ANS Data Centres
  • About ANS
  • Careers
  • Blog
  • Get in touch
  •  
  • Sales 0800 458 4545
  • Support 0800 230 0032
  • Get in touch

© ANS Group Limited | Terms and Conditions | Corporate Guidance | Sitemap
ANS Group Limited, registered in England and Wales, company registration number 03176761, registered office 1 Archway, Birley Fields, Manchester M15 5QJ