RightScale Support Wiki > Tutorials > MySQL Setup > Create a Redundant MySQL Database Setup

Create a Redundant MySQL Database Setup

Objective

The purpose of the MySQL Setup tutorial is to show you how to take an existing MySQL database and port it over onto an EC2 instance and set up a redundant master-slave database with constant backups being to S3.

NOTE: This tutorial only applies to Premium accounts.  If you have a Developer account and would like to upgrade, please contact sales@rightscale.com.

The RightScale Dashboard makes it easy to create a redundant MySQL database on EC2 with complete failover and recovery.  Learn more about our MySQL redundancy from our blog post on our RightScale Blog.

To see how easy it is to build a redundant MySQL database on the cloud, follow the tutorial below.

This tutorial is divided into 6 Steps:
Step 1: Add "Master" MySQL database to the deployment
Step 2: Add "Slave" MySQL database to the deployment
Step 3: Define Input parameters
Step 4: Launch "Master-DB"
Step 5: Create a backup of "Master-DB" on S3
Step 6: "What happens when the Master-DB is terminated?"

 

mysql_setup_step0_b.gif

Step 1 - Add "Master" MySQL database to the deployment

mysql_setup_step3.gif

The first step is to add the Master and Slave databases as components to the Production deployment.

Go to Manage -> Servers -> Deployments.

 

 

 

 

Select the Production deployment that you already created.

00-select_production_deployment.gif

 

Next, we need to add an instance on EC2 for the Master-DB. Click the Add Server button.

01-add_server.gif

 

13-add_server_master.gifSelect the most recent MySql Bootstrap server template from the RightScale list and name it MySQL Master. Select the production SSH key and production Security Group. Keep the default selections for Availability Zone and Elastic IP and click Add.

 

 

 

 

 

 

 

 

Step 2 - Add "Slave" MySQL database to the deployment

mysql_setup_step4.gifNow that the Master-DB has been added to the Production deployment, add the Slave-DB.

Click the Add Server button.

 

 

 

 

14-add_server_slave.gifThis time select the most recent MySql Additional template, and name it MySQL Slave.  Be sure to select the production SSH key and production Security Group.  Keep the default selections for Availability Zone and Elastic IP and click Add.

 

 

 

 

 

 

 

 

The Master-DB and Slave-DB are now components of the Production deployment.

07b-launch_btn_master.gif

 

Step 3 - Define Input parameters

Now we need to configure the Input parameters for the Production deployment.

05-inputs_main.gif

06-inputs_types.gif

Select the Production deployment's Inputs tab. We will now configure the Input parameters for the Master-DB and Slave-DB. Click Edit.

There are five different types of inputs:

  • Ignore – ignore this variable
  • Env – environment variable
  • Text – plain text
  • Cred – credential
  • Key – SSH key

 

AWS Credentials

Since we want to create backups of our database in S3, the templates require that you include your Amazon keys. For the first two parameters, select cred (credential) as the input type and chose your AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.

 AWS_ACCESS_KEY_ID  Cred  Select your AWS Access Key ID
 AWS_SECRET_ACCESS_KEY  Cred  Select your Secret Access Key

 

Filename Conventions

The next two parameters tell the Master-DB where to place the backups in S3. When backups are taken, the files will be named with the prefix that you define followed by a timestamp of when they were created so that you can easily verify the most recent backup.  You can define whatever prefix that you want to use.

 BACKUPFILE_PREFIX  Text  production
 BACKUP_S3_BUCKET  Text  Enter the name of the bucket that you created earlier. This is where you want the database backups to be stored in S3.

 

User Access

The following four input variables define the admin usernames and passwords for the database and replication setup.

NOTE: You need to make sure that the database users (DBADMIN_USER) are not root. 

 DBADMIN_PASSWORD  Text  Your Password
 DBADMIN_USER  Text  Your User ID(Do not use "root")
 DBREPLICATION_PASSWORD  Text  Your Password
 DBREPLICATION_USER  Text  Your User ID

 

DB Server

The DB_SERVER_USAGE parameter defines which MySQL config that you want to use. Use the dropdown menu to select your instance type (dedicated or shared).  The default is "dedicated."

 DB_SERVER_USAGE  Dropdown Menu dedicated

 

DNS

In order to handle the possibility of a failure, we will need to use a DNS record to refer to the Master-DB and Slave-DB instances. Therefore, the server will need to access your DNSMadeEasy username and password.

 DNSMADEEASY_PASSWORD  Cred  Your Password
 DNSMADEEASY_USER  Cred  Your User ID
 DNS_ID  Ignore  
 EXTERNAL_DNS_ID  Ignore  

 

 

Slave-DB

To make sure that the Slave-DB automatically initializes itself when it boots, set the INIT_SLAVE_AT_BOOT input parameter to True (default). Set to False if you do not want it to initialize at boot-time.  The only case that you would want to do this is if you don't already have a Master-DB instance running and you want to launch a slave.

 INIT_SLAVE_AT_BOOT  Text  True

 

