wercker.com connect to MySQL database
<h1>Background</h1>
<p>I'm trying to build some automated tests for a Yii2 app using <a href="http://wercker.com" rel="nofollow noreferrer">http://wercker.com</a>. I have created my own Docker repo <a href="https://hub.docker.com/r/consynki/yii2/" rel="nofollow noreferrer">https://hub.docker.com/r/consynki/yii2/</a> that provides a simple LAMP stack.</p>
<p>I'm using that Docker repo as the box in my wercker.yml file with a few simple steps to setup my app, initialized the database, then run my phpunit tests</p>
<pre><code>box: consynki/yii2

build:
  steps:

- script:
    name: Update enviroment dependencies
    code: |-
      curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin/ --filename=composer

- script:
    name: Install Composer dependencies
    code: |-
      rm -rf ./vendor
      /usr/bin/composer install --no-interaction --prefer-source

- script:
    name: Apache site install
    code: |-
      sudo chmod -R 755 /var/www
      sudo cp -r ./ /var/www/example.lan/
      sudo chown -R $USER:$USER /var/www/example.lan/
      sudo cp ./config/example.lan.conf /etc/apache2/sites-available/example.lan.conf
      sudo a2ensite example.lan.conf
      sudo cp -fr ./config/hosts /etc/hosts
      sudo service apache2 restart

- script:
    name: Create database
    code: |-
      mysql -uroot -e &quot;SHOW DATABASES;&quot;
      mysql -uroot -e &quot;CREATE DATABASE IF NOT EXISTS test&quot;

- script:
    name: PHPUnit integration tests
    code: |-
      ./vendor/bin/phpunit --configuration phpunit.xml
</code></pre>
<h1>Problem</h1>
<p>The problem is, when I run my build using the wercker CLI <code>wercker build</code>, it fails to connect to mysql. I keep getting the following connection error <code>ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)</code> when trying to run the 'Create database' step.</p>
<pre><code> - script:
    name: Create database
    code: |-
      mysql -uroot -e &quot;SHOW DATABASES;&quot;
      mysql -uroot -e &quot;CREATE DATABASE IF NOT EXISTS test&quot;
</code></pre>
<p>I know the docker container has a mysql connection as I can ssh into it and run <code>mysql -uroot -e &quot;SHOW DATABASES;&quot;</code></p>
<p><strong>edit</strong> My current work in progress wercker lamp template is available at <a href="https://github.com/levi-putna/php-cli-template" rel="nofollow noreferrer">https://github.com/levi-putna/php-cli-template</a></p>
<h1>Request</h1>
<p>I know wercker.com is relatively new, and doesn't have a lot of documentation. But I was hoping someone could provide me with an example of how to use mysql within a wercker test run.</p>
<h1>Edit - Using Wercker Service</h1>
<p>I have also tries connecting using a Wercker MariaBD service, it's not exactly MySQL so not a true test of my production environment, but should be good enough to get testing started.</p>
<p>I updates my wercker.yml to add a the mariadb service. Simplified my steps to just get the DB connection stuff working.</p>
<pre><code>box: consynki/yii2

services:
    - id: mariadb

      # your credentials for Docker Hub
      username: $USERNAME
      password: $PASSWORD
      tag: latest

      # set the required environment variable
      env:
        MYSQL_ROOT_PASSWORD: mypassword
        MYSQL_DATABASE: test_database
        MYSQL_USER: admin
        MYSQL_PASSWORD: test123

build:

  steps:

    - script:
        name: Create database
        code: |-
          mysql -h $MARIADB_PORT_3306_TCP_ADDR -P $MARIADB_PORT_3306_TCP_PORT -u $MYSQL_USER -p test123 -v
          mysql -h $MARIADB_PORT_3306_TCP_ADDR -P $MARIADB_PORT_3306_TCP_PORT -u $MYSQL_USER -p test123 -e &quot;SHOW DATABASES;&quot;
