AWS Elastic Beanstalk
Developer Guide (API Version 2010-12-01)
« PreviousNext »
View the PDF for this guide.Go to the AWS Discussion Forum for this product.Go to the Kindle Store to download this guide in Kindle format.Did this page help you?  Yes | No |  Tell us about it...

Using Amazon RDS and MySQL Connector/J

With Amazon Relational Database Service (Amazon RDS), you can quickly and easily provision and maintain a MySQL, Oracle, or Microsoft SQL Server instance in the cloud. For more information about Amazon RDS, go to http://aws.amazon.com/rds/.

This topic explains how you can use Amazon RDS and MySQL Connector/J with your AWS Elastic Beanstalk Java application.

Note

The instructions in this topic are for non-legacy container types. If you have deployed an AWS Elastic Beanstalk application using a legacy container type, we recommend that you migrate to a non-legacy container type to gain access to new features. For instructions on how to check the container type and migrate your application, see Migrating Your Application from a Legacy Container Type. For instructions on using MySQL Connector/J with applications running on legacy container types, see Using Amazon RDS and MySQL Connector/J (Legacy Container Types).

To use Amazon RDS from your AWS Elastic Beanstalk application, you need to do the following:

  1. Create an Amazon RDS DB instance.

  2. Download and install MySQL Connector/J.

  3. Establish a database connection in your code by using the connectivity information for your Amazon RDS DB instance.

  4. Deploy your application to AWS Elastic Beanstalk.

This topic walks you through the following:

  • Using a new Amazon RDS DB instance with your application

  • Using an existing Amazon RDS DB instance with your application

Using a New Amazon RDS DB Instance with Java

This topic walks you through creating a new Amazon RDS DB Instance and using it with your Java application.

  1. Create an Amazon RDS DB Instance. You can create an RDS DB Instance in one of the following ways:

    • Create an RDS DB instance when you create a new application version. For instructions using the AWS Elastic Beanstalk console, see Creating New Applications.

    • Create an RDS DB instance when you launch a new environment with an existing application version. For instructions using the AWS Elastic Beanstalk console, see Launching New Environments.

    • If you already deployed an application to AWS Elastic Beanstalk, you can create an RDS DB Instance and attach it to an existing environment. For instructions using the AWS Elastic Beanstalk console, see Step 4: Deploy New Version.

  2. Download and install MySQL Connector/J for your development environment. For download and installation instructions, go to http://dev.mysql.com/downloads/connector/j.

  3. Establish a database connection in your code using your Amazon RDS DB Instance's connectivity information. You can access your connectivity information using environment variables. The following shows how you would connect to the database on an RDS instance.

    String dbName = System.getProperty("RDS_DB_NAME"); 
    String userName = System.getProperty("RDS_USERNAME"); 
    String password = System.getProperty("RDS_PASSWORD"); 
    String hostname = System.getProperty("RDS_HOSTNAME");
    String port = System.getProperty("RDS_PORT");

    Build your JDBC connection string:

    String jdbcUrl = "jdbc:mysql://" + hostname + ":" + port + "/" + dbName + "?user=" + userName + "&password=" + password;

    Your connection string should look similar to this:

    jdbc:mysql://mydbinstance.abcdefghijkl.us-east-1.rds.amazonaws.com:3306/employees?user=sa&password=mypassword

    For more information on getting started using the MySQL Connector/J to access your MySQL database, go to http://dev.mysql.com/doc/refman/5.0/en/connector-j.html.

  4. Copy the MySQL Connector/J JAR file into the Tomcat WEB-INF/lib directory.

  5. Deploy your updated application to your existing AWS Elastic Beanstalk environment. For information on how to deploy a new application version to an existing environment using the Elastic Beanstalk Console, see Step 4: Deploy New Version. For information on how to deploy your application using Eclipse, see Creating and Deploying AWS Elastic Beanstalk Applications in Java Using AWS Toolkit for Eclipse.

Note

To connect to Tomcat RDS environments you must load the driver explicitly using Class.forName(<driverClassName>) prior to the call to DriverManager.getConnection() in the Java code.

Using an Existing Amazon RDS DB Instance with Java

Amazon Relational Database Service (Amazon RDS) lets you quickly and easily provision and maintain a MySQL Server instance in the cloud. This topic discusses how you can use Amazon RDS and the MySQL Connector/J with your AWS Elastic Beanstalk application.

To use an existing Amazon RDS DB Instance and Java from your AWS Elastic Beanstalk application

  1. Create an AWS Elastic Beanstalk environment in one of the following ways:

    • Create a new application with a new environment. For instructions using the Elastic Beanstalk console, see Creating New Applications. For instructions using Eclipse, see Develop, Test, and Deploy. You do not need to create an RDS DB Instance with this environment because you already have an existing RDS DB Instance.

    • Launch a new environment with an existing application version. For instructions using the Elastic Beanstalk console, see Launching New Environments. You do not need to create an RDS DB instance with this environment because you already have an existing RDS DB instance.

  2. Configure your Amazon RDS DB Security Group to allow access from the Amazon EC2 security group used by your AWS Elastic Beanstalk application. For instructions on how to find the name of your EC2 security group using the AWS Management Console, see Amazon EC2 Security Groups. For more information, go to the "Authorizing Network Access to an Amazon EC2 Security Group" section of Working with DB Security Groups in the Amazon Relational Database Service User Guide.

  3. Download and install MySQL Connector/J for your development environment. For download and installation instructions, go to http://dev.mysql.com/downloads/connector/j.

  4. Create a JDBC connection string using your Amazon RDS DB Instance's public DNS name, port number, and (optionally) database name and login credentials. The following example shows a JDBC connection string that would connect to the employees database on an RDS instance at mydbinstance.abcdefghijkl.us-east-1.rds.amazonaws.com using port 3306, with the user name "sa" and the password "mypassword".

    jdbc:mysql://mydbinstance.abcdefghijkl.us-east-1.rds.amazonaws.com:3306/employees?user=sa&password=mypassword
  5. Configure your AWS Elastic Beanstalk environment to pass the string to your AWS Elastic Beanstalk application as an environment property. For instructions on how to do this, go to Using Custom Environment Properties with AWS Elastic Beanstalk.

  6. Retrieve the JDBC connection string from the environment property passed to your server instance from AWS Elastic Beanstalk and use MySQL Connector/J to access your Amazon RDS database. The following code example shows how to retrieve the JDBC_CONNECTION_STRING custom environment property from a Java Server Page (JSP).

    <p>
        The JDBC_CONNECTION_STRING environment variable is:
        <%= System.getProperty("JDBC_CONNECTION_STRING") %>
    </p>

    For more information on getting started using the MySQL Connector/J to access your MySQL database, go to http://dev.mysql.com/doc/refman/5.0/en/connector-j.html.

  7. Copy the MySQL Connector/J JAR file into the Tomcat WEB-INF/lib directory.

  8. Deploy your updated application to your existing AWS Elastic Beanstalk environment. For information on how to deploy a new application version to an existing environment using the Elastic Beanstalk Console, see Step 4: Deploy New Version. For information on how to deploy your application using Eclipse, see Develop, Test, and Deploy.

Note

To connect to Tomcat RDS environments you must load the driver explicitly using Class.forName(<driverClassName>) prior to the call to DriverManager.getConnection() in the Java code.