Trouble tunneling my local WordPress install to the mysql database on appfog

I’ve set up a wordpress install on appfog (using rackspace), and cloned the install to my local machine for development. I know the install works (using MAMP) because I created a local mysql database and changed wp-config.php to point to it. However, I want to develop without having to change wp-config.php every time I commit. After doing some research, it seems like the Appfog service Caldecott lets me tunnel into the mysql database on the server, using af tunnel. Unfortunately, I’m having issues with getting it working. Even if I change my MAMP mysql port to something like 8889, and tunnel mysql through port 3306, it looks like it’s connected but I still get “Error establishing a database connection” when loading my localhost WordPress. When I quit the mysql monitor (using ctrl+x, ctrl+c), I get a message stating “Error: ‘mysql’ execution failed; is it in your $PATH?’. Originally, no, it wasn’t, but I’ve fixed my PATH variable on my local machine so that when I go to Terminal and just type mysql, it loads up.

So I guess my question is 2 parts:

Read More

1.)Am I going with the right approach for WordPress development on my local machine

and

2.)If so, why is the tunnel not working?

Related posts

Leave a Reply

3 comments

  1. One way to deal with this is to mimic the VCAP_SERVICES environment variable on your local system with your local database settings. This would allow you to use the same custom AppFog wp-config.php file which looks at VCAP_SERVICES to get its db creds.

    export VCAP_SERVICES='{"mysql-5.1": [{"credentials": {                      
        "hostname": "localhost",          
        "port": 3306,            
        "password": "root",            
        "name": "LOCAL_DATABASE_NAME",            
        "user": "root"}}]}'
    

    EDIT: You will need to restart the Apache server in MAMP after setting this env var. (Thanks Dex)

    This will eliminate the need to point your development code at your production database.

  2. simply test for whether vcap_services are available. if they are, use one config. if they arent, use a different config.

    here is an example of my local + appfog development website configuration file.

    if(getenv("VCAP_SERVICES")){
        //if in webserver
        $services_json = json_decode(getenv("VCAP_SERVICES"),true);
        $mysql_config = $services_json["mysql-5.1"][0]["credentials"];
        $username = $mysql_config["username"];
        $password = $mysql_config["password"];
        $hostname = $mysql_config["hostname"];
        $port = $mysql_config["port"];
        $db = $mysql_config["name"];
    
        define("DB_SERVER", "$hostname");
        define("DB_USER", "$username");//enter your database username
        define("DB_PASS", "$password");//databse password
        define("DB_NAME", "$db");//database name
    } else {
         //if in local development
         define("DB_SERVER", "localhost");
         define("DB_USER", "website");//enter your database username
         define("DB_PASS", "dfgdfgdf");//databse password
         define("DB_NAME", "fgdf_web");//database name
    }
    

    also, you can use .afignore same way you’d use .gitignore to ignore some files from the af update feature. u can update once with appropriate config, then add afignore, then it will never get updated again.

  3. Here is a quick and very dirty script to automate the process based on Tim Santeford’s answer. Be sure to change the LOCAL_DATABASE_NAME

    #!/bin/bash
    
    export VCAP_SERVICES='{"mysql-5.1": [{"credentials": {"hostname": "localhost", "port": 8889, "password": "root", "name": "LOCAL_DATABASE_NAME", "user": "root"}}]}'
    
    /Applications/MAMP/Library/bin/httpd -k stop
    
    sleep 3
    
    /Applications/MAMP/Library/bin/httpd -k start