Master-DB

Input the DNS-ID and DNS-Name values that you received during your DNSMadeEasy registration.

 MASTER_DB_DNSID  Text  Your DNS ID
 MASTER_DB_DNSNAME  Text  Your DNS Name
 master1.mysite.com

 

Monitoring

At this time you can set MON_PROCESSES to Ignore. In the future, if you want to monitor a specific process. You can set this Input parameter as Text and input the name of the process.

 MON_PROCESSES  Ignore  

 

Backup Settings: Slave-DB

OPT_BACKUP_SLAVE_ROTATION_SIZE defines the number of revolving backups for the Slave-DB to take. If you leave this parameter undefined, the default value of 6 will be used. i.e. Only the last 6 backups will be saved to S3. The oldest backup is overwritten in order to write the new backup.  By default, snapshots (backups) of a slave database are taken every 10 minutes.  Setting OPT_COMPRESS_SLAVE_BACKUPS to "no" or "false" will disable the compression of these backups which will take some of the CPU load off the slave. Setting it to any other value will enable compression which will decrease the storage size on S3.  Set both of these to Ignore

NOTE: You will only have to go to S3 in the event that you have no database server running or all running databases are corrupted.

 OPT_BACKUP_SLAVE_ROTATION_SIZE  Ignore  
 OPT_BACKUP_SLAVE_BACKUPS  Ignore  

 

User Access Settings

OPT_MYSQL_MAX_CONNECTIONS defines the number of maximum MySQL connections that you want to allow to the database at any given time. Once your deployment is operational, you might want to define a limit.  The default value is 500.

OPT_VOLUME_SIZE_GB is the size in gigabytes that you want /mnt to be formatted. This is a tradeoff between disk capacity and boot time since larger sizes will take longer to format. Use "max" to get the full capacity. The default value is 160GB.  

 OPT_MYSQL_MAX_CONNECTIONS  Ignore  
 OPT_VOLUME_SIZE_GB  Ignore  

 

 

SSH Key

The Slave-DB will need access to the Master-DB in order to take backups, so it will need the Master-DB's SSH key.

 PRIVATE_SSH_KEY  Key  production

 

Slave DNS-ID

Enter the Slave-DB DNS ID for the SLAVE_DB_DNSID.

 SLAVE_DB_DNSID  Text  The Slave-DB's DNS ID

 

Logging Server

Set the SYSLOG_SERVER variable to syslog.rightscale.com.

 SYSLOG_SERVER  Text  syslog.rightscale.com

 

 

Be sure to save all of the new Input parameters that we just defined and click Save.

 

Step 4 - Launch "Master-DB"

Now we are ready to launch the Master-DB. Click the Servers tab.  Press the green play button that corresponds to the MySQL Bootstrap instance.  Verify that the input values are correct, and click Launch. (If you forgot to provide information for any required inputs, they will be highlighted on the next screen in red. Before you can launch the instance, all required inputs must have acceptable values.)

mysql_setup_step4_launch.gif

It should take between 5 and 10 minutes for the instance to boot. You should see the instance pending in the Recent Events list on the left hand side of the browser.

We expect the server to come up in a "stranded in booting" state.  You can track the status under Recent Events.

11b-master_stranded_stop.gif

In order to take the fastest and most efficient backups possible, backups are taken as Logical Volume Management (LVM) snapshots. The Bootstrap template is designed to get the most recent backup from S3 upon launch. Since this is the first time we are launching the Master-DB, the template will not find such a backup in S3, so the startup process will fail. But this is expected and OK.  In the next step we will add a backup of the Master-DB to S3.

Use SSH to apply the MySQL-DB 

Once the instance comes up as "stranded in booting," use the SSH Console to SSH into the Master-DB and manually apply your *.sql dump file. See the sample code below. 

  • s3bucket - the name of the S3 bucket that contains the MySQL dump file
  • DUMPFILE - the MySQL database filename (*.sql)
  • DBSCHEMA - the name of the MySQL database
wget http://s3bucket.s3.amazonaws.com/DUMPFILE.gz

gunzip DUMPFILE.gz

mysqladmin -u root create DBSCHEMA

mysql -u root DBSCHEMA < DUMPFILE.sql

 

Set Application Permissions

Once you've applied your MySQL database, you should now set the application password.  You will need to set the application user and password before you take the initial database backup. You only need to do this once because the values will be stored in the database and will be available to the next Master-DB that pulls from S3.

The following script shows how you can permit full read/write permissions to all databases for a particular user. You may want to customize this script depending on your specific needs. Be sure to replace the default "user" and "password" values with your own information.

#!/bin/bash -e
# Copyright (c) 2007 by RightScale Inc., all rights reserved

