Tutorial: Connect a WordPress website to a MySQL managed database in Lightsail - Amazon Lightsail

Tutorial: Connect a WordPress website to a MySQL managed database in Lightsail

Crucial WordPress website data for posts, pages, and users, is stored on the MySQL database that is running on your instance in Amazon Lightsail. If your instance fails, your data may become unrecoverable. To prevent this scenario, you should transfer your website data to a MySQL managed database.

In this tutorial, we show you how to transfer your WordPress website data to a MySQL managed database in Lightsail. We also show you how to edit the WordPress configuration (wp-config.php) file on your instance so that your website connects to the managed database, and stops connecting to the database running on the instance.

Contents

Step 1: Complete the prerequisites

Complete the following prerequisites before getting started:

Step 2: Transfer the WordPress database to your MySQL managed database

Complete the following procedure to transfer your WordPress website data to your MySQL managed database in Lightsail.

  1. Sign in to the Lightsail console.

  2. In the Instances tab, choose the browser-based SSH client icon for your WordPress instance.

    
            The browser-based SSH client icon in the Lightsail console.
  3. After the browser-based SSH client is connected to your WordPress instance, enter the following command to transfer the data in the bitnami_wordpress database that is on your instance to your MySQL managed database. Be sure to replace DbUserName with the user name of your managed database, and replace DbEndpoint with the endpoint address of your managed database.

    sudo mysqldump -u root --databases bitnami_wordpress --single-transaction --compress --order-by-primary -p$(cat /home/bitnami/bitnami_application_password) | sudo mysql -u DbUserName --host DbEndpoint --password

    Example

    sudo mysqldump -u root --databases bitnami_wordpress --single-transaction --compress --order-by-primary -p$(cat /home/bitnami/bitnami_application_password) | sudo mysql -u dbmasteruser --host ls-abc123exampleE67890.czowadgeezqi.us-west-2.rds.amazonaws.com --password
  4. At the prompt, enter the password for your MySQL managed database, and press Enter.

    You will not be able to see the password as it is being typed.

    
            Password prompt to transfer WordPress database to a MySQL managed database in
              Lightsail.
  5. A response similar to the following example is displayed if the data was successfully transferred.

    If you get an error, confirm that you’re using the correct database user name, password, or endpoint, and try again.

    
            Successfully transferred WordPress database to a MySQL managed database in
              Lightsail.

Step 3: Configure WordPress to connect to your MySQL managed database

Complete the following procedure to edit the WordPress configuration file (wp-config.php) so that your website connects to your MySQL managed database.

  1. In the browser-based SSH client that is connected to your WordPress instance, enter the following command to create a backup of the wp-config.php file in case something goes wrong.

    cp /opt/bitnami/wordpress/wp-config.php /opt/bitnami/wordpress/wp-config.php-backup
  2. Enter the following command to open the wp-config.php file using the Nano text editor.

    nano /opt/bitnami/wordpress/wp-config.php
  3. Scroll down until you find the values for DB_USER, DB_PASSWORD, and DB_HOST as shown in the following example.

    
            Wordpress configuration file before modifications.
  4. Modify the following values:

    • DB_USER — Edit this to match the user name of your MySQL managed database. The default primary user name for Lightsail managed databases is dbmasteruser.

    • DB_PASSWORD — Edit this to match the strong password of your MySQL managed database. For more information, see Manage your database password.

    • DB_HOST — Edit this to match the endpoint of your MySQL managed database. Be sure to add the :3306 port number at the end of the host address. For example ls-abc123exampleE67890.czowadgeezqi.us-west-2.rds.amazonaws.com:3306.

    The result should look like the following example.

    
            Modifications to the WordPress configuration file.
  5. Press Ctrl+X to exit Nano, then press Y and Enter to save your edits.

  6. Enter the following command to restart the web services on your instance.

    sudo /opt/bitnami/ctlscript.sh restart

    A result similar to the following example is displayed when the services have restarted.

    
            Restarting server services on the WordPress instances.

    Congratulations! Your WordPress site is now configured to use the MySQL managed database.

    Note

    If for any reason you need to restore the original wp-config.php file, enter the following command to restore it using the backup you created earlier in this tutorial.

    cp /opt/bitnami/wordpress/wp-config.php-backup /opt/bitnami/wordpress/wp-config.php

Step 4: Complete the next steps

You should complete these additional steps after you’re done connecting your WordPress website to a MySQL managed database: