migratewp

#mysql … As always, view the wiki for the most current version.

[Update: The context for this tutorial should be explained. I had this tutorial on my site for over a year, and it used to be one line long. It said, execute this command sudo mysql -u root -h localhost -p < /path/to/backup-databases.sqland all would be solved. Sadly, in many cases, such as the day this was posted, this command fails for a variety of reasons. In those cases, I do what I have described below.]

This tutorial is designed to step one through how to migrate a self-hosted Word Press site manually. If you have not set up your WordPress, visit the Self-Hosted WordPress page first. Now, once that is set up, authenticate as root on the old host and backup your entire database (thanks to jason for this mysqldump syntax):

sudo -i
/usr/bin/mysqldump \
--add-drop-database \
--all-databases \
--allow-keywords \
--comments \
--complete-insert \
--lock-all-tables \
--skip-dump-date \
--events \
--flush-logs \
--flush-privileges \
--hex-blob \
--opt \
--routines \
mysqldump-all-databases.sql

Use scp to get the file to the new host:

sudo scp mysqldump-all-databases.sql user@newhostorip.com:

Before your import the mysqldump databases into the new database, you need to create what I call a “surrogate” user and database to do the heavy lifting for you first. You will also grant this surrogate user super privileges. Make sure the name you pick is unique and was not present in the database backup and/or a restricted user name.

CREATE DATABASE newdatabase DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
GRANT ALL ON newdatabase.* TO 'newdatabaseuser'@'localhost' IDENTIFIED BY 'temporarypassword';
FLUSH PRIVILEGES;
EXIT;
CREATE USER 'newdatabaseuser'@'%' IDENTIFIED BY 'temporarypassword';
GRANT ALL PRIVILEGES ON *.* TO 'newdatabaseuser'@'%' WITH GRANT OPTION;

Now, you need to create databases and grant privileges to the surrogate user for each of them. You do this as follows:

> CREATE DATABASE restoreddb1 DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
> GRANT ALL ON restoreddb1.* TO 'newdatabaseuser'@'localhost' IDENTIFIED BY 'temporarypassword';

If there are others, then repeat the commands above for each one, replacing restoreddb1 with the name of the databases you are importing and bringing in. Now, time to import that database into the new host:

sudo mysql -u newdatabaseuser -h localhost -p --database=newdatabase < /path/to/backup-databases.sql

Once it finishes, log into the mysql command mode and verify the original databases made it over:

sudo mysql -u user -p
> show databases;
> exit;

If possibly, run this command on both machines, and the output should be identical. Now, it is time to migrate the website //files// over to the new host:

sudo scp -r /var/www/website.com/public_html user@newhostorip.com:/var/www/newwebsite.com/

Put the files in the appropriate locations, restart the mysql service and reboot. Lastly, once you verify the original databases made it over and the website is restored and functioning, you can delete the surrogate user and database as follows:

DROP DATABASE newdatabase;
DROP USER 'newdatabaseuser'@'localhost';

Okay, that should get the job done!

Leave a Reply

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

Close
JavaScript licenses