Stay updated with announcements, get answers from the community, and share your feature suggestions with us.
You can also submit a request or send us an email at support@appfog.zendesk.com.
Lucas Carlson Apr 17 • 1 • Tips & Tricks
Details coming soon.
In the meantime, browse the repositories we're using in production on AppFog: https://github.com/appfog
Especially the parts with database connections like https://github.com/appfog/af-php-wordpress/blob/master/wp-config.php
If you want more information, email jumpstarts@appfog.com
Lucas Carlson Apr 17 • Tips & Tricks
With AppFog, when you create and bind services to your app, it modifies an environment variable called VCAP_SERVICES.
This is easy to access with any Ruby application.
MySQL Example
gem "mysql2"
services = JSON.parse(ENV['VCAP_SERVICES'])
mysql_key = services.keys.select { |svc| svc =~ /mysql/i }.first
mysql = services[mysql_key].first['credentials']
mysql_conn = {:host => mysql['hostname'], :port => mysql['port'], :username => mysql['user'], :password => mysql['password']}
connection = Mysql2::Client.new(mysql_conn)
MongoDB Example
gem "mongo"
services = JSON.parse(ENV['VCAP_SERVICES'])
mongo_key = services.keys.select { |svc| svc =~ /mongo/i }.first
mongo = services[mongo_key].first['credentials']
mongo_conn = {:host => mongo['hostname'], :port => mongo['port'], :username => mongo['user'], :password => mongo['password']}
connection = Mongo::Connection.new(mongo['hostname'], mongo['port']).db(mongo['name']).authenticate(mongo['user'], mongo['password'])
Postgres Example
gem "pg"
services = JSON.parse(ENV['VCAP_SERVICES'])
postgresql_key = services.keys.select { |svc| svc =~ /postgresql/i }.first
postgresql = services[postgresql_key].first['credentials']
postgresql_conn = {:host => postgresql['hostname'], :port => postgresql['port'], :username => postgresql['user'], :password => postgresql['password'], :dbname => postgresql['name']}
connection = PG.connect(postgresql_conn)
Lucas Carlson Apr 17 • Tips & Tricks
With AppFog, when you create and bind services to your app, it modifies an environment variable called VCAP_SERVICES.
This is easy to access with any Node application.
MySQL Example
var env = JSON.parse(process.env.VCAP_SERVICES);
var mysql = env['mysql-5.1'][0]['credentials'];
var client = require('mysql').createClient({
user: mysql.username,
password: mysql.password,
port: mysql.port,
database: mysql.name,
host: mysql.hostname
});
MongoDB Example
var env = JSON.parse(process.env.VCAP_SERVICES);
var mongo = env['mongodb-1.8'][0]['credentials'];
var mongo_conn = "mongodb://" + mongo.username + ":" + mongo.password + "@" + mongo.hostname + ":" + mongo.port + "/" + mongo.db;
require('mongodb').connect(mongo_conn, function(err, conn){});
Postgres Example
var env = JSON.parse(process.env.VCAP_SERVICES);
var postgresql = env['postgresql-9.1'][0]['credentials'];
var postgresql_conn = "tcp://" + postgresql.username + ":" + postgresql.password + "@" + postgresql.hostname + ":" + postgresql.port + "/" + postgresql.db;
require('pg').connect(postgresql_conn, function(err, client) {});
Lucas Carlson Apr 17 • Tips & Tricks
With AppFog, when you create and bind services to your app, it modifies an environment variable called VCAP_SERVICES.
This is easy to access with any PHP application.
MySQL Example
$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"];
$link = mysql_connect("$hostname:$port", $username, $password);
$db_selected = mysql_select_db($db, $link);
MongoDB Example
$services_json = json_decode(getenv("VCAP_SERVICES"),true);
$mongo_config = $services_json["mongodb-1.8"][0]["credentials"];
$username = $mongo_config["username"];
$password = $mongo_config["password"];
$hostname = $mongo_config["hostname"];
$port = $mongo_config["port"];
$db = $mongo_config["db"];
$name = $mongo_config["name"];
$connect = "mongodb://${username}:${password}@${hostname}:${port}/${name}";
$m = new Mongo($connect);
$db = $m->selectDB($db);
Postgres Example
$services_json = json_decode(getenv("VCAP_SERVICES"),true);
$postgresql_config = $services_json["postgresql-9.1"][0]["credentials"];
$username = $postgresql_config["username"];
$password = $postgresql_config["password"];
$hostname = $postgresql_config["hostname"];
$port = $postgresql_config["port"];
$db = $postgresql_config["name"];
$link = pg_connect("host=$hostname port=$port dbname=$db user=$username password=$password");
Matthew Zalewski November 07, 2011 • 12 • Community Help
Is it possible to access a mysql instance remotely? It would be great to be able to import existing data from a local mysql database - I've managed to get the username/password/host from the env settings, but it doesn't want to connect.