Configure MySQL on AWS t2.micro Linux AMI

I moved my blog over to an AWS VM, because I get 12 months of a free t2.micro instance. Can’t beat free hosting for a year, right? And about $10/month after that, on my own private virtual machine. Assumed things were going well, but I came back a few weeks later to find everything had gone to hell. WordPress was not connecting to the database.

[ ~]$ sudo service mysqld start
Starting mysqld:                                           [FAILED]

In looking at my /var/log/mysqld.log file, I found

160120 21:49:58 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
160120 21:49:58 [Note] /usr/libexec/mysql55/mysqld (mysqld 5.5.46) starting as process 27540 ...
160120 21:49:58 [Note] Plugin 'FEDERATED' is disabled.
160120 21:49:58 InnoDB: The InnoDB memory heap is disabled
160120 21:49:58 InnoDB: Mutexes and rw_locks use GCC atomic builtins
160120 21:49:58 InnoDB: Compressed tables use zlib 1.2.8
160120 21:49:58 InnoDB: Using Linux native AIO
160120 21:49:58 InnoDB: Initializing buffer pool, size = 128.0M
InnoDB: mmap(137363456 bytes) failed; errno 12
160120 21:49:58 InnoDB: Completed initialization of buffer pool
160120 21:49:58 InnoDB: Fatal error: cannot allocate memory for the buffer pool
160120 21:49:58 [ERROR] Plugin 'InnoDB' init function returned error.
160120 21:49:58 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
160120 21:49:58 [ERROR] Unknown/unsupported storage engine: InnoDB
160120 21:49:58 [ERROR] Aborting
160120 21:49:58 [Note] /usr/libexec/mysql55/mysqld: Shutdown complete

The important thing here is InnoDB: mmap(137363456 bytes) failed; errno 12.

It looks like it couldn’t allocate the memory, which makes sense because a t2.micro instance only has a gig of RAM. Ought to be enough for anyone, right? Not for MySQL! The way to fix this is to open up your /etc/my.cnf file, and add a param innodb_buffer_pool_size = 1M (something reasonable). If you haven’t made any other changes, it should look similar to this:

# Disabling symbolic-links is recommended to prevent assorted security risks
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mysqld according to the
# instructions in

innodb_buffer_pool_size = 1M


Now if you start MySQL back up, everything should be fine.

[ ~]$ sudo service mysqld start
Starting mysqld:                                           [  OK  ]

Using Java 8 Lambdas with Google Guava Caches

With Guava, you can define a simple in-memory cache with

import static java.util.concurrent.TimeUnit.DAYS;

Cache cache =
        .expireAfterAccess(7, DAYS)

With this you can use .put(K, V) to load values, and .getIfPresent(K), which returns null if the key isn’t present. Sometimes it’s more convenient to use get(K key, Callable<? extends V> valueLoader), where the valueLoader is called on a cache miss, and populates the cache and gives you what a cache hit would have given you. The old Java 7 way of doing this was really ugly:

