Support Search

    Objective

    To set up two MySQL 5.1/5.5 database servers running in an asynchronously replicated (master/slave) configuration in a single deployment in a public or private cloud environment.

    Table of Contents

    Prerequisites

    • This tutorial only applies to paid-edition RightScale accounts. If you have a free edition account and would like to upgrade, contact sales@rightscale.com.
    • You must log in under a RightScale account with "actor," "designer," "security manager" and "library" user roles in order to complete the tutorial.
    • For Amazon EC2, CloudStack, and other clouds that support security groups, you must have a security group defined with TCP port 22 open for SSH access, and any other ports required by the server (for example, the default MySQL port, TCP port 3306), for the required security groups and IP addresses. Also, remember that iptables is installed by default on all servers, as described under Security below.
    • We strongly recommend that you set up credentials for password values and any other sensitive data included as Chef recipe inputs. Also, some default input values assume that predefined credentials exist, such as the MySQL user name and password credentials described in Create Credentials for Common Inputs.
    • This tutorial installs a blank MySQL database and uses a mysql dump file stored in an Amazon S3 bucket or Rackspace Cloudfile container to restore asingle database. If you wish to restore a database from an Primary backup, please review the Database Manager for MySQL 5.1/5.5 Runbook.
    • In MySQL, do not set a password for the "root" user; otherwise, the recipes will fail. The “root” user should only be able to log in from “localhost”.
    • To import a MySQL database, you will need a MySQL dump file stored in an Amazon S3 bucket or RackSpacee CloudFiles container. If you only wish to install a blank MySQL server, the data import step can be skipped.

    Overview

    This tutorial describes the steps for launching MySQL database servers running in an asynchronously replicated (master/slave) configuration in the cloud.

    Backup and Storage

    The Database Manager for MySQL 5.1 ServerTemplate supports the use of a primary and secondary storage location when backing up databases.

    Primary Storage

    When you launch your database server and run an operational recipe to initialize it as either a master or slave (see Initialize the Master Server and Initialize the Slave Server), the operational recipe schedules a cron job to save backups to your primary storage type, which is automatically selected for you based on your cloud provider. For clouds that support it, volume-based storage is always used as the primary storage location. For example:

    • Amazon EC2 - Amazon EBS volumes (single or striped volumes)
    • Rackspace - Rackspace Cloud Files
    • CloudStack - CloudStack volumes (single or striped volumes)
       

    By default, the primary scheduled backup job runs once per hour on the slave server, and once every four hours on the master server.

    Note: For cloud providers with volume support, you can assign a RightScale-specific nickname to the storage volumes attached to your servers, for convenience and organization purposes, using the "Nickname" input. If the "Nickname" input is not set, volumes are assigned the default "data_storage" nickname.

    Secondary Storage

    The Database Manager for MySQL 5.1 and Database Manager for MySQL 5.5 ServerTemplates also let you select a secondary backup location from the following available public-cloud remote object storage (ROS) providers:

    • Amazon S3
    • Rackspace Cloud Files
       

    ROS is a generic term describing cloud storage services for static files. Data is stored as files (or objects) in named containers, which typically govern access permissions.

    While secondary backups are typically slower and more resource-intensive than primary backups, they are recommended and useful in disaster-recovery scenarios. You can run secondary backups manually as needed using the db::do_secondary_backup operational recipe, as described in the Database Manager for MySQL 5.1/5.5 Runbook.

    Security

    The methods you will use to secure access to your MySQL database server depend on your cloud provider. For Amazon EC2, CloudStack, and other clouds that support security groups, you must have a security group defined with TCP port 22 open for SSH access, along with any other ports required by the server (for example, TCP port 3306 for MySQL client access).

    Iptables is also enabled by default on all servers, with the default SSH port (22) and web-access ports (80 and 443) open. For each database server, the default MySQL client port, TCP port 3306, is automatically opened to one or more other servers in your architecture depending on the server's replication role:

    • For the master, port 3306 is opened to application servers in the same deployment that are based on the PHP App Server ServerTemplate. For more information, see the PHP Application Server Setup tutorial. 
    • For the slave, port 3306 is opened to the master database server when the db::do_primary_init_slave operational recipe is run, as described under Initialize the Slave Server.
       

    If you need to open other ports on a database server besides the default TCP ports (22, 80, 443, and 3306), you must create the appropriate rules using the "Firewall" inputs, as described in the Base ServerTemplate for Linux (Chef) documentation.

    Note: For more information on iptables, refer to the Linux supporting documentation for this tool.

    Create Credentials

    Prerequisite: Requires 'actor' user role privileges in the RightScale account.

    In order to securely pass sensitive information to a script at runtime, you can use Credentials as a means of variable substitution. Later in this tutorial you will select these credentials when you define your inputs.

    Create the following credentials.  For more information on setting up credentials, see Create a New Credential.

    • DBADMIN_USER - Username of a database user with admin-level privileges.
    • DBADMIN_PASSWORD - Matching password for DBADMIN_USER.
    • DBAPPLICATION_USER - User name of a database user with user-level privileges.
    • DBAPPLICATION_PASSWORD - Matching password for DBAPPLICATION_USER.
    • DBREPLICATION_USER - User name of a database user with replication permissions on the server.
    • DBREPLICATION_PASSWORD - Matching password for DBREPLICATION_USER.
    • DNS_USER*Username that's used to log into your DNS provider and access your DNS records.
    • DNS_PASSWORD* - Password for DNS_USER.

    If you use Amazon Route 53 as your DNS provider, you do not need to set up separate DNS user name and password credentials because your AWS credentials are used for authentication purposes. 

    Depending on your cloud provider and backup storage selections, you may need to create additional credentials.

    Amazon Snapshots

    If you are using Amazon to make snapshot/binary backups of your database, you will need to use the following credentials. Fortunately, these credentials were automatically created when you added your AWS credentials to the RightScale account. Note: They are not listed under Design -> Credentials.

    • AWS_ACCESS_KEY_ID - Amazon access key ID for authentication.
    • AWS_SECRET_ACCESS_KEY - Amazon secret key corresponding to AWS_ACCESS_KEY_ID.


    Rackspace Cloud Files

    If you are using Rackspace Cloud Files for storing binary database backups, you will need to create the following credentials.

    • RACKSPACE_USERNAME - The username used to log into Rackspace's Cloud Control Panel. Use this credential for the "Backup Primary User" and/or the "Secondary Backup User" input if you are using Rackspace Cloud Files for primary and/or secondary backups. 
    • RACKSPACE_AUTH_KEY - The Rackspace account API key. Use this credential for the "Backup Primary Secret" and/or the "Secondary Backup Secret" input if you are using Rackspace Cloud Files for primary and/or secondary backups.

    Steps

    Upload the Database Dump File

    The ServerTemplate contains scripts that can retrieve a MySQL database dump file from either an Amazon S3 bucket or a Rackspace Cloud Files container. Create a new bucket/container and upload your database dump file. The file can remain a 'private' object because your cloud credentials can be used (as inputs) for authentication purposes to retrieve the file. Make sure the uploaded file maintains the .gz file extension.


    If you are setting up a database server for testing purposes, you may use the following sample MySQL dump file to complete the tutorial.

    Create a Database Server

    Follow these steps to add a database server to the deployment.

    1. Go to the MultiCloud Marketplace (Design -> MultiCloud Marketplace -> ServerTemplates) and import the most recently published revision of the following ServerTemplate into the RightScale account.
      • Database Manager for MySQL 5.1 or Database Manager for MySQL 5.5
    2. From the imported ServerTemplate's show page, click the Add Server button.
    3. Select the cloud for which you will configure a server. 
    4. Select the deployment into which the new server will be placed.
    5. Next, the Add Server Assistant wizard will walk you through the remaining steps that are required to create a server based on the selected cloud.
      • Server Name - Provide a nickname for your new database server (e.g., mysql-db1). Do not include "master" or "slave" in the name, because a database server's role can change in the future.
      • Select the appropriate cloud-specific resources (e.g. SSH Key, Security Group, etc.) that are required in order to launch a server into the chosen cloud. The required cloud resources may differ depending on the type of cloud infrastructure. If the cloud supports multiple datacenters / zones, select a specific zone. Later, when you create the other database server you will use a different datacenter / zone to ensure high-availability. For more information, see Add Server Assistant.
    6. Click Confirm, review the server's configuration and click Finish to create the server.

    Configure Inputs

    The next step is to define the properties of your database server or servers by entering values for inputs. It is simplest and best to do this at the deployment level. For a detailed explanation of how inputs are defined and used in Chef recipes and RightScripts, see Inputs and their Hierarchy.

    The inputs that you need to provide values for will depend on which options you're going to use. The ServerTemplate is very flexible and supports a variety of different configurations. You will need to provide the necessary values as inputs based on which options you want to use.

    • Where will the contents of the database be stored?
      • On volumes attached to the instance
      • On the local/ephemeral drive - Ignore all "Block Device" inputs.
    • If you're using volumes, are you going to use a stripe?
      • Yes - Use a stripe of multiple volumes
      • No - Use a single volume. 
      • INPUTS: Block Device Mount Directory (1), Nickname (1), Number of Volumes in the Stripe (1), Total Volume Size (1)
    • Are you building a master-slave database setup with replication?
      • Yes
      • No
      • INPUTS: Database Master FQDN, Database Master DNS Record ID, Database Slave FQDN, Database Slave DNS Record ID, Database Replication Password, Database Replication Username
    • What are you going to use to take "primary" backups of the database?
      • Snapshots
      • Binary Dumps to a ROS container (e.g. S3 bucket or Cloud Files container)
      • INPUTS: Backup Primary Secret (default), Backup Primary User (default), Primary Backup Storage Cloud (default)
    • Are you going take "secondary" backups of the database? If yes, which ROS provider will you use?
      • Amazon S3
      • Rackspace Cloud Files
      • INPUTS: Secondary Backup Storage Cloud (default), Secondary Backup Secret (default), Secondary Backup User (default), Secondary Backup Storage Container (1)
    • Which DNS provider are you using for dynamic DNS at the database level?
      • DNS Made Easy
      • DynDNS
      • Amazon Route 53
      • INPUTS: DNS Service Provider, DNS Password, DNS User

     

    Set Inputs at the Deployment Level
    Go to the deployment's Inputs tab and click Edit.

    Although you can enter values for missing inputs as text values, it's strongly recommended that you set up credentials for passing sensitive information to scripts such as passwords or any other sensitive data.

    Rackspace only
    If you use Rackspace for your database servers and backup storage (i.e., Cloud Files) the storage-related Chef recipes will use Rackspace Service Net (SNET) by default. SNET is Rackspace's internal private networking service for optimized communication between Rackspace Cloud Servers and Cloud Files. If SNET is not supported in your Rackspace environment, you must set the "Rackspace SNET Enabled for Backup" input to false; otherwise, all backup and restore operations that rely on Cloud Files will fail.

    Block Device

    If the cloud supports the use of block devices (e.g. AWS EBS Volumes, CloudStack volumes, etc.), it's strongly recommended that you use block devices to store the contents of the MySQL database. Backups of the database will be stored as snapshots.

    If the cloud does not support block devices (e.g. Rackspace), you must use a Remote Object Store container (e.g. Rackspace Cloud Files container) the contents of the MySQL database will be stored locally on the instance's ephemeral drive. Backups of the database will be stored as binary dump files to the specified storage container.

    Required

    Input Name
    Description
    Example Value
    Number of Volumes in the Stripe (1) To use striped volumes with your databases, specify a volume quantity. The default is 1, indicating no volume striping. Ignored for clouds that do not support volume-based storage (e.g., Rackspace). text:1
    Total Volume Size (1)

    Specify the total size, in GB, of the volume or striped volume set used for primary backup. If dividing this value by the stripe volume quantity does not yield a whole number, then each volume's size is rounded up to the nearest whole integer. For example, if "Number of Volumes in the Stripe" is 3 and you specify a "Total Volume Size" of 5 GB, each volume will be 2 GB.

    If deploying on a CloudStack-based cloud that does not allow custom volume sizes, the smallest predefined volume size is used instead of the size specified here. This input is ignored for clouds that do not support volume storage (e.g., Rackspace).

    Important! 50% of each volume's size is reserved for LVM snapshots and is not part of the usable space. Therefore, your total volume size should be at least twice the size of your databases, with additional room for growth as needed.

    text:10

     

    Optional

    Input Name
    Description
    Example Value

    Backup Primary Secret (default)

    Backup Primary User (default)

    For Rackspace, specify the Rackspace username and API key to use for Rackspace Cloud Files authentication. For Amazon and other cloud providers, set these to "ignore" since authentication is not required for volume storage.

    No value/Ignore

    cred:RACKSPACE_AUTH_KEY
    cred:RACKSPACE_USERNAME

    Secondary Backup Storage Cloud (default)

    Cloud provider for the remote object storage (ROS) to use as your secondary backup location: either "s3" for Amazon S3, or "cloudfiles" for Rackspace Cloud Files.

    text:cloudfiles

    Secondary Backup Secret (default)

    Secondary Backup User (default)

    If using Amazon S3 for secondary backup storage, specify your Amazon access keys (key ID and secret key) to use for authentication. If using Rackspace Cloud Files, specify your Rackspace username and API key.

    cred:AWS_SECRET_ACCESS_KEY
    cred:AWS_ACCESS_KEY_ID

    cred:RACKSPACE_AUTH_KEY
    cred:RACKSPACE_USERNAME

    Secondary Backup Storage Container (1) Name of Amazon S3 bucket or Rackspace Cloud Files container to use for secondary backups. text:mysqlbackups
    Block Device Mount Directory (1) For cloud providers supporting volume-based storage, the mount point for your backup volume or volumes. (Default is /mnt/storage.) Ignored for clouds that do not support volume-based storage (e.g. Rackspace). text:/mnt/storage
    Nickname (1) For cloud providers supporting volume-based storage, the nickname will be used to name the created volumes and snapshots along with an epoch timestamp (e.g. data_storage-201203100927) This input is ignored for clouds that do not support volume-based storage (e.g. Rackspace). text:data_storage

    DB

    Input Name
    Description
    Example Value

    Database Admin Password

    Database Admin Username

    Username and password of a database user with administrator privileges. The admin username and password are used for tasks that require administrator access to the database.

    cred:DBADMIN_PASSWORD

    cred:DBADMIN_USER

    Database Application Password

    Database Application Username

    Username and password of a database user with user-level privileges. The application username and password allow the application to access the database in a restricted fashion.

    cred:DBAPPLICATION_PASSWORD

    cred:DBAPPLICATION_USER

    Database Backup Lineage

    Name associated with your primary and secondary database backups, used to associate them with your database environment for maintenance, restore, and replication purposes. Backup snapshots will automatically be tagged with this value. (e.g. rs_backup:lineage=mysqlbackup) 

    Note: For servers running on Rackspace, this value also indicates the Cloud Files container to use for storing primary backups. If a Cloud Files container with this name does not already exist, the setup process creates one.

    text:mysqlbackup
    Database Master FQDN Fully qualified domain name for the master database server. text:master-db.example.com
    Database Master DNS Record ID

    The record ID or hostname used to identify your master database server to your DNS provider. See Domain Setup for more information.

    Examples:

    • DNSMadeEasy: 1234567  (Dynamic DNS ID)
    • Route53: Z3DSDFSDFX:db-master.aws.example.com
    • DynDNS: db-master.example.com
    text:1234567
    Database Slave DNS Record ID (Optional) The record ID or hostname used to identify your master database server to your DNS provider. See Domain Setup for more information. text:2233445

    Database Replication Password

    Database Replication User

    Username and password of a database user with replication permissions on the MySQL server. The replication username and password are used for replication between the "master" and "slave" database servers.

    cred:DBREPLICATION_PASSWORD

    cred:DBREPLICATION_USER

    SYS_DNS

    Input Name
    Description
    Example Value
    DNS Service Provider Select the DNS provider that you used to create the DNS records for the database servers. Choose "DNSMadeEasy," "DynDNS," or "Route53" (Amazon Route 53). text:DNSMadeEasy

    DNS Password

    DNS User

    The user name and password used to log into your DNS provider. For Amazon Route 53, specify your Amazon access keys (key ID and secret key).

    cred:DNS_PASSWORD

    cred:DNS_USER

    Launch the Database Server

    After configuring your inputs, launch your newly configured master database server.

    1. Go to the deployment's Servers tab and launch the database server. When you view the input confirmation should page, there should not be any required inputs with missing values.  If there are any required inputs that are missing values (highlighted in red) at the input confirmation page, cancel the launch and add values for those inputs at the deployment level before launching the server again. Refer to the instructions in Launch a Server if you are not familiar with this process.

    Initialize the Master Database Server

    Wait for the server to reach the "operational" state before you run a script to initialize the database server.

    1. Go to the "current" server's Scripts tab and run the db::do_init_and_become_master operational script to initialize it as the "Master" database server.

    screen-InitBecomeMaster-v2.png


    The script will perform the following actions:

    • Register it as the "master" database server and assign appropriate replication privileges.
    • For cloud providers with volume support, create and mount either a single volume or group of striped volumes for data storage, based on the inputs configured for your primary database backups.
    • Create a backup.
    • Schedule a cron job to run backups to primary storage once every four hours on the server. (For information on modifying the default backup schedule, see the Database Manager for MySQL 5.1/5.5 Runbook.)
    • Updates the dynamic DNS record for the "Master" database with your DNS provider. The DNS record will be updated with the server's private IP address. 
      Warning! If using Amazon Route 53 as your DNS provider, ensure that the TTL (time to live) is set to 60 seconds for each DNS record. Using a different TTL value will cause the db::do_init_and_become_master script to fail.

    Disable Scheduled Primary Backups

    Since you have not loaded an actual database onto the server there is no reason to create a primary backup of the database.

    Go to the "current" server's Scripts tab and run the db::do_primary_backup_schedule_disable operational script to disable your scheduled backups (cron jobs). 

    Later, once you have imported your database you will reverse this action and enable continuous backups.

    Set Up the Database

    After initializing the master database server and disabling scheduled backups, you will need to add your database (or databases) and records to it.

    1. Go to the "current" server's Scripts tab and run the db::do_dump_import operational script to import a MySQL dump file from an ROS location.


    Note: If you use a previous backup snapshot instead of a MySQL dump file or initialize a blank MySQL database, refer to the Database Manager for MySQL 5.1/5.5 Runbook for instructions.

    Input Name
    Description
    Example Value
    Dump Container Name of Amazon S3 bucket or Rackspace Cloud Files container with the MySQL dump file to import upon server startup. text:mysqldumps
    Dump Schema/Database Name

    Name of the MySQL database schema to restore from the MySQL dump file identified by the "Dump Prefix" input. This name is set when you import the dump file into MySQL. The name is only defined within the MySQL instance and not within the actual dump file. As a result the name is somewhat arbitrary but should be descriptive.

    Important!
    Be sure to record this value. You will need to specify this value again when you set up the application server tier so that they can  connect to the correct database schema.

    text:my_db_schema
    Dump Prefix

    The prefix of the MySQL dump file (without the associated .gz extension) to retrieve from the Amazon S3 bucket or Rackspace Cloud Files container specified in "Dump Container." You can specify either the entire file name including the timestamp or just the file prefix without the timestamp, which selects the most recent dump file with that prefix.

    Example: If your dump file is named "mydb-201112202212.gz," you could specify either "mydb-201112202212" or "mydb."

    If you are using the attached sample MySQL dump file (app_test-201109010029.gz), use "app_test" as the value.

    text:app_test

    Dump Storage Account ID

    Dump Storage Account Secret

    If using Amazon S3 for MySQL dump file storage, specify your Amazon access keys (key ID and secret key) to use for authentication. If using Rackspace Cloud Files, specify your Rackspace username and API key.

    Note: If your dump file is a public object in an Amazon S3 bucket, you do not need to specify your key ID or secret key here.

    cred:AWS_ACCESS_KEY_ID
    cred:AWS_SECRET_ACCESS_KEY

    cred:RACKSPACE_USERNAME
    cred:RACKSPACE_AUTH_KEY

    Dump Storage Account Provider The remote object storage provider where your MySQL dump file is stored.  (Amazon) s3, (Rackspace) cloudfiles, etc. text:s3

    Create a Primary Backup

    You are now ready to create the first primary backup of the database. You will need a completed backup in order to initialize a slave database server.

    1. Go to the "current" server's Scripts tab and run the db::do_primary_backup operational script to manually generate a primary backup of your database server.

    Enable Scheduled Primary Backups

    It is now safe to enable continuous backups of the database server.

    1. Go to the "current" server's Scripts tab and run the db::do_primary_backup_schedule_enable operational script.


    For more information on configuring and modifying your scheduled backup policy, see the Database Manager for MySQL 5.1/5.5 Runbook.

    Add a Slave Database Server

    Although you can run MySQL in single-server mode and having a separate slave server for replication purposes is not required, this is strongly recommended for failover purposes. To create a slave server in your deployment.

    1. Clone the Master-DB server. See Clone a Server.
    2. Rename the server accordingly. (e.g. mysql-db2) Remember, you do not want to include the word "slave" in the nickname because this server may become the "master" server during a failover scenario. You don't want the server's nickname to potentially cause any confusion.
    3. Under the server's Info tab, click Edit and change the server's availability zone. In order to ensure high availability, it's strongly recommended that you launch the Slave-DB server in a different availability zone as the Master-DB.  Note: Cross-zone data transfer costs may apply.

    Launch the Slave Database Server

    Make sure the following conditions are true before you launch the second database server.

    • The master database server state is "operational."
    • The initial primary backup of the master database server is 100% complete. If you are using a cloud that supports snapshots for backups, you can track the status in the dashboard (Clouds -> Region -> Snapshots). The time required to complete the initial primary backup will vary based on factors such as storage type, volume size, etc.


    You are now ready to launch a "slave" database server for failover and redundancy purposes. 

    1. Go to the deployment's Servers tab and launch the database server. There should no missing inputs. 

    Initialize the Slave Database Server

    Wait for the server to reach the "operational" state before initializing it as a "slave" of the master database server.

    1. Go to the "current" server's Scripts tab and run the db::do_primary_init_slave operational script.


    The script will perform the following actions:

    • Assign the "slave" role to the server.
    • Use the most recently completed database backup (default) to initially populate the database in order to reduce the time needed for the slave to become in-sync with the master.
    • Send a request to the master server to allow connections from the slave server's private IP address, and open the default MySQL client port, TCP port 3306, on the master server's firewall (i.e., iptables) for this purpose.
    • Schedule a cron job to run primary backups of the database. 
    • If you set a value for the 'Database Slave DNS Record ID' input (optional), it will update the DNS record for the "slave" database server with its private IP address. 

    Test Database Setup (optional)

    If you want to test the status of the "master" and "slave" database servers, see Checking master or slave database status.

    Powered by MindTouch