Previous: , Up: Task 5---Migrating to Amazon Relational Database Service   [Contents][Index]


3.7.3 Reconfigure the Database Connection

In this task, you will again update your application configuration file (config.php) to point to the Amazon RDS database.

Because your current Lightsail instances all run under a load balancer, it would be unwise to reconfigure only some of them to point to the Amazon RDS database. Doing so could result in a situation where the load balancer would present some front ends that connect to the Lightsail database, and other front ends that connect to the Amazon RDS database.

To avoid this situation, you will deploy a new PHP front end instance based on your existing snapshot, and then modify that instance.

  1. Navigate to the Lightsail snapshots page.
  2. Next to PHP-fe-1:
  3. Name the instance php-fe-rds.
  4. Scroll to the bottom of the screen and click ‘Create instance’.

    Now that you have a new instance to work from, you can reconfigure the configuration file to point to the Amazon RDS database.

  5. Connect to the php-fe-rds instance through SSH.
  6. Navigate to the Amazon RDS databases page.
  7. From the list of databases, click the name of the Amazon RDS database you created earlier (the suggested name for this database was tasks-db to access the database details screen.
  8. In the ‘Connectivity’ section, copy your Endpoint.
  9. Return to the SSH session for the php-fe-rds instance.
  10. Create an environment variable (RDS_ENDPOINT) to hold the value of your RDS database endpoint by:
    RDS_ENDPOINT=<rds-endpoint>
    
  11. Set environment variables for the default user name (dbmasteruser) and the password (taskstasks):
    RDS_USERNAME=dbmasteruser
    RDS_PASSWORD=taskstasks
    
  12. Verify the environment variables are set correctly:
    echo "Endpoint = "$RDS_ENDPOINT
    echo "Username = "$RDS_USERNAME
    echo "Password = "$RDS_PASSWORD
    
  13. Create a new configuration file that points to the Amazon RDS database:
         cat /opt/bitnami/apache2/configs/config.php.bak | \
             sed "s/<endpoint>/$RDS_ENDPOINT/; \
             s/<username>/$RDS_USERNAME/; \
             s/<password>/$RDS_PASSWORD/;" \
             > /opt/bitnami/apache2/configs/config.php.rds_db
    
  14. Activate the new configuration by replacing the existing config.php with the new version:
    cp /opt/bitnami/apache2/configs/config.php.rds_db /opt/bitnami/apache2/configs/config.php
    
  15. Verify the values of the new config.php file:
    cat /opt/bitnami/apache2/configs/config.php
    
  16. In a browser tap install the new database:
    http://PHP-FE-RDS/install.php
    

    You should get the following error:

    SQLSTATE[HY000] [1049] Unknown database 'tasks'
    

    This is because while you can reach the Amazon RDS server, the ’tasks’ database has not yet been created.

  17. In the final step, you will migrate the data from your Amazon Lightsail database into your Amazon RDS database.

    In the SSH window create an environment variable name LS_ENDPOINT to hold the value of the endpoint of your database.

    LS_ENDPOINT=<IP-address>
    

    Your variable should look similar to:

    LS_ENDPOINT='ls-966d5bf6be8ee5178432a633398bf4256bfcab69.cucxkvhp11zu.us-west-2.rds.amazonaws.com'
    
  18. Set environment variables for the default user name (dbmasteruser) and the password (taskstasks):
    LS_USERNAME=dbmasteruser
    LS_PASSWORD=taskstasks
    
  19. Check to make sure the environment variables are set correctly (the output from the command below should match the values you just set for the LS endpoint, the user name, and the password):
    echo "Endpoint = "$LS_ENDPOINT
    echo "username = "$LS_USERNAME
    echo "Password = "$RDS_PASSWORD
    
  20. Issue the following command to export the database file into a file tasks.sql:
         mysqldump -u $LS_USERNAME \
            --host $LS_ENDPOINT \
            --databases tasks \
            --single-transaction \
            --compress \
            --order-by-primary  \
            --set-gtid-purged=OFF \
            -p$LS_PASSWORD  > tasks.sql
    
  21. Access your RDS instance via the mysql command line tool:
         mysql -u $RDS_username \
           --port=3306 \
           --host=$RDS_ENDPOINT \
           -p$RDS_PASSWORD
    
  22. Import the previously created database dump file into MySQL:
    source tasks.sql
    
  23. In a browser tab:
    http://<IP-address>
    

    You should see that the tasks you created originally are now present in the database that is managed by Amazon RDS.

    From this point, you could repeat the steps from Task 4 and create a new snapshot from your php-fe-rds instance, deploy two new instances from that new snapshot, and replace the existing instances in your load balancer with your three new instances that use Amazon RDS.

    This process would give you a redundant web front end that runs in Amazon Lightsail, with the database running in Amazon RDS. However, for this lab you will not do that.


Previous: , Up: Task 5---Migrating to Amazon Relational Database Service   [Contents][Index]