cache.get(key, new Callable() {
    public V call() {
        return calculatedValue(key);

Don’t write ugly code. With Java 8 Lambdas, just do this:

cache.get(key, () -> {

How to parameterize an RSpec shared context

I had a RSpec shared_context which was creating a FactoryGirl user mock and then logging in with it, and then running some shared examples for testing permissions on a generic user. I needed to modify it to accept a parameter of using a different user.

It originally looked like this:

RSpec.shared_context "auth" do
  let(:current_user) { FactoryGirl.create(:user) }

  before(:each) do
    allow(User).to receive(:find_by_id)
    allow(User).to receive(:find_by_id).with( { current_user }

  shared_context "logged in" do
    before(:each) do
      login_as current_user

In my spec files, it was included by doing this:

describe MyController do
  include_context 'auth'

  context "when logged in as a normal user" do
    include_context 'logged in'


However I needed MyController to override current_user and use an admin user instead. Rather conveniently, you can override a context by sending it a block.

describe MyController do
  include_context 'auth'

  context "when logged in as an admin" do
    include_context 'logged in' do
      let(:current_user) { FactoryGirl.create(:admin) }


This works because the let inside of the block given is called again, and the new block calls let(:current_user) again and overrides the original block and FactoryGirl.create(:user) is never called.

This is documented here, however they don’t really show that it can be used for making your shared_context parameterized. So I’m going to say the word parameterized a lot here, and hope that when I (or you!) google “How do I parameterize an RSpec shared_context?”, you find this post.

Weblabora, Sailing, and Portland

Hi friends. I’ve been learning a lot of things recently, but nothing cool enough to be worth an entire blog post of its own, so here they are.


I’ve been resurrecting Weblabora recently by abstracting out the game logic into a JAR and rewriting the front-end with more modern technology. The front-end is also open source on Github and will be hosted at Not much to see right now, but I am making slow and steady progress. The idea, this time, is to make it so that more games can be added relatively easily.

A JAR of the game library is published to Maven repositories is the first JAR I’ve published under the com.philihp group


Lately I’ve been learning how to sail. The other day in practicing man-over-board drills in a J/24 we spotted a watermelon floating in San Francisco Bay. We rescued it and ate it for lunch.



For the second half of 2015 I will be living in Portland, Oregon. That’s about all there is to say about that.

How to use JUnit4 Hamcrest Matchers

JUnit borrows matchers from a library called Hamcrest, which look a lot like RSpec matchers in Ruby. They can be tricky to write, but end up being much more readable for complex assertions. At its simplest case, we can turn this assertion



assertThat(board.isGameOver(), is(true));

It’s slightly wordier, but I think it reads in English a little better, and is capable of still-readable more-complex assertions. The is() function here isn’t from extending TestCase (JUnit 4 no longer needs this, that was a JUnit 3 thing). It’s actually a static import, although there seems to be conflicting information on the internet on what needs to be imported. I found these to work in most cases:

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

From what I can tell, JUnit pulled in some, but not all of the matchers from Hamcrest. There are still some useful matchers in Hamcrest, so I like to import them, and there aren’t any namespace collisions. It doesn’t add any additional lines to your POM since you can actually leave out the JUnit dependency and just import the Hamcrest library (since it itself depends on JUnit).


In the actual test that motivated this post, I had a really specific need to test that a method would return a list which contained at any index a POJO bean which had properties for settlement being an instance of a Village class and score being 42. I was able to express this as

          hasProperty("settlement", instanceOf(Village.class)),
          hasProperty("score", is(42)))));

New City, New Job, New Format

I have relocated to San Francisco, and I now work at Airbnb as a Software Engineer.

Unfortunately, this blog will probably not see many more SAS-related posts until I can get my hands on a license. It will probably see a lot of new blog posts with newer, freer technologies. I’m learning a lot of Ruby and Rails, so expect some cool stuff on that front.

Also, I’ll probably drop more F-bombs.

Using SUBSTR on the left of an assignment in SAS

This is a pretty cool feature, and as far as I know it’s unique to SAS and Perl. In SAS, you can have a substr to the left of an assignment.


This avoids potentially awkward situations like this:


The bounds don’t have to be static. Lets suppose we have a dataset with 1 row per letter, and we want to combine those letters into a word.

data have;
    input letter $1.;

My initial thought was to just retain and append each letter:

data want;
  length word $200;
  retain word;
  set have nobs=nobs;
  call cats(word,letter);
  if(nobs=_n_) then output;

However this breaks if one of the letters is a space, and SAS treats the space as null, so appending a space has no effect on the string. Instead of appending, just stick the letter where it’s supposed to be.

data want;
  length word $200;
  retain word ' ';
  set have nobs=nobs;
  substr(word,_n_,1) = letter;
  if(_n_==nobs) then output;

Installing SAS 9.4 on Ubuntu 14.04

Officially SAS does not support Ubuntu Linux as a platform, in spite of it being the most popular desktop distribution. It will run, but there are two things you need to do:

First, reconfigure Ubuntu not to use dash. The SAS Deployment Wizard will freeze when you first click “Next” during the install if you don’t do this.

sudo dpkg-reconfigure dash

That being done, you should be able to install SAS Foundation.

Second, SAS requires libjpeg to run. When you first run sas, you’ll get this error:

        MODULE: sasmotifsasvsub HyS SUBSYSTEM: 8 SLOT: 11 
ERROR: Could not load /usr/local/SASHome/SASFoundation/9.4/sasexe/sasmotif (35 images loaded) 
ERROR: cannot open shared object file: No such file or directory 
        MODULE: sasmotifsasvsub HyS SUBSYSTEM: 8 SLOT: 11

Install this by running this command:

sudo apt-get install libjpeg62

SAS should now run.

Additionally, while not common, but if you have SAS/Secure installed and want to pull files via https, you’ll need to point it at SSL CA certificates. You can kick off SAS with


Or just create a file ~/sasv9.cfg with that in it.

Update: This works on 13.10 and 14.04

SAS Macro Variable Resolution Timing

I thought this was neat and quirky. The implications of it are important, too. When you assign a macro variable in SAS, it doesn’t resolve immediately. It resolves It will also warn you if the variable doesn’t exist yet, too.

%let x = &y;
%let y = z;
%put &x;

Will output


Even though, when x was defined, y did not exist. We’ll get the following warning, which can be ignored.

WARNING: Apparent symbolic reference Y not resolved.

As far as I can tell (I could be wrong!), I don’t think there’s a way of turning off that warning.

Using Secure HTTPS (SSL) URLs in SAS on Linux

Out of the box SAS 9.4 is not configured to use SSL on Linux x64. If you try to do this, you’ll get the error

ERROR: SSL Error: Missing CA trust list

This happens because when SAS gets the SSL certificate from the server, it sees that it’s signed by a Certificate Authority, but it doesn’t have the SSL certificates from those authorities. Those come with OpenSSL, so get that.

sudo apt-get install openssl

With that installed, the SSL certificates should be in /etc/ssl/certs, with ca-certificates.crt being an uber-cert that contains all of the other files in one file. To tell SAS to look at that file, add this to SASHOME/SASFoundation/9.4/sasv9_local.cfg:


You’ll probably need sudo to modify that file, but you probably had that if you installed OpenSSL. But maybe your admin installed it, he just didn’t know to configure it, and he’s out on holiday because he’s European. You can put that config option in ~/sasv9.cfg instead, creating it if it doesn’t exist.

NOTE: This was tested on Ubuntu Linux x64 13.10. The location of the cert file will probably differ from distro to distro and version to version. Just look for the SSL CA cert directory, and find the biggest file in it, that’s probably the uber-cert.