Step 3 ⊶ Reconnecting The Files And Database


Here we’ll do the following:
  • Create a new database container
  • Import and reconnect the database dump
  • Move the old DocumentRoot (core WordPress files, uploads, themes and plugins) into the new DocumentRoot
  • Edit the wp-config.php

Doing The Work

  1. Create a new database container and admin user for it in MariaDB or MySQL and import database dump into the new container.
  2. Note: MariaDB is a fork of MySQL led by the original developers of MySQL. MariaDB is a drop in replacement for MySQL and uses the same command syntax most of the time, there are some differences as well as configuration files, but overall is a more stable choice as MySQL is now owned by Oracle and its future is uncertain with the FOSS community. MariaDB is intended to remain free under the GNU GPL.

    Before we begin

    You’ll need access to PHPMyAdmin or the command line of your Linux server and a database administrator account.

    PHPMyAdmin screenshots provided by sterndata on Freenode

    Don’t forget to read about serialized PHP arrays

    To import the database dump from the command line on the TARGET (new) server, follow these steps:

    Login to MariaDB or MySQL
    mysql -p
    Create a new database container
    create database database_name;
    Create a new user for the database container
    grant all privileges on database_name.* to 'database_admin'@'localhost' identified by 'strong_password';

    Important security consideration: WordPress requires SELECT, INSERT, UPDATE and DELETE privileges to function normally, however some plugins, themes and some major updates may require table alteration or other functionality which would need elevated privileges. In most cases there is no need to limit these privileges but if you do, do so with caution. Attempting these updates without the proper privileges can damage or destroy your install.

    Exit MariaDB or MySQL Terminal
    Import database dump from Step 1 into the newly created database container
    mysql -u username -ppassword database_name < database_name.sql

    note: There is no space between the p's above in -ppassword, this is intended and is not a typo
    note: There is almost never a need to use flush privileges;
    In order to improve performance. MariaDB/MySQL maintains an in-memory copy of the GRANT tables, so they do not require reading it from disk on every connection, every default database change and every query sent to the server. The mentioned command forces the reload of this cache by reading it directly from disk (or the filesystem cache).

    However, its execution is unnecessary in most practical cases because, "If you modify the grant tables indirectly using account-management statements such as GRANT, REVOKE, SET PASSWORD, or RENAME USER, the server notices these changes and loads the grant tables into memory again immediately."

    There leaves only one then reason to perform that reload operation manually: "you modify the grant tables directly using statements such as INSERT, UPDATE, or DELETE"

    See the MySQL docs

  3. Move DocumentRoot to the new location | This could require webserver setup and/or configuration
  4. See this nginx WordPress recipe for getting your nginx server up and serving your WordPress site. Here's a link to the WordPress Codex article on nginx.

    Here's a sample Apache httpd config that assumes you will be using SSL. Note to WPMU users: If your site is SSL enabled you must have a wildcard ssl certificate for * or you will run into verification problems with sub-sites.

    Apache httpd Configuration

    nginx Configuration (Read More)

    openssl dhparam -out /etc/nginx/ssl/dhparam.pem 4096

    More helpful resources
  5. Edit wp-config.php | we'll need to change the database name, database user, database host, secret key salt table and add some WordPress PHP constants
  6. Additionally See: this useful guide and full explanation of the wp-config.php file & WordPress PHP Constants

  7. Generate new secret key salts and/or replace your old salts. Use the WordPress secret key salt generator.
  8. If you've used absolute URLs (even if you've used relative URLs chances are high there's still some absolute URLs in your database that are serialized), use one of the following to search/replace the old/new domain names in your database. Do not edit the database dump manually with a text editor or using sed, the URLs are stored in serialized arrays and will be corrupted.
Serialized Arrays
Use *one* of these to change serialized URL/domain (or other) values in your WordPress database
interconnectit's SearchReplaceDB tool
wp-cli's Search-Replace command
Better Search Replace WordPress Plugin

A example of a normal PHP array and a serialized PHP array from Andrew Nacin's site

Let's examine what's going on:
Normal PHP array: array( 'apple', 'banana', 'orange' );
Serialized PHP array: a:3:{i:0;s:5:"apple";i:1;s:6:"banana";i:2;s:6:"orange";}

33 elements in array
{open array
0integer value = 0
55 chars in string
}close array

Last Modified: 20 Apr, 2020 at 22:19:30