</code></pre>
<p>I still seem to be getting the same error as my original approach. <code>ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)</code></p>
<p>I'm also seeing some additional log messages after the wercker steps exit.</p>
<pre><code>WARNING Box container has already stopped.
Initializing database
2016-03-01 22:51:00 140429748197312 [Note] /usr/sbin/mysqld (mysqld 10.1.12-MariaDB-1~jessie) starting as process 51 ...
2016-03-01 22:51:00 140429748197312 [Note] InnoDB: Using mutexes to ref count buffer pool pages
2016-03-01 22:51:00 140429748197312 [Note] InnoDB: The InnoDB memory heap is disabled
2016-03-01 22:51:00 140429748197312 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-03-01 22:51:00 140429748197312 [Note] InnoDB: Memory barrier is not used
2016-03-01 22:51:00 140429748197312 [Note] InnoDB: Compressed tables use zlib 1.2.8
2016-03-01 22:51:00 140429748197312 [Note] InnoDB: Using Linux native AIO
2016-03-01 22:51:00 140429748197312 [Note] InnoDB: Using SSE crc32 instructions
2016-03-01 22:51:00 140429748197312 [Note] InnoDB: Initializing buffer pool, size = 256.0M
2016-03-01 22:51:00 140429748197312 [Note] InnoDB: Completed initialization of buffer pool
2016-03-01 22:51:00 140429748197312 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2016-03-01 22:51:00 140429748197312 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2016-03-01 22:51:00 140429748197312 [Note] InnoDB: Database physically writes the file full: wait...
2016-03-01 22:51:01 140429748197312 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2016-03-01 22:51:01 140429748197312 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2016-03-01 22:51:01 140429748197312 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2016-03-01 22:51:01 140429748197312 [Warning] InnoDB: New log files created, LSN=45883
2016-03-01 22:51:01 140429748197312 [Note] InnoDB: Doublewrite buffer not found: creating new
2016-03-01 22:51:01 140429748197312 [Note] InnoDB: Doublewrite buffer created
2016-03-01 22:51:01 140429748197312 [Note] InnoDB: 128 rollback segment(s) are active.
2016-03-01 22:51:01 140429748197312 [Warning] InnoDB: Creating foreign key constraint system tables.
2016-03-01 22:51:01 140429748197312 [Note] InnoDB: Foreign key constraint system tables created
2016-03-01 22:51:01 140429748197312 [Note] InnoDB: Creating tablespace and datafile system tables.
2016-03-01 22:51:01 140429748197312 [Note] InnoDB: Tablespace and datafile system tables created.
2016-03-01 22:51:01 140429748197312 [Note] InnoDB: Waiting for purge to start
2016-03-01 22:51:01 140429748197312 [Note] InnoDB:  Percona XtraDB (http://www.percona.com) 5.6.28-76.1 started; log sequence number 0
2016-03-01 22:51:01 140428963710720 [Note] InnoDB: Dumping buffer pool(s) not yet started
2016-03-01 22:51:03 140216194230208 [Note] /usr/sbin/mysqld (mysqld 10.1.12-MariaDB-1~jessie) starting as process 80 ...
2016-03-01 22:51:03 140216194230208 [Note] InnoDB: Using mutexes to ref count buffer pool pages
2016-03-01 22:51:03 140216194230208 [Note] InnoDB: The InnoDB memory heap is disabled
2016-03-01 22:51:03 140216194230208 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-03-01 22:51:03 140216194230208 [Note] InnoDB: Memory barrier is not used
2016-03-01 22:51:03 140216194230208 [Note] InnoDB: Compressed tables use zlib 1.2.8
2016-03-01 22:51:03 140216194230208 [Note] InnoDB: Using Linux native AIO
2016-03-01 22:51:03 140216194230208 [Note] InnoDB: Using SSE crc32 instructions
2016-03-01 22:51:03 140216194230208 [Note] InnoDB: Initializing buffer pool, size = 256.0M
2016-03-01 22:51:03 140216194230208 [Note] InnoDB: Completed initialization of buffer pool
2016-03-01 22:51:03 140216194230208 [Note] InnoDB: Highest supported file format is Barracuda.
2016-03-01 22:51:03 140216194230208 [Note] InnoDB: 128 rollback segment(s) are active.
2016-03-01 22:51:03 140216194230208 [Note] InnoDB: Waiting for purge to start
2016-03-01 22:51:03 140216194230208 [Note] InnoDB:  Percona XtraDB (http://www.percona.com) 5.6.28-76.1 started; log sequence number 1616799
2016-03-01 22:51:04 140215410722560 [Note] InnoDB: Dumping buffer pool(s) not yet started
2016-03-01 22:51:06 140659046377408 [Note] /usr/sbin/mysqld (mysqld 10.1.12-MariaDB-1~jessie) starting as process 109 ...
2016-03-01 22:51:06 140659046377408 [Note] InnoDB: Using mutexes to ref count buffer pool pages
2016-03-01 22:51:06 140659046377408 [Note] InnoDB: The InnoDB memory heap is disabled
2016-03-01 22:51:06 140659046377408 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-03-01 22:51:06 140659046377408 [Note] InnoDB: Memory barrier is not used
2016-03-01 22:51:06 140659046377408 [Note] InnoDB: Compressed tables use zlib 1.2.8
2016-03-01 22:51:06 140659046377408 [Note] InnoDB: Using Linux native AIO
2016-03-01 22:51:06 140659046377408 [Note] InnoDB: Using SSE crc32 instructions
2016-03-01 22:51:06 140659046377408 [Note] InnoDB: Initializing buffer pool, size = 256.0M
2016-03-01 22:51:06 140659046377408 [Note] InnoDB: Completed initialization of buffer pool
2016-03-01 22:51:06 140659046377408 [Note] InnoDB: Highest supported file format is Barracuda.
2016-03-01 22:51:06 140659046377408 [Note] InnoDB: 128 rollback segment(s) are active.
2016-03-01 22:51:06 140659046377408 [Note] InnoDB: Waiting for purge to start
2016-03-01 22:51:06 140659046377408 [Note] InnoDB:  Percona XtraDB (http://www.percona.com) 5.6.28-76.1 started; log sequence number 1616809
2016-03-01 22:51:06 140658262116096 [Note] InnoDB: Dumping buffer pool(s) not yet started
ERROR: 1049  Unknown database 'test'
2016-03-01 22:51:06 140659046377408 [ERROR] Aborting
</code></pre>