title: Intermittent MySQL connection on Vagrant VirtualBox when Jenkins runs PHPUnit
body: <p>We have a Jenkins CI server that runs our suite of tests on every commit, triggered by a GitHub hook.</p>
  
  <p>We recently moved the suite of tests from running locally on the Jenkins server to running inside a VirtualBox/Vagrant VM. This is to ensure that the test configuration matches the dev environment. This is an Ubuntu 14.04 guest running on Ubuntu 14.04 host.</p>
  
  <p>After moving to the VM model, PHPUnit occasionally fails with no connection to MySQL. The error is <code>Can't connect to MySQL server on '127.0.0.1'</code>. </p>
  
  <p>This error is intermittent, not easily reproducible. That is, if I trigger a new build on Jenkins, it usually succeeds. However, when the new build is triggered by the GitHub hook, it fails more often than manually triggered builds, and sometimes succeeds.</p>
  
  <p>Here's what I tried:</p>
  
  <ul>
  <li><code>sudo service mysql restart</code> before running <code>phpunit</code></li>
  <li><code>sleep 5</code> between the <code>mysql restart</code> and <code>phpunit</code></li>
  <li>Connecting to <code>localhost</code> and <code>127.0.0.1</code> -- When I tried connecting to <code>localhost</code>, I received intermittent errors <code>Can't connect to MySQL server on '/var/run/mysqld/mysqld.sock'</code>.</li>
  </ul>
  
  <p>Here's the full output of the failed build:</p>
  
  <pre><code>sudo service mysql restart
   * Stopping MySQL (Percona Server) mysqld
     ...done.
   * Starting MySQL (Percona Server) database server mysqld
     ...done.
   * Checking for corrupt, not cleanly closed and upgrade needing tables.
  
  sleep 5
  
  sudo service mysql status
   * /usr/bin/mysqladmin  Ver 8.42 Distrib 5.6.23-72.1, for debian-linux-gnu on x86_64
  
  Server version      5.6.23-72.1-log
  Protocol version    10
  Connection      Localhost via UNIX socket
  UNIX socket     /var/run/mysqld/mysqld.sock
  Uptime:         6 sec    
  Threads: 1  Questions: 111  Slow queries: 0  Opens: 761  Flush tables: 1  Open tables: 754  Queries per second avg: 18.500
  
  phpunit
  PHPUnit 4.6.2 by Sebastian Bergmann and contributors.    
  
  Configuration read from /vagrant/phpunit.xml    
  
  ...........EEE.E.............E............................EEEEE.    
  
  Time: 8.51 seconds, Memory: 135.25Mb    
  
  1) ProcessDatasetsTest::test_process_on_census_fraction
  PDOException: SQLSTATE[HY000] [2003] Can't connect to MySQL server on '127.0.0.1' (111)
  </code></pre>