We often find ourselves setting up WordPress infrastructures and so I thought it would make a good article to discuss some of the processes we often go through in order to either setup a server or migrate an existing WordPress website.
WordPress Database Migration (MySQL)
There are many ways to get your data from one WordPress instance to another, however when we are migrating a WordPress install, we prefer the following.
We always access Linux servers via SSH. This gives us access to the MySQL terminal client. In order to get a database dump of the current website, we usually use a command such as the following, which create a database dump which we can use later on:
mysqldump -u liverpool -p liverpool wordpressdb > wordpressdb.sql
The above command will create a file containing the database dump of the “wordpressdb” database within MysQL. The basics of the command are:
- mysqldump – command used to output a text SQL database dump.
- “u” flag – to specify your database username. Some MySQL users may not have access to all databases so be sure you use the correct user. Looking at the wp-config.php file within WordPress should reveal the MySQL user which you can use for this.
- “p” flag – to specify the password. This is the password for the MySQL user. It is good practice to not actually specify the password itself in the terminal (e.g. just type -p without the password). This is because if you specify the password within the command, the other users may also be able to see the password within the BASH history (by typing “history”). Leave the “p” blank will force the MySQL terminal client to prompt for the password and so it will not be saved in the bash history.
- “wordpressdb” – This is the database name within MySQL. This can also be found within the wp-config.php file in the root of your WordPress install.
- “> wordpressdb.sql” – The chevron tells BASH to send the output of the mysqldump command to a file named wordpressdb.sql. This file can be called anything you like.
One you have produced a MySQL dump for your WordPress website, depending on it’s size you may wish to compress it as we will be sending it over the internet to either your client machine or direct to the new server you are setting up. To do this you could use a command such as the following:
This will compress the file producing a file named wordpressdb.sql.gz .
Now we need to copy our file to our new server. In order to do this we can use the “scp” command within BASH as long as you have SSH access to your new server also.
scp wordpressdb.sql.gz email@example.com:~/
This command will send you file “wordpressdb.sql.gz” to your other server/environment specified by the user “carl” and the ip address “22.214.171.124”, in to the home directory of the “carl” user.
Once here we can then ssh into our second environment and import the database dump. In order to do this we must first create an empty database to import our dump into:
mysql -u merseyside -p merseyside -e “create database wordpressdb”
Similar to the previous mysql command except this time we use the “-e” flag and specify some SQL to run. In this case the SQL is simply to create a black database.
Now we can import our database dump into the database of our second server:
zcat wordpressdb.sql.gz | mysql -u merseyside -p merseyside wordpressdb
The above command will output the wordpressdb.sql.gz file contents and we then “pipe” this output as input to the MySQL command. Again this is similar to the previous MySQL commands.
At this point we have our database setup and if we have our wp-config.php file configured and in place, WordPress will use this database, however if you are changing domain name (or subdomain) you may also need to run some SQL to update all of the permalinks. In order to do this you will need to login to MySQL via your terminal client and run some SQL.
UPDATE wp_options SET option_value = replace(option_value, 'http://localhost:8888/', 'http://somesite.local/') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET guid = replace(guid, 'http://localhost:8888/', 'http://somesite.local/');
UPDATE wp_posts SET post_content = replace(post_content, 'http://localhost:8888/', 'http://somesite.local/');
UPDATE wp_postmeta SET meta_value = replace(meta_value, 'http://localhost:8888/', 'http://somesite.local/');
The above SQL will search through all the WordPress posts etc and replace the old website links with your new links. So for example the above will replace “http://localhost:8888/” with “http://somesite.local/” (or in other words old URL to new URL). After doing this WordPress should now work as expected. You can reference this SQL here.
WordPress Assets Migration
Again there are may ways to migrate your assets from one server to another, however the following is often how we do it.
scp -r wp-content/uploads firstname.lastname@example.org:~/wordpress/wp-content/
Similar to how we copied the MySQL database dump from one server to another, we can also copy the assets in a similar fashion. The “-r” flasg simly means to recursively copy files (all files and sub directories etc).
In conclusion, if you are familiar with SSH, BASH and MySQL this process should be very simple to replicate. If however you are a non-techie, this article should help explain the processes involved. If you feel you may need help with such a task, then please get in touch. We are happy to help.