Connecting to Your Cloud Database


This article describes three methods for connecting to your Cloud Database: 

  • Connect Through a Cloud Server Using ssh and the Hostname 
  • Connect Directly to the Database Using the Hostname
  • Connect Using a Cloud Load Balancer

Connect Through a Cloud Server Using ssh and the Hostname

  1. Open the New Cloud Control Panel and click Databases at the top of the page. 
  2. Click the instance name that you want to connect to.  
  3. Electronically copy the Hostname string.
  4. From a terminal window, use ssh to log into a Cloud Server that's been created in the same region as your Cloud Database. Here's an example ssh command:

    ssh user@IPaddress
  5. On your server, use the MySQL client (or a similar tool) to access the database. For MySQL, use this command and paste the hostname string following the -h option:

    mysql -h hostname_string -u database_instance_username -p

 

Connect to the Database Directly Using the Hostname 

This section provides a sample script at the end of the section that creates a very simple webpage. You can use this webpage to test that your MySQL database is alive and well. You can also use it as a very simple calculator. Copy / paste the script into a text editor. Then modify the script with your own hostname, user, password, and database name information and save the changes. Copy the script to your Cloud Server, and execute the script to display the simple webpage and test your connection to your database.

Your web server will need to be in the same region as your Cloud Database (i.e. able to connect to the database via the cloud private network).

Note: this assumes that you already have your web server and PHP configured properly.

Copy / paste the script at the end of this section

  1. Copy the PHP Script provided at the end of this section.
  2. Paste it into a text editor.

Copy the Instance Hostname

  1. Log into the New Cloud Control Panel and click Databases at the top of the page. 
  2. Click the instance name for the database you want to connect to and view the details for the instance. 
  3. Electronically copy the Hostname string.

Paste the Instance Hostname into the text editor

  1. Locate the following line of the script in the text editor:
    $THE_HOST = "5c70345ad036fc112dc0a14ee1db7992f5c172db.rackspaceclouddb.com";
    
  2. Paste the Hostname string inside the double quotation marks, replacing the following value:
    5c70345ad036fc112dc0a14ee1db7992f5c172db.rackspaceclouddb.com
    

Modify the information in the script to specify your database user, password, and database name

  1. Locate the following line of the script in the text editor:
    $THE_USER = "fmdb_readonly";
    
  2. Replace the value fmdb_readonly with the name of your database user (you can find this information in the Users panel for the Database in the control panel).
  3. Locate the following line of the script in the text editor:
    $THE_PWD = "fmdb_readonly";
    
  4. Replace the value fmdb_readonly with the password for the user.
  5. Locate the following line of the script in the text editor:
    $THE_DB = "FEATUREMANIA";
    
  6. Replace the value FEATUREMANIA with the name of your database.
  7. Save the changes you made to the script in the editor to a file name that ends in .php (for example clouddatabases.php).
    Note: if you change the script file name to something other than clouddatabases.php, you will need to modify the following line in the script to replace the value clouddatabases.php with the name you chose for your script:
     <FORM ACTION='clouddatabases.php' METHOD='POST'>

Copy the modified script and execute it on your Cloud Server

  1. Copy the modified script to your Cloud Server (for example, to the website's cgi-bin folder).
  2. Execute the script to test connectivity to your database.
  3. The web page should run and display a message similar to the following if the connection to your database succeeds:
    The database connection worked, and MySQL says that PI()*3*3 = 28.274334
  4. You can then type in a MySQL expression (for example PI()*3*3) and click Submit to have it evaluated.

PHP Script: COPY-PASTE THIS SCRIPT (and replace with your own HOST, DB, USER and PWD values)

<html> 
<head><title>Connecting to Cloud Databases</title></head> 
<body><pre> 
<?php 
    // phpinfo(); 
    $THE_HOST = "5c70345ad036fc112dc0a14ee1db7992f5c172db.rackspaceclouddb.com"; 
    $THE_USER = "fmdb_readonly"; 
    $THE_PWD = "fmdb_readonly"; 
    $THE_DB = "FEATUREMANIA"; 

    // 
    // Get "e" 
    // 
    $arg_expr = trim($_POST["e"]); 
    if($arg_expr == "") { 
        $arg_expr = "PI()"; 
    } 
    else { 
        if(get_magic_quotes_gpc()) { 
         $arg_expr = stripslashes($arg_expr); 
        } 
        
        // 
        // Connect to the database 
        // 
        $connection = mysql_connect($THE_HOST, $THE_USER, $THE_PWD); 
        if (!$connection) { 
             die('I could not connect to the database. The error is: ' . mysql_error()); 
        } 
        mysql_select_db($THE_DB, $connection); 
        // 
        // Calculation 
        // 
        $result = mysql_query("SELECT (" . $arg_expr . ");", $connection); 
        $row = mysql_fetch_array($result, MYSQL_NUM); 
        $eValue = $row[0]; 
        printf("The database connection worked, and MySQL says that %s = %s<BR>%s", $arg_expr, $eValue, mysql_error());
        mysql_free_result($result); 
        mysql_close($connection); 
    } 
?> 
    <FORM ACTION='clouddatabases.php' METHOD='POST'> 
        Enter a MySQL expression: 
        <INPUT TYPE="TEXT" NAME="e" VALUE="<? echo $arg_expr; ?>"/> 
        <INPUT TYPE="SUBMIT"> 
    </FORM> 
    This is a simple PHP example to test your connection to Rackspace Cloud Databases. 
    It does not require your database to have any tables. 
    It doubles as a handy way to calculate simple MySQL expressions from the browser. 
    <BR> 
    Because this sample uses string concatenation to compose SQL statements, only use this in your development environment in your password-protected site. 
    <BR>  
    EXAMPLES: 
        PI()*3*3 
        curdate() 
        3=3 AND 4>4 
        MID('Rackspace',1,4) 
        SIN(PI()/2) 
        SHA1('Rackspace Cloud Databases') 
    </pre></body> 
</html>

 

Connect Using a Cloud Load Balancer 

Note: This Cloud Load Balancer should only be used to access your Cloud Database. Do not add other nodes to the load balancer. 

Copy the Instance Hostname

  1. Log into the New Cloud Control Panel and Databases at the top of the page. 
  2. Click the instance name that you want to connect to the load balancer and view the details for the instance. 
  3. Take note of the Region this database is in. You'll need to create the load balancer in the same region. 
  4. Electronically copy the Hostname string. 

Create a Load Balancer for the Instance

  1. Click Load Balancers at the top of the page. 
  2. Click Create Load Balancer. The Create Load Balancer page appears.
  3. Enter a Name for the load balancer. 
  4. In the Configuration area, select Accessible on the Public Internet for the Virtual IP option. 
  5. Select MySQL for the Protocol/Port.
  6. Select the Region, making sure it's the same one that your database is located in. 
  7. Click Add External Node. 
  8. Electronically paste the hostname string of the database instance in the IP or Hostname field. 
  9. Enter default MySQL Port 3306 for the Port.
  10. Click Add External Node. 
  11. Click Create Load Balancer.

The Cloud Load Balancer is created and your Cloud Database is now accessible through the load balancer. 

Note: Using a Cloud Load Balancer to access your database instance incurs normal load balancing and bandwidth charges. For more information, see Cloud Load Balancers Pricing and Calculator



Was this content helpful?




© 2014 Rackspace US, Inc.

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License


See license specifics and DISCLAIMER