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. Log in to the Cloud Control Panel.
  2. In the top navigation bar, select Databases > Database Instances
  3. Click the name of the instance that you want to connect to.  
  4. Copy the hostname string.
  5. From a terminal window, use SSH to log in to a cloud server that's been created in the same region as your cloud database. Following is an example SSH command:

            ssh user@IPaddress
            
  6. On your server, use the MySQL client (or a similar tool) to access the database. For MySQL, use the following 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 that creates a very simple webpage. You can use this webpage to test that your MySQL database is working. You can also use it as a very simple calculator. Copy the script and paste it into a text editor. Then modify the script with your own hostname, user name, 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 database (that is, able to connect to the database via the cloud private network).

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

Copy and 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 in to the Cloud Control Panel.
  2. In the top navigation bar, select Databases > Database Instances.
  3. Click the name for the instance you want to connect to and view the details for the instance.
  4. 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 section of the database instance details page 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 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 server (for example, to the website's cgi-bin folder).
  2. Execute the script to test connectivity to your database.

    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

  3. Type 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 in to the Cloud Control Panel.
  2. In the top navigation bar, select Databases > Database Instances.
  3. Click the name of the instance that you want to connect to the load balancer and view the details for the instance.
  4. Note the region that this database is in. You must create the load balancer in the same region.
  5. Copy the hostname string.

Create a Load Balancer for the Instance

  1. In the top navigation bar of the Cloud Control Panel, click Networking.
  2. In the menu, under Create Resources, click Load Balancer.

    The Create Load Balancer page appears.

  3. Enter a name for the load balancer.
  4. Specify the same region in which your database instance is located.
  5. In the Configuration section, select Accessible on the Public Internet for the Virtual IP option.
  6. Select MySQL for the Protocol/Port.
  7. In the Add Nodes section, click Add External Node.
  8. Paste the hostname string of the database instance in the IP or Hostname field.
  9. Enter the default MySQL port 3306 for the Port.
  10. Click Add External Node.
  11. Click Create Load Balancer.

The load balancer is created and your 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?




© 2015 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