#Set your Application username and password here
DBAPPLICATION_USER=user
DBAPPLICATION_PASSWORD=password

# Enable remote administration
echo 'GRANT ALL PRIVILEGES on *.* TO '\'$DBAPPLICATION_USER\''@'\''%'\'' IDENTIFIED BY '\'$DBAPPLICATION_PASSWORD\'' ;' | mysql -u root
echo 'GRANT ALL PRIVILEGES on *.* TO '\'$DBAPPLICATION_USER\''@'\''localhost'\'' IDENTIFIED BY '\'$DBAPPLICATION_PASSWORD\'' ;' | mysql -u root
echo "FLUSH PRIVILEGES;" | mysql -u root

 

Step 5 - Create a backup of "Master-DB" on S3

The next step is to create a backup of the Master-DB to S3 for the first time.

Go to the running instance page (Shortcut: Click the RightScale logo).
Click on MySQL Master under the Production deployment.
Click the Scripts tab.

Execute the "DB backup" operational action by clicking the Run button.

08-db_backup_btn.gif

You will see this task pending in the Recent Events list. It will take a few minutes to complete depending on the size of the database.

When it is "done," navigate to the S3 bucket where you put the backups and verify that a backup was saved. Go to the S3 Browser (Manage -> Servers -> Storage).

Click on the bucket that you created in the Create an S3 Bucket step.

09-s3_backup_bucket.gif

 

You should now see a backup file of your Master-DB. Notice that the filename has the "production" prefix that you specified in the Filename Conventions inputs section along with a timestamp.

10-s3_backup_file.gif

 

Step 6 - "What happens when the Master-DB is terminated?"

Our setup is almost complete. Now that there is a backup of the Master-DB in your S3 bucket, let's relaunch the Master-DB instance. This time it will no longer become stuck in the "stranded in booting" state because the template will now find a backup of the Master-DB database in your S3 bucket. But, since our Master-DB instance is currently running, we'll want to stop/terminate it. Click the Stop button for the MySQL Master instance.

11c-master_stranded_stop.gif

Now you can go back to the deployment and terminate the Master-DB MySQL instance and relaunch. If everything is properly configured, it will reach the "operational" state.

12b-master_operational.gif

After the Master is up, click the Launch button for the MySQL Slave instance.

Once the MySQL Slave instance is operational it will begin taking regular backups of the Master-DB and save these backups to your S3 bucket in 10 minute intervals.

mysql_setup_final.gif

Congratulations! You just set up a redundant MySQL database on EC2 that is saving regular backups on S3. Now that your servers are up, use the  Manager for MySQL Runbook as a user guide for managing your MySQL setup.

 

 ----------------------

Did you find this document helpful?  Please feel free to leave us a comment below.  We're always looking for ways  to improve our documentation.  Thanks!

 

Tag page

Files 2

FileSizeDateAttached by 
 Manager for MySQL Runbook.pdf
No description
132.64 kB18:39, 8 Apr 2008martinActions
mysql_failover_wiki_v1.swf
Redundant MySQL Setup with Failover and Recovery
14.03 kB18:50, 9 Jun 2008deanoActions
Viewing 6 of 6 comments: view all
dedicated-small is not a valid instance name; I believe it works better to use m1-small or similar.
Posted 20:15, 4 Jun 2008
Hi Efremen,

That parameter refers to which mysql config file to use, not just the size of the instance. I have tried to clarified this.

When we created this template, AWS only provided the 'm' series of instances so it was not necessary to integrate this into the config file name, but now with the 'c' series, it might make sense to do so.

Thanks for your comment,

Martin
Posted 21:29, 4 Jun 2008
In the current deployment script Inputs, there is a variable called EXTERNAL_DNS_ID which has no clear explanation for what to set it to. I'm not sure how it's different than MASTER_DB_DNSID?
Posted 23:56, 23 Jun 2008
Hi blitzlocal,

Sorry for the delay but the difference is that the EXTERNAL_DNS_ID, is so that users can connect to it remotely.
Posted 17:03, 2 Jul 2008
The space on disk for the Data Base is less than the total 160 gig on a small. Some of the space is used for the Data Base backups.

The best way to KNOW exactly what is value is on any server is to use the SSH Login and do the disk free command.

I like the "df -h" for a more human readable format. The missing space is reserved for Backups and accounts for the difference you see on the running servers.

If you are close to full on your disks you need to migrate to a larger system soon. (or delete a new records).

Ed@RightScale.com
Posted 06:20, 29 Jul 2008
Suggestion for doing the db import.

nohup mysql - u root DBSCHEMA < DUMPFILE.sql &

If your DUMPFile is big, it is going to take a while to import it all in. And your ssh connection might time out.
Posted 01:09, 30 Sep 2008
Viewing 6 of 6 comments: view all
You must login to post